Friday 4 November 2016

I'm a programmer and code makes me uncomfortable

Well, BAD code makes me uncomfortable.

When I start to write a piece of code, I tend to focus on the functional part of the problem. To just actually solve the problem at hand. That is simply step 1 of development. This is also the most important step. When you write code that doesn't perform the function it is supposed to, then it is literally worthless. Less than worthless actually.

So, now I have a piece of code. Chances are, I am not happy with it. It really makes me uncomfortable. There might be some ugly/non-descriptive variable names, some large methods, incoherent classes. It itches. My stomach turns a little. Things have to be simple, the code should flow naturally. The amount of WTFs should be low. I am not intelligent enough that I can afford having complex code. If it was hard to write, it will be infinitely harder to read later on when I have to track down some obscure bug.

So I start improving the code. Split out a method to simplify a complex piece of code. Improve those variable and method names. Sometimes I remove good chunks of code only to be replaced by some more elegant and simpler piece. Maybe there is a piece of existing code that needs some love. Sometimes there is an ugly piece of code that won't budge. It is the nature of the beast. In such cases I try to be as explicit as possible documenting the piece. I usually try to write as little documentation as possible. I find that when code is well written, it is self explanatory.

Sometimes it is really the design that makes me uncomfortable and I have to refactor large chunks of code. I won't hesitate to do it immediately. It will be worth it in the long run.

I keep doing this until I am happy with the overall quality. The design is right, methods are not too large or too small, no unnecessarily complex pieces of code, no magic numbers etc etc. This makes me happy. The uncomfortable feeling is gone. Now I can ask my colleagues to review what I have written and hopefully they have some insight in how I can improve my code.

Be humble. Be open. Be uncomfortable.

No comments:

Post a Comment