Note that there are some explanatory texts on larger screens.

plurals
  1. POWhere might I find a concise, well-written argument against this initialization anti-pattern, and possibly others?
    primarykey
    data
    text
    <p>A coworker of mine tends to design classes like:</p> <pre><code>class ImportantClass { public SomeClass ImportantField; public SomeOtherClass OtherImportantField; // etc. } </code></pre> <p>He'll write no constructor. Then wherever he instantiates this type his code will look like this:</p> <pre><code>ImportantClass x; try { // This is problematic enough to begin with, as every field (which is public) // is set manually, which causes code duplication wherever this type is // instantiated as well as maintenance problems in the event new fields are // introduced or internal implementation is changed. x = new ImportantClass(); x.ImportantField = new SomeClass(); x.OtherImportantField = new SomeOtherClass(); } catch (Exception ex) { // ...what's even worse: now x can easily be in a totally invalid state, // with some fields initialized and some not. HandleAnyException(ex); } </code></pre> <p>Obviously the above is an oversimplified (and exaggerated) example; but I think you get the point.</p> <p>I'm not posting this to criticize my coworker; rather, I want to get across to him the many problems in developing classes like this and point him in the right direction. I personally feel that this design, while quite poor, is something that is easily "fixed."</p> <p>My main concern is that <strong>if I want to convince him, it will be important for me to have an argument that is strong (well-supported), easy to understand, and concise</strong> (he's not going to want to read a 2-page e-mail telling him why I think his code needs to change).</p> <p>Are there any good resources out there that provide such well-crafted arguments, addressing the drawbacks of this and perhaps other ill-advised design anti-patterns? Something like a compendium of "common software design mistakes" would be extremely helpful, if it exists.</p> <p><strong>Note</strong>: I realize that simply talking about it is the most direct and probably least disruptive way to give advice; I'm just thinking, it would be nice to also have this resource that I can point to as a reference—again, if such a resource exists.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
    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