Monday, May 24, 2010

Writing good code is ALWAYS faster than writing bad code.

"No matter who. No matter what. No matter when. Short term. Long term. Any term. Writing good code is ALWAYS faster than writing bad code." - Uncle Bob

That was a recent tweet by Robert C. Martin who has been doing software development for more than 30 years and has written many books on the subject.

I couldn't agree more. In one of the projects I was involved in, I encountered some code that developers rushed out the door quickly in two weeks without writing any tests or paying attention to how clean the code is because they felt pressured by the deadline. Needless to say, it had many bugs, so it took much longer to fix all of them and truly deliver the feature than the originally planned two weeks.

The real irony of the matter though is I believe it would have taken no more than the two weeks originally planned if tests were written first. That is because without test-driven design, the code was bloated and much more complex than needed. When I later covered the code with tests and performed refactorings, I discovered that the original code was more than double or even triple the amount of code needed with a simple clean design. In other words, it took longer to write the code without tests, and that is not counting the extra time spent tracing and fixing bugs.

Instead, developers had to get embarrassed by the large number of bugs reported, spend a long time tediously tracing through complex code to find the bugs, scramble to fix the bugs quickly, and be delayed by a few weeks in truly delivering the feature. It makes me wonder what gain they got out of not test-driving clean code in the first place given that it was an Agile environment where everybody was already aware of the practice.

I bet it was the convenience of not having to change their ways or ignorance to the true practical benefits of test-driven development. And, I have to admit, during my learning phase of test-driven development, it took me a while of swinging between doing it and abandoning it before I started seeing the futility of not doing it and truly appreciating how faster I got in developing software that way.

To recap, I wholeheartedly agree with Uncle Bob. Writing good code is ALWAYS faster than writing bad code.

No comments: