Last active
February 13, 2016 19:24
-
-
Save danieleli/52b315ca9cee727ba454 to your computer and use it in GitHub Desktop.
Extending Functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Original | |
{ | |
public string OldFunction(string s1, string s2) | |
{ | |
return s1 + s2; | |
} | |
} | |
public class RobertsSolution | |
{ | |
// this potentially breaks old code unless new params are optional. | |
// but raises concerns around ensuring validity of inputs. | |
public string OldFunction(string s1, string s2, string newParam) | |
{ | |
// this increases code complexity for all code paths | |
// function gets longer and longer and longer and longer | |
if (newParam != null) | |
{ | |
s2 = newParam; | |
} | |
return s1 + s2; | |
} | |
} | |
public class DansSolution | |
{ | |
public string OldFunction(string s1, string s2) | |
{ | |
return s1 + s2; | |
} | |
// this increases code complexity only for new code path | |
// plus, the increase in complexity is smaller because there is no branching | |
public string NewFunction(string newParam) | |
{ | |
string s1 = null; // default value | |
return OldFunction(s1, newParam); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment