Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>My recommendation would be to write some simple code-generation routines to generate something close to the code you want and copy it to the clipboard. Then paste it into your program and make whatever little tweaks are required.</p> <p>Having to write large amounts of boilerplate code usually implies a design deficiency either in what you're doing or in the language/framework you're using. Depending upon exactly what you're doing, the fault could be in either the former or the latter.</p> <p>There are situations where large structures are appropriate; if each variable of some type is supposed to encapsulate a fixed collection of independent values, an exposed-field structure expresses that perfectly. Until things start getting so big as to create a risk of stack overflow, the factors which favor using a 4-field structures over a 4-field class will be even more significant with a 20-field structure versus a 20-field class.</p> <p>There are some definite differences between programming with structures versus programming with classes. If one uses immutable classes, generating from a class instance a new instance which is identical except for a few fields is difficult. If one uses mutable classes, it can be difficult to ensure that every variable encapsulates its own set of independent values. Suppose one has a <code>List&lt;classWithPropertyX&gt; myList</code>, and <code>myList[0]</code> holds an instance where <code>X</code> is 3. One wishes to have <code>myList[0]</code> hold an instance where <code>X</code> is 4, but not affect the value of the <code>X</code> property associated with any other variable or storage location of type <code>classWithPropertyX</code>.</p> <p>It's possible that the proper approach is</p> <pre><code>myList[0].X = 4; </code></pre> <p>but that could have unwanted side-effects. Perhaps one needs to use</p> <pre><code>myList[0] = myList[0].WithX(4); </code></pre> <p>or maybe</p> <pre><code>var temp = myList[0]; myList[0] = new classWithPropertyX(temp.this, 4, temp.that, temp.theOther, etc.); </code></pre> <p>One may have to examine a lot of code to ascertain with certainty which technique is appropriate. By contrast, if one has a <code>List&lt;structWithExposedFieldX&gt; myList</code> the proper code is:</p> <pre><code>var temp = myList[0]; temp.X = 4; myList[0] = temp; </code></pre> <p>The only information one needs to know that's the correct approach is the fact that <code>structWithExposedFieldX</code> is a struct with an exposed public field <code>X</code>.</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