Tuesday, April 29, 2008

Fixing Code

I've touched a nerve and it seems to be a pretty raw nerve with a lot of people.  Fixing code.  Here's what one person said the developers need to do when fixing their code:

  • take the time to understand the bigger picture and the context of the codes’ function
  • take the time to understand the impacts of what they’re doing, even just commenting out a line
  • rewrite their code rather than creating more spaghetti code

So, this person seems to want programmers to understand what they are doing, the impact of what they are doing and to do it correctly (no spaghetti code).  When you go to a mechanic to have him fix a problem with you car, you expect him to know what he is doing, to understand the impact of the change and to fix the problem correctly.  If you go to a doctor for an operation, you expect him to know what he is doing, to understand the impact of the operation and to do the operation correctly.

So why is it that programmers, developers, whatever you want to call them, why is it that they continually make silly changes to the code, make things more complicated and just don't seem to care to do things correctly.  For instance, in a recent project recently we observed the following code (things have been changed to protect the guilty, but the same concept is there):

IF anotherField > 3 THEN year = year +3

IF anotherField <= 3 THEN year = year + 3

These lines were back to back in the code.  The net result is that regardless of the value of anotherField, year is going to be incremented by 3.  There may have been some different logic in there previously, by whoever made the change did not bother to look up and down the code for a few lines, the immediately preceding line in this case, to see what was going on.  As it is the code is longer than necessary, takes longer to run than necessary, is more complicated then necessary, is confusing to the casual reader and just needs to be rewritten.

Yes, sometimes to fix a problem you only need to change a single line.  But the better approach may be to change half a dozen lines, or more, so that the next person finds the code cleaner, simpler and better.

No comments: