Should he be a member of the class?

The class implements a public function that evaluates some value. During the calculation, the function creates temporary data, which I also want to get.

I could either add a member of the class that stores this data, which I can get after the function completes, or give the function another parameter so that the caller needs to take care of this. So this is 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 do not look right. The temp data doesnโ€™t really belong to class A and functions with an extra parameter in class B, I donโ€™t know ...

Is there any other solution? If not, which of the two will you choose and why?

+4
source share
2 answers

It looks like you are struggling with a function problem with multiple outputs. The best solution is to create a separate class that represents the return value of this function and contains the objects Output and TempData. Then you can purely return an instance of this class.

+10
source

It is really difficult, if not impossible, to say from that. You must ask yourself what this object is.

So is there a โ€œTempDataโ€ feature of your class A? If you have object A, do you expect it to be able to tell you what "TempData" is? Then it should be like a member. This is probably more related to OO design and then to your current situation, especially with classes called "A"

0
source

All Articles