Class design: should objects return the results?

snotnose

Ars Tribunus Militum
2,747
Subscriptor
Code:
public class HowTo {
    private class returnResult {
        private int value;

        returnResult(int value) {
            this.value = value;
        }

        private returnResult add(int other) {
            returnResult result = new returnResult(0);
            result.value = this.value + other;
            return result;
        }
    }

    private class internalResult {
        private int value;

        internalResult(int value) {
            this.value = value;
        }

        private void add(int other) {
            this.value += other;
        }
    }
}
Do you prefer returnResult or internalResult? When and why?
 

Haas Bioroid

Ars Scholae Palatinae
1,424
Subscriptor
returnResult : great for processing data, avoid internal side-effect, easy to use in parallel or asynchrone context. Can be a hassle to implements with class, hopefully your language has records.

internalResult : better performance (mutation does not imply copy), easier to use with stateful side effect (like GUI), the thing years of OOP paradigm has accustomed us (both a blessing and a curse).
 

ShuggyCoUk

Ars Tribunus Angusticlavius
9,975
Subscriptor++
So first off I assume it's way more complex in reality (because otherwise just use an int!)

Second note that you should declare the value in returnResult as final. It makes it crystal clear and potentially allows the compiler and JIT to do much better.

So assuming the concept merits a type (because in java types have quite a cost on readability, as well as runtime evaluation etc) It depends how it is used.
If it's genuinely private (so extremely limited scope) then, if it's never used in a multi threaded context then the mutable version will be significantly faster, and IMO easier to debug.
If it's in a multi threading use the immutable one.
 

Haas Bioroid

Ars Scholae Palatinae
1,424
Subscriptor
I'm learning OO and I've seen classes designed both ways, each has their strength/weaknesses. I'm curious what the groupthink on the issue is.

I'm also learning Kotlin, you'd be amazed how long it took me to get that sample Java code to compile. I never before noticed how easy it is to miss a missing semicolon.

Take note there is a record type in recent versions of Java that is the fitting type for implementing returnResult.
 

bjn

Ars Praefectus
3,217
Subscriptor++
It depends on context. I tend to operate on immutable objects if there is any degree of sharing of those objects by different things. It makes reasoning about the object's state much easier. I don't get surprised by an object being changed by another chunk of code. If an object is unshared, I'll tend to make it mutable and allow state to change. You could possibly do both with your object and have 2 functions calls and choose the appropriate one depending on the context. I'm not a Java dev, but with my C++ hat on it might look something like...

C++:
class Thing {
public :
  Thing(int value) : value_(value) {}

  /// immutable add, equivalent to x = a + b
  auto add(const Thing &other) const -> Thing
  {
    return Thing(value_ + other.value_);
  }

  /// mutable add, equivalent to x += b
  auto addToSelf(const Thing &other) -> Thing &
  {
    value_ += other.value_;
    return *this;
  }

protected :
  int value_;
};
 

snotnose

Ars Tribunus Militum
2,747
Subscriptor
It depends on context. I tend to operate on immutable objects if there is any degree of sharing of those objects by different things. It makes reasoning about the object's state much easier. I don't get surprised by an object being changed by another chunk of code.
You just put your finger on why internalResult just feels wrong to me somehow.
 
  • Like
Reactions: bjn