Note that there are some explanatory texts on larger screens.

plurals
  1. POMemory and code balance
    text
    copied!<p>I know this is an old topic, but during my programming I always encounter such a problem: how to balance the memory usage and code size?</p> <p>Here is an example:</p> <p>I want to implement a class (in C#), and I want the constructor(s) to accept a wild range of different types of parameters. So I would need a constructor to accept an IEnumerable object (this includes array, lists etc.), but I would also need a consstructor to accept variable length parameters (using <strong>params</strong>). The internal construction routine of these two constructors are exactly the same, but in order to allow variable length parameters, I need the latter one still.</p> <pre><code>Foo(IEnumerable&lt;Bar&gt; a) {/*construction*/} Foo(params Bar[] a) {/*the same construction like above*/} </code></pre> <p>I could however do a conversion of the parameter array of the one using the <strong>params</strong> and call the the other constructor:</p> <pre><code>Foo(params Bar[] a) : this(new List&lt;Bar&gt;(a)) {/*here empty*/} </code></pre> <p>While using this manner the code size is reduced, but it introduces a temporary list object, hence increases the memory usage (although also temporary).</p> <p>I know there would be a great discussion of this example, about which one is better in which way. But I would like to know, how would you all fellows deal with this kind of balance problem in general. In many cases the situation will be more complicated, and sometimes you need to also consider the execution time balance. </p> <p>There is a principle called "Don't repeat yourself", could this also apply to small piece of code like the example here? </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