Beautiful code, test-first Jamis 37 comments Latest by danielle

I was talking with a friend recently and discovered we have a failing in common: we both have a tendency towards a peculiar form of perfectionism. He was telling me how he feels like he has to sit and tweak his code over and over until it not only acts right, but looks right. It cannot be merely functional, it must be beautiful, as well.

I’d never before thought about it, but I realized that the way I keep this tendency from dominating me is to write my code test-first. I constrain the behavior of my unborn code with tests, and find that it leaves me free to consider the esthetics of the solution as I work. Otherwise, I have to waste effort playing with the cotton-candy cloud of requirements, making sure the code I’m writing is satisfying those nebulous demands, and that in turn distracts from the higher-level concerns.

This has the side-effect of making me happier and more productive in my job. When the code I’m working in is ugly, or messy, I’m less inclined to do good work there. It’s like having to hang a picture in a messy room—what does it matter if the picture is a little crooked, in the midst of so much chaos? It’s demoralizing.

On the other hand, if the code is clean, concise, and attractive, I will work harder to keep the code in that state. I’m happier doing it, too. My morale remains high.

I encouraged my friend to give test-first development a try, for a month. If you haven’t tried it for at least a full month, you ought to take the challenge, too.