Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java = Return Object list/array vs. Result-Object (the same with method parameters)

Tags:

java

oop

This might seem to be a strange question: I am struggling to decide whether it is a good practice and "efficient" to work with "Typed Objects" on a very granular level.

public Object[] doSomething() {
    Object[] resultList = new Object[] {new Foo(), new Bar()};
    return resultList;
}

versus

public Result doSomething() {
    Result result = new Result();
    result.foo = new Foo();
    result.bar = new Bar();
    return result;
}

public class Result{
    Foo foo;
    Bar bar;
}

My question is concrete as follows:

  1. In terms of CPU Cycles (as a relative figure), how much does the second approach consume more resources. (like 100% more)

  2. The same question in regard to memory consumption

NB (these two are questions to understand it more, its not about premature optimization)

  1. In terms of "good design practice". Do you think version 1 is an absolute No-Go or do you rather think it actually does not matter...Or would you propose never returning "object Arrays" (((in an object oriented programming language)))...

This is something, I am always wondering if I should create dedicated Objects for everything (for passing values) or I should rather use generic objects (and common method parameters...)

The question also applies to

public doSomething(Query query ) 

versus

public doSomething(Foo foo, Bar bar, Aaaa, a, Bbbbb)

thanks

Markus

like image 793
Markus Avatar asked Feb 01 '26 12:02

Markus


2 Answers

3.) In terms of "good design pratice". Do you think version 1 is an absolute No-Go or do you rather think it actually does not matter...Or would you propose never returnung "object Arrays" (((in an object oriented programming langauge/regarding encapsulation ...)))...

Version 1 is absolutely a no-go. It's almost completely untyped. The caller has to know the actual types and where they are in the array, and cast appropriately. You lose any useful compile-time type checking, and the code itself is significantly less clear.

I would never return an Object[] unless the values it contained were constructed with new Object().

like image 121
Matt Ball Avatar answered Feb 03 '26 00:02

Matt Ball


I don't believe that defining a Result class and returning that consumes any more resources at run time than constructing an Object[]. (Granted, there's a miniscule cost for storing and loading the class definition.) Do you have data that indicate otherwise?

Returning an untyped object array is poor practice for various reasons, among which are:

  1. It's prone to error.
  2. It's harder to maintain.
  3. Casting back to the "real" type is not free, either.

Regarding your other query:

public doSomething(Query query) 

versus

public doSomething(Foo foo, Bar bar)

This is less clear-cut. If packaging up a Foo and a Bar into a Query object makes sense in the problem domain, then I would definitely do it. If it's just a packaging up for the sake of minimizing the number of arguments (that is, there's no "query object" concept in your problem domain), then I would probably not do it. If it's a question of run-time performance, then the answer is (as always) to profile.

like image 40
Ted Hopp Avatar answered Feb 03 '26 01:02

Ted Hopp