Thinking, Discipline and Courage...

Wayne Allen asks why so much code is so bad? Why the majority of code, even new code, that he sees as a consultant is terrible.

Meanwhile, Rory Blyth is complaining about “Paper bag” code.

I think Wayne hits the nail on the head with his comment about much of the problem being down to the individuals involved willingness to learn and change. But there’s more. Doing a good job requires lots of thinking, lots of self-discipline and lots of courage. Finding programmers who can do all of that and program in the required language is hard…

To many in the industry bad code is just “how it is”. Many of the teams I work with have a “that’s the way it’s always been” view of the problems that they face day to day. This is the easy way out. If you can convince yourself that it’s always been like this and there’s no reason to do things differently then you can avoid lots of hard questions.

For some programmers “it’s just a job”. Often they don’t actually care about what they do, they’re not really that interested; they come in, they do stuff, they go home, they get paid. Often many of the people who are responsible for what these programmers produce couldn’t tell good code from bad and don’t much care about the difference. Together these people churn out reams and reams of tangled and complicated read only code, often without thinking at all about the longer term. Often without thinking at all.

As long as the code solves today’s problem that’s fine. Hit today’s deadline with a quick hacky fix and move on. Forget about the code until someone requests a change which, for some strange reason, seems harder to make than it should be. These people often aren’t interested in learning and improving; that’s hard, they avoid it. This amazes me, but then I’m always trying to learn new ways of doing things; new ways that make what I produce better and easier. I’m lazy, I like my day to be a series of easy decisions the more I learn and improve the easier my day becomes… I often find that the more I think the less code I actually need to write. The more I think the easier it is to write the code I do need to write.

I think a lot of it boils down to self discipline. Software is such an amazingly malleable thing that you can build structures of incredible complexity and fragility and yet they can still work. It’s actually easier to build something really complicated than it is to build something really simple. Complexity is easy; write some code without thinking too much to do X. Now integrate that with some code that does Y. Now change it to do Z and X2 and then twist it a little bit so that it does something that you didn’t think about at the start. Leave this code for a couple of days and come back to it and it’s almost guaranteed to have become a rotten tangle of misunderstandings. Forcing yourself to search for the simplest design takes discipline. It’s easy to do the complex thing; it’s tempting to do things the “easiest” way. It’s hard and sometimes painful and embarrassing to be critical of the work you’re doing but if you’re not then you’ll never grow.

Development also takes courage. Developers need to stand up to managers who don’t understand why it is worth spending time on things like testing and refactoring. Yes men are dangerous, we need more why? men.

So, why is so much code so bad? Well, for starters, it’s hard to write good code. It’s a skill that takes time to learn and time and discipline to apply and lots of thought. Also it’s hard to know if the code you’ve written is good until you come back to it and find that it’s not; when you’re relaxing in the afterglow of hitting the deadline all code looks wonderful. It’s only when you look at the code the morning after that you start to wonder what was on your mind… Code rots. It takes work and discipline to ensure that code is kept clean and fresh and safe. All of this is a hard and ongoing job that requires lots of thinking and lots of hard choices. The sad thing is that many developers just don’t care enough about what they do to write good code and the people they report to don’t know or care about the differences. In such an environment it’s hardly surprising that so much code sucks.