Note that there are some explanatory texts on larger screens.

plurals
  1. POShould I have two different variables in one class that represent almost the same thing
    text
    copied!<p>At first I had a class A that took a list B as constructor argument. This list B was kept within class A in a variable. Another class that used this class A needed the data in list B, but in a different way. So I created a specific object C and had a getSomething() method in class A that on the fly created one of these object C, based on information in the original list B. This worked fine.</p> <p>Then it turned out that the class that called getSomething() needed a little bit extra information that the original list B did not contain. A small calculation on the specific elements in list B however gave me that information. That information needed to be added to the object C. This calculation is a bit heavy so I do not want to do it on the fly when I create object C in getSomething(). This gave me a problem since I cannot update any elements of object C with this calculated information, since the elements of object C do not exist yet, they are created in getSomething().</p> <p>How should I solve this the best way. Are there perhaps any patterns?</p> <ul> <li><p>Do I in the constructor of class A convert the elements in list B to a list of object C and then discard list B, and then perform the calculation and updates all elements of object C?</p></li> <li><p>Do I keep both the original list B and a list of object C and have two variables with almost the same information, just presented in a different way?</p></li> <li><p>Or something else?</p></li> </ul> <p>Edit: C is not an extension of an element in list B, its more that one or more elements in list B is combined to a C.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload