Skip to content

Instantly share code, notes, and snippets.

@danieleli
Last active February 13, 2016 19:24
Show Gist options
  • Save danieleli/52b315ca9cee727ba454 to your computer and use it in GitHub Desktop.
Save danieleli/52b315ca9cee727ba454 to your computer and use it in GitHub Desktop.
Extending Functions
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