Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should this be a class member?

A class implements a public function that calculates some value. During the calculation the function creates temp data that I also want to retrieve.

I could either add a class member that stores that data which I can get after the function has finished, or give the function another parameter so that the callee has to take care of it. So it's basically

class A {
    TempData member;
    ...
    public Output function(Input input) {
        // calculate return value and save temp data to member
    }
}

vs.

class B {
    ...
    public Output function(Input input, TempData fillme) {
        // calculate return value and save temp data to fillme
    }
}

My problem: both versions look wrong to me. The temp data doesn't really belong to class A and the function with the additional parameter in class B, I don't know...

Is there another solution? If not, which one of those two would you choose and why?

like image 624
javabeginner14 Avatar asked Dec 28 '22 22:12

javabeginner14


1 Answers

It sounds like you are struggling with a multi-output function problem. The best solution is to create a separate class that represents the return value of this function and holds your Output and TempData objects. Then you can cleanly return an instance of this class.

like image 87
Konstantin Komissarchik Avatar answered Jan 08 '23 18:01

Konstantin Komissarchik