Fun way to get yourself in trouble with overloading
Debugged an interesting thing the other day. Turned out to be caused by an improper use of overloading.
Chapter 1.
Developer A writes a function with two overloads:
public string GetGreeting(string FirstName, string LastName)
{
return string.Format("Hello, {0} {1}! Nice to have you back!",FirstName,LastName);
}
public string GetGreeting(string UserName, string Password, string Error)
{
return "Hello, user! Please update your profile so we can greet you by name.";
}
All good so far...
Chapter 2.
We've merged with another company. The "Merge Databases" project won't be done until next fiscal year, so we're just using company codes to indicate which company you're affiliated with. Developer B adds a company code to both overloads to add some extra functionality based on the Company Code:
public string GetGreeting(string FirstName, string LastName, string CompanyCode)
{
if (CompanyCode == "AAA")
return string.Format("Hello, {0} {1}! I can't believe we got bought out!");
else
return string.Format("Bonjour, {0} {1}!");
}
public string GetGreeting(string UserName,string Password,string Error,string CompanyCode)
{
if (CompanyCode == "AAA")
return "Hello, user! Please update your profile so we can greet you by name.";
else
return "Bonjour, utilisateur! Veuillez mettre à jour votre profil ainsi nous pouvons vous saluer de nom. Or whatever.";
}
Chapter 3.
Developer C (who will be played by Jon) has to troubleshoot why passwords are being shown in welcome messages. Several releases later. Why? Some (actually, all) old code calling
GetGreeting(UserName,Password,Error)
has been redirected to
GetGreeting(FirstName,LastName,CompanyCode)
because the signatures are the same.
This is a fun one because it'll compile just fine, and you won't know who's calling what without searching the whole project.
Notes:
(1) Developer C made up an example that will probably not compile (No Intellisense Day) and will likely be of no business value. Trust me, the pain was real.
(2) Developer C is sure there's some cool CS term for the above affect (something like Polymorphic Andorgeny or Sociopathic Signature Usurpation) that might be nice to know but wouldn't have succeeded in robbing Developer C of the fun of debugging this issue.