Tuesday, May 03, 2011

Programming while stupid

A significant benefit of test-driven development (TDD) is that you don't need to work very hard. Now turn that idea around: when you cannot work at your full capacity, you can still be productive and produce well designed, working code.

TDD reduces cognitive load by encouraging small steps and by capturing the precise requirements for a piece of code in the test's assertions. Typically, that allows us to devote more of our energy to other important matters like clear design, intention-revealing code and delivering value to the customer.

But what if you're tired? What if you're just not at your best? It happens; we're human! Sleep deprivation makes us stupid. So does stress.

How are we supposed to program while stupid?

Maybe you have a teething baby. Maybe you got into an accident on the way to work. Maybe the office is absolute Bedlam today. Maybe you're hung over. (Note: I have never experienced this latter phenomenon, though I believe I may have read about it in books.)

The rhythm and structure of TDD provide enough support that you can still be productive. Granted, those "extra cycles" will not be available to perform the higher level functions we mentioned, such as keeping an eye on the broader design of the code. But at least you'll be able to function. You will be able to program - without embarrassing yourself.

Contrast this with situations in which intense focus and concentration are an absolute requirement for producing any code at all.

Before I learned to TDD, I would keep large chunks of infrastructure in my head. Needless to say, such a volatile storage mechanism had to be treated with great care. An overheard conversation or a phone call could cause enough of a distraction that I would lose track of what I was trying so desperately to keep aloft. I'd get frustrated and angry. Back in my wilder youth, I'd often call out loudly for peace and QUIET!!

And what about the morning after a colicky baby all-nighter? You've made it in to work. You want to sling some code. You have a moral and ethical obligation to do something useful to earn your pay. But keep a complex design, a lofty edifice, a convoluted algorithm in what remains of your battered brain? Not gonna happen.

So don't even try. Take baby steps, write the asserts first, be modest and methodical and your work-ethic conscience is assuaged.

I still don't enjoy distractions, and having learned a lot more about neuropsychology over the decades, I decry the misplaced affinity for multitasking that some people seem to think is a necessary part of modern life. But I can happily walk away from a red test, knowing that I can pick up at any time and, by continuing to take baby steps, finish what I was doing.

Interruptions are more tolerable with TDD. A trance-like focus is no longer a basic prerequisite for programming.

And TDD cures hangovers.


Javin Paul said...

Inspirational article :) ,one of the best I have read on TDD advocacy and convincingly you convince me.

Why String is immutable in Java

Anonymous said...

Or you can just turn-off your mobile and put on some noise-cancelling headphones.

jdtangney said...

Thanks for that suggestion, Anonymous. That's exactly what I used to do 15 years ago (though I didn't have a mobile phone back then.)

Unfortunately, this doesn't work for pair programming. It also doesn't work when the cause of your slowdown is internal rather than external interruptions.

Arne Riemann said...

Nice article, hope in future we can use unit tests :-) Hard to use in old crappy code

Anonymous said...

i just don't program any more and collect a management salary

Ben said...

nice post. we tend to overlook our own ability to be stupid, and it definitely needs to be taken into consideration.

Anonymous said...

TDD is a religion adopted by lesser skilled programmers. It mainly serves to waste one's time.

Anonymous said...

Take notes? Jot down stuff, sketch schematics ? Use a whiteboard?

I advocate the utility of TDD for various reasons, but reduction in cognitive load is not one of them.

Myself, I said...

alternate title: another day in the life of an html developer

jdtangney said...

@Arne Actually, there are ways to bring crappy, legacy code under test. It's quite an art, but it's possible. See http://ow.ly/4P92R

@Anonymous #1 If TDD is good for less skilled programmers, surely it can make a more skilled programer into a master, no?

Post a Comment