Thursday, April 28, 2011

Reading and Understanding, but not Writing, equals Wasting

Take a look at this very simple code snippet:


It is taken out of context of course, but the intent should be clear:

  • create an image 
  • place it within a cell 
  • make sure it vertically in the middle of the same cell
  • and all the way to the right

If the code does this right or wrong is not what I want to discuss. But hopefully it did not take you much time to understand the intent of the code.

Now, take a look at this snippet, which should express the same intent.
It should do the same thing. (Does it?)


Yes! It does to the same thing!
But I'm willing to bet that it took you less time to understand the first version.
Given I am very slow when reading code, my guess is that it would take me 15-20 seconds longer to understand the intent of the second version. (Maybe a few more extra seconds if I loose focus and double check that the algorithm was implemented correctly.)

This is of course very little extra time. Or is it?
Let's say three or four persons spend an extra 20 seconds on this very trivial piece of code. And if you think about what your projects are really about, the actual problems you are trying to solve, these kind of trivial code snippets are all over the place. Those seconds will add up.
Not to mention the risk of losing your trail of thought while you double check the algorithm when you are really chasing down a production bug.

- "Ok, so just refactor it!", you say.

- Yes! That's exactly what I should do. Change the second snippet so it looks like the first snippet!

I'm slow coder too so it might take me 60 second to do it (in addition to the extra 20 I already spent understanding it in the first place).

- But wait! If I make the change and make a mistake while copy-pasting, it might break. I need to run it before I check it in!

(And this is where the trouble starts.)
The code that I want to change is in a branch of an if-statement. The branch executes when a regular user logs in and that user doesn't have an email address registered.

- I only have a super user test account. I could register a new regular user account. That will take me 2 minutes, if the email server sending confirmation mails for the test environment is up....
- Maybe I should ask Bill if he has an account I could use....but he looks busy. Don't want to disturb him with such a trivial thing.

- Ah, never mind. I'll just leave the code the way it is. It's not that complicated. Too bad we never wrote those unit tests...

No comments:

Post a Comment