Note that there are some explanatory texts on larger screens.

plurals
  1. PODRY without having single code?
    primarykey
    data
    text
    <p>i want to not repeat myself (DRY), but i cannot have a single piece of code. For example here is code repeated 3 times with the same bug:</p> <pre><code>class StarWars : Movie { //Calculate "base ^ exponent" public float Power(float base, float exponent) { return (base * exponent); } } class Customer: Object { //Calculate "base ^ exponent" public float Exponential(float base, float exponent) { return (base ^ exponent); } } class Student: Person { //Calculate "base ^ exponent" public float CalculateExpoential(float base, float exponent) { return CalculateExponential(2.7182818, exponent * Ln(base)); } } </code></pre> <p>Now ideally i would have extracted this common function into it's own helper somewhere:</p> <pre><code>class LibraryOfHelperCode { public static float Exponentiation(float base, float exponent) { return Exp(2.71828183, base * Ln(exponent)); } } </code></pre> <p>And converted the existing code to use it:</p> <pre><code>class StarWars : Movie { //Calculate "base ^ exponent" public float Power(float base, float exponent) { return LibraryOfHelperCode.Exponentiation(base, exponent); } } class Customer: Object { //Calculate "base ^ exponent" public float Exponential(float base, float exponent) { return LibraryOfHelperCode.Exponentiation(base, exponent); } } class Student: Person { //Calculate "base ^ exponent" public float CalculateExpoential(float base, float exponent) { return LibraryOfHelperCode.Exponentiation(base, exponent); } } </code></pre> <p>The value is that now i've extracted the repeated code from </p> <ul> <li>Power</li> <li>Exponential</li> <li>CalculateExpoential</li> </ul> <p>into a single function. This means that if there are any bugs, they only have to be fixed once. Which is good in this case, because there <strong>is</strong> a bug:</p> <pre><code> public float CalculateExpoential(float base, float exponent) { //19971012: Oops, should be natrual log of base, not exponent return Exp(2.71828183, exponent * Ln(base)); } </code></pre> <p>and a few years after that:</p> <pre><code> public float CalculateExpoential(float base, float exponent) { //19990321: Oops, need to handle when exponent is zero if (exponent == 0) return 1.0; //19971012: Oops, should be natrual log of base, not exponent return Exp(2.71828183, exponent * Ln(base)); } </code></pre> <p>and later on:</p> <pre><code> public float CalculateExpoential(float base, float exponent) { //19990321: Oops, need to handle when exponent is zero if (exponent == 0) return 1.0; //20040523: Another special case if (Base = 0.0) &amp;&amp; (Exponent &gt; 0.0) then return 0.0; // 0**n = 0, n &gt; 0 //19971012: Oops, should be natrual log of base, not exponent return Exp(2.71828183, exponent * Ln(base)); } </code></pre> <p>and finally:</p> <pre><code> public float CalculateExpoential(float base, float exponent) { //20101027: Microsoft just release a method in .NET framework 4.0 that does //what we need. Use it: return Math.Pow(base, exponent); } </code></pre> <p>And everyone gets the fixes. On the other hand, i cannot guarantee that any one of those increment fixes won't break existing code. </p> <p><em>Imagine</em> a guy was calling:</p> <pre><code>char ps = Math.Trunc(Exponential(ProblemSize, ProblemComplexity)); </code></pre> <p>and was never expecting the value to be larger than 128. He was wrong. And while the code was <strong>wrong</strong> all this time: it <strong>happened</strong> to work.</p> <p>Now i come along and <em>fix</em> things, and suddenly code is crashing due to overflow and/or wraparound.</p> <hr> <p>The problem i'm facing today is that a change in DRY common code affects everywhere it's used. The only acceptable (polotical) solution is to keep a copy of the library class for every executable/moduble/namespace/class that uses it.</p> <p>Undoing any DRYness.</p> <p>Is there any way out of this mess? When i can not repeat myself, but continue to get fixes and improvements as they are added to the single DRY code?</p> <hr> <p>i mean...should i have shared code, but branch it at every release? But the issue that is polotically nobody wants the code every to be reverse-integrated.</p>
    singulars
    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