3 Reasons Why People Write Insanely Bad Code
3 Reasons Why People Write Insanely Bad Code
Ever wondered why you see bad code... everywhere? There are a few reasons why this keeps happening.
Join the DZone community and get the full member experience.Join For Free
Open source vulnerabilities are on the rise. Read here how to tackle them effectively.
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 about much more than the pay-check….
So, I won’t delve into that 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 learned about programming at the universities, through self-study or books, never reflect the needs of 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 a great accumulation of experience. What's more, bad code is even expected from beginners!
When beginners are expected to produce poor quality code, more often than not, that is what you will get!
While 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:
- By expecting lower standards from those at the start of their careers, we are saying that quality is not an important factor. A direct result of low entry barriers is having a huge influx of low quality developers, and that leads to point number 2…
- By forcing them 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.
- The 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’ and ‘Pragmatic Programming’ that 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
Three-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 or 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.
The books also need to get rid of “quick fix” styles of programming. For example, it is not uncommon to find methods with 10 arguments.
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 two-page long method! Just to list a few.
A good way to learn programming is to master the syntax of the language, the tools needed for efficient development, and the 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 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 have read this post and are going to be authors in the future, please remember to have good code in the book! I myself have been guilty of using makeshift 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 beginner books contain the poorest of the poor quality code, how to we expect people who learn from them to produce good quality code?
Published at DZone with permission of Deepak Karanth , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.