Motley says: "I don't have time to Refactor - I need to get it right the first time"

 

Summary

 

Motley: Refactoring takes too much time - I have to ship you know!

 

Maven: Refactoring leads to much more maintainable code, and if you have tests in place, the return on investment far outweighs the cost.

______________________________

 

[Context: Motley has just embarked on his first adventures with TDD, but Maven wants to check-in to ensure Motley really understands the process]

 

Maven: So how are things going with TDD, Motley?

 

Motley: I've only been at this for a couple of hours - why are you bugging me already?

 

Maven: It's good to start off on the right path. Just want to make sure you understand the TDD process.

 

Motley: Yeah, yeah, yeah. Write a failing test. Write code to pass it. Move on to the next requirement.

 

Maven: Whoa there, Nelly! You forgot one of the most important steps - refactoring.

 

Motley: Not really sure what you mean by that. If I have syntax errors I fix them. Duh.

 

Maven: Refactoring is all about improving the design of your code without changing any existing functionality. You mentioned previously that one of your values is writing great software. Well, refactoring is not just about writing it great the first time, it's about being realistic with yourself that you won't get it right the first time. You make the necessary changes to prevent it from starting just OK and turning into a big turd later as you maintain it. Obviously it takes more time than just writing the code once, but the pay-off down the road is huge.

 

Motley: You've been sniffing too much glue, Maven. I barely have enough time to write my code once let alone keep redoing it over and over. I have to ship. If I don't ship, the business suffers, and most important, I get fired, and I suffer.

 

Maven: What about when someone has to maintain that code moving forward? Do you want to make it difficult for them? How much do you think that costs the business?

 

Motley: Like everything in life, there is a trade-off. I have to do my best given that one shot is all I get. I vote ship and deal with maintenance issues later.

 

Maven: That's what has gotten us into all this trouble with our legacy code! How do you like fixing bugs in all that old stuff?

 

Motley: Well… it sucks. Our legacy code is a "turd" - as you so eloquently put it.

 

Maven: Exactly. We need to make it easier for the next person to maintain. Refactoring is a way to get us there. Write your code, improve it, and then move forward with the next requirement. It's actually quite natural once you get in the rhythm.

 

Motley: But I'm afraid that once I have it working that if I make changes I'll break something.

 

Maven: Ah, but you're doing TDD, right? That means you have tests in place so that if you do happen to break something you know about it right away. It's quite beautiful, really. Give it a shot and I'll check in with you later.

 

Motley: Why do I keep letting you do this to me? Fine, I'll try it. But if I'm late on my deliverables, it's your paycheck. Deal?

 

Maven: I'm so confident that you're going to rock that yeah, I'll take that bet.

______________________________

 

Maven's Pointer: Many times the primary reason that developers are afraid to refactor isn't because they don't have the time, but because they don't have the confidence to make a change without breaking existing behavior. Having unit tests in place increases that confidence and makes refactoring easier and fun. Avoid refactoring without first having tests in place!

 

Maven's Resources:  Refactoring: Improving the Design of Existing Code, by Martin Fowler et al, Addison-Wesley Professional, ISBN: 0201485672, 1999.