Archive for February, 2009
Working with Legacy Code
I give special attention to code readability and good design when I write programs, these are two metrics that I care about. I like the idea that the team members can read code and understand what the code does.
Today I spent two hours to find a silly bug. I dig with the debugger until the code near the bug, and in that code I have this instruction:
collection.Add(currentKey, newValue);
The name of the method talk for it, the parameter are clear and so I didn’t step into the method to view what happens thinking that the error was in another location. Then after 1,5 hour of search without result I decided to step into the Add method and this is what I found:
public void Add(String key, MyObject value)
{
if (value == null)
{
Remove(key);
}
else
{
if (ContainsKey(key))
{
this[key] = value;
}
else
{
_base.Add(key, value);
}
}
}
The big problem here is that Remove that live in an Add method!
This paradoxical situation create a big headhake, why an Add method should remove one element from the collection? If you decide to call it Add there will be a reason, why?
It is one of most important things to remember when you write code: the name of a method must meet it’s implementation! It’s simple but very valuable!
RSS


