SoftwareYoga

Articles on Coding and Architecture

3 Reasons Why People Write Insanely Bad Code

Ever Wondered Why You See Bad Code Everywhere..?

I have been contemplating over the last number of days as to why we as an industry keep producing bad code all the time.

The number of developers who are able to build systems with high quality code are far and few in between, as the cliche goes, like a needle in a haystack.

1. The Obvious Reason…

The most obvious reasoning I could imagine are that there are good programmers and not-so-good programmers, there are people who are at a higher skill level and those at lower levels, there are people interested in the craft and the rest who don’t care much more than the pay-check….

So, I won’t delve into those any more.

Could there be more issues at the ground level? Are there fundamental issues resulting in all the bad code? I had to dig deeper to find out.

2. Low Expectations…

For one, it is widely acknowledged that the stuff learnt about programming at the universities, through self-study or books never reflect the needs of the real world jobs.

Hence, it is always assumed that beginners are always going to learn how to produce good code on the job. It is believed that real programming lessons are only to be had with great accumulation of experience. What more, bad code is even expected of beginners…!

When beginners are expected to produce poor quality code, more often than not, that is what you will get!

Whilst part of the above reasoning is true, it is an assumption that I do not want to accept in full for many reasons, but mainly the three below.

  1. By expecting lower standards from those at the start of their careers, we are saying that quality is not an important factor. A direct dripping result of low entry barriers is having a huge influx of low quality developers…and that leads to point number 2…
  2. By forcing to work with mostly low quality developers, we are making life miserable for those who do care about the craft and those who pride themselves on the quality of their work.
  3. Combination of the two factors above has resulted in the situation we are in today, wherein everyone believes they can code and professional developers are treated like commodity resources.

We need to expect more out of beginners. Who would hire a doctor if he said that he can’t perform a surgery to the highest standards, or if a pilot says that he can’t really fly an airplane without crash-landing. Its simply not acceptable.

So then, why is a lower quality acceptable in the software industry?

So, what is the root cause of low quality developers?

3. Programming Books…

A few days back, I was looking at some of my old books and came across a couple of books on Java – One for beginners learning Java and the other one for SCJP Certification. For reasons that will be obvious in the next minute or so, I would not want to name the books.

Unfortunately, the very books that beginners refer inadvertently depict exactly how poor code is supposed to be.

Most beginners books on any programming language are full of bad code. Bad code of every variety that people such as Robert Martin have covered in their books on ‘Clean Code’, ‘Pragmatic Programmer’ are widely used in most beginner books.

Some examples of bad code you would find are…

3.1 Badly named classes, variables and methods….

i, ii, j, k for loops, clazz, SampleChapter1 for class names etc.

3.2 No Separation of Concerns…

3 page long main() methods that do everything, instead of splitting responsibilities into different methods.

3.3 Bad Coding Practices…

No good code coding practices such as including validations, exception handling. Instead they generally use one big generic try()..catch(Exception e) block that encompasses all the code within the class.

Generous use of if-else, switch, goto statements etc.

3.4 Shortcuts…

The books also need to get rid of “quick fix” style of programming. For example, it is not uncommon to find methods with 10 arguments.

Too many code examples using shortcuts…

Need to do two types of calculations? No problem, pass a boolean parameter and add an if-else construct…

Need to add new functionality? Hey got you there, add more code into the already 2 page long method..

etc. to list just a few.

A good way to learn programming is to master the syntax of the language, the tools needed for efficient development and design of components and modules that make up the code – in that order.

Unfortunately, most books stop at syntax without even briefly touching on the topics of tools and design improvements. The beginners always pick up more than what the author intended to teach. While the intent and the objective of these books were to teach programming language syntax,** the reader always picks up the coding style and methodology as well.**

When people are learning to code, it’s important to teach them that code is written for other humans to read and understand, not just for compilers.

Hopefully, if some of you who have read this post and are going to be authors in the future, please remember to have good code in the book. I have myself been guilty of using make-do code in a number of my posts. Learning to produce good quality code should not only be confined to expert level books alone, but should be part and parcel of every book on programming!

When the beginners books contain the poorest of the poor quality code, how to we expect people who learn from them to produce good quality code?

As a perfect companion to this post, here are the 10 Books Every Developer Must Read