About Writing Clean Code

Posted by & filed under , .

iStock_000008980370XSmallEvery now and then I feel any developer should sit down and reflect upon some of the experiences from the past. I feel this is healthy: once you allow enough time to pass, you can go back and revisit good and bad (technical and less technical) experiences from the past and learn from them. The good experiences will teach you what to keep doing. The bad experiences will teach you what to avoid, and that is very very useful knowledge. (As a side note, it is said that that good judgement comes from experience, and experience comes from bad judgement — so don’t shy away from failures, they provide a lot of learning!)

Based on my own advice above, I do try to do this myself rather often. And changes in my life provide a good point often to sit down and do this. Right now I have just joined Netflix and the focus and tempo of my work has changed again so this is a good point in my life, I feel, to do so.

And as with a lot of times when I sit down and think, there’s a blog post to accompany my trail of thoughts 🙂 This particular time I’m going to talk about writing clean code.

Here’s the thing: most techies you will encounter read (and some write) material about why clean code is important and that we MUST, MUST, MUST do it! And here’s the other thing: a lot of these guys prefer often just advocating it rather than doing it!

I have seen projects where you open up the code and it’s a spaghetti-meatballs situation: magic numbers, non-OOP code where everything is thrown into one big method, inconsideration for the poor garbage collector and so on. And if you raise your eyebrows and ask a question about this you normally get this answer: “Oh, yes, I know that code is messy but it doesn’t matter, it only runs once a month and we’ll get rid of that project in a couple of months.”

Now some of you will be jumping up and down to remind me and everyone else that a lot of times that doesn’t happen and that becomes legacy code and everyone has to support all the rubbish code from here to eternity, I know. And I’m sure there are cases where (sadly!) such projects remain forever. But those are not the cases I care about — though, you are absolutely right, that is a reason enough to not write messy code!

I am going to assume for the purpose of this post that all these guys are right: that the code indeed only runs once a month and will be phased out in 2 months — so for the purpose of running this code twice more it wasn’t worth the effort of writing the code “properly”. However, even given these circumstances I still advocate writing clean code and I totally believe it’s actually worth the effort!

You see, I think coding activities are similar to such other activities as maths, reading, writing and (why not?) even running marathons: the more you do it the better you get at it! Think back in school when I started reading and writing, I used to have to read each word letter by letter, sound it out and used to take me ages to go through a paragraph. Constant repetition got my brain trained to recognize not letter by letter, but group letters together and make a syllable, then group these in words and so on to the point where I can now read entire pages in seconds. Same for writing. And the same for maths: the more I practiced my times table the better I got at it.

You can argue that reading all the 1st grade stuff was not worth the effort, however, reading “Love in the time of cholera” definitely was. I could have of course read Gabriel Garcia Marquez’s novel without going through all the preparation in primary school — but it would have taken me ages to do so! However, because I did practice my reading all the years prior to that, I was able to read this very smoothly and very quickly and actually have an enjoyable experience throughout doing so.

If you think for instance of all the marathon runners, they train every day, and part of their training they often run 5 miles or 10 miles only in one day. One would argue that a 10 mile run does not justify their effort really as it’s training first of all and secondly it’s not a full marathon. However, it’s those 10 miles sessions daily that shapes them to be marathon runners — and as such, when the Olympics arrive, they are ready to break another record.

Getting back to programming, I see the same case here: sure, there are projects out there for which we have to cobble up some code and run it once or twice until a proper solution gets rolled out. While those projects are never going to get any attention from users, indeed, however, writing the code for those apps cleanly can shape you into a better coder — so when those skills matter and you need to apply them in a “proper” project, you will find that you have been keeping them sharp and you they will come to you naturally. From there on, the clean code will flow in your “proper” project naturally, without you having to think about it, thus saving you brain bandwidth to dedicate to things like implementing the business logic, UI and so on.

If you get used to using an Iterator when dealing with List‘s in Java for instance, even though you are dealing with instances of ArrayList and as such it might seem natural to use an index-based approach, it will pay off when you are finally using a LinkedList instance. (Remember that accessing items in a LinkedList by their index is an O(n) operation!). If you get in the habit of doing something like:

try {
   resource = acquire();
   resource.use();
} finally {
   resource.close();
}

Then when you finally end up dealing with things like database pools that will help with preventing suffocating your pool with dead connections.

And the list goes on…

Bottom line: I think all code written is important, even if the project you are writing the code for isn’t. Put effort in it and try to write clean code — it will keep you in shape and pay off when it comes to writing a “proper” project.

Leave a Reply

Your email address will not be published.