Note that there are some explanatory texts on larger screens.

plurals
  1. POConstructor with all class properties or default constructor with setters?
    primarykey
    data
    text
    <p>Following are the two approaches:</p> <ul> <li>constructor with all the class properties</li> </ul> <p>Pros: I have to put an exact number of types of parameters so if I make an error the compiler warns me (by the way, is there a way to prevent the problem of having erroneously switched two Integer on the parameter list?) </p> <p>Cons: if I have lots of properties the instantiation line can become really long and it could span over two or more lines</p> <ul> <li>setters and the default empty constructor</li> </ul> <p>Pros: I can clearly see what I'm setting, so if I'm doing something wrong I can pinpoint it as soon as I'm typing it (I can't make the previuos error of switching two variables of the same type)</p> <p>Cons: the instantiation of an object with lots of properties could take several lines (don't know if this is really a con) and if I forget to set a property the compiler doesn't say anything.</p> <p>What will you do and why? Do you know of any light pattern (consider that it should be used everytime an object wth 7+ properties is instantiated) to suggest? I'm asking this because I tend to dislike large constructors where I can't figure out fast where is the variable I'm looking for, on the other hand I find the "set all properties" vulnerable to missing some of the properties.</p> <p>Feel free to argument my assumptions in pros and cons as they are only mine thoughts :)</p> <p>Update - a question I've found which is related to this: <a href="https://stackoverflow.com/questions/2848938/building-big-immutable-objects-without-using-constructors-having-long-parameter">Building big, immutable objects without using constructors having long parameter lists</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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