Good Code vs Bad Code

Code Morning!

We are software developers and we write code. We aim to solve the problems with our coding skills but coding skills are not just limited to solving a problem. The code written by you today is most likely to be modified tomorrow by you or someone else. Writing good code is one of the many other programming skills that every developer should master in. Good code helps others understand the intent of the code. How can we define Good Code? I think we first need to understand Bad Code and we will do that next.

Bad Code

Bad Code is the code that is bad. Well, this could be the dumbest definition you would ever find on internet but that’s what comes in my mind at the moment. Wasting not much time on formal definitions, let’s try to understand what a Bad Code really means. Any code can be categorized as Bad Code if,

  • It’s hard to read and understand.
  • It contains many levels of nested blocks like if-else statements, loops etc.
  • It has poorly named methods and variables.
  • It contains unnecessary code comments, at multiple places within a method.
  • There are very less or no logical separations and everything is written in a single method.
  • The classes are highly coupled and even a small change in the code may require changing many other parts of the system.
  • Similar code is duplicated at multiple places without giving any thoughts of re-usability of the code.
  • And many more that make yours and your peers life hell.

Programmers write Bad Code due to many reasons but the most common ones are:

  1. Meeting the deadlines in a fast paced project.
  2. Unawareness about code quality and best coding practices.
  3. Carelessness, i.e. taking shortcuts, not thinking much about future changes and ignoring the possibility of other developers touching the code.

Bad code written today may save some time and resources in present but costs heavily in future. There comes a time when making even a small change warrants many adjustments, mostly unnecessary ones, all because different modules are highly coupled. Today’s bad code becomes worse tomorrow, becomes more worse with time and slowly becomes a big mess.

So what is Good Code? Let’s talk about it.

Good Code

Good Code doesn’t fall into any of the categories we outlined in previous section. The most important virtues of Good Code are it’s clean and elegant. There are many definitions given by many people and I’m just highlighting one of them:

Clean code is a code that is written by someone who cares.

-Michael Feathers

Those who write Good Code actually cares about their code. They know how frustrating it is to spend hours and days finding that single erroneous line of code in the big mess created by others over time.

Some of the characteristics of Good Code are:

  • It’s easy to read and understand. Good code is as simple as a prose. People should enjoy reading the code.
  • It doesn’t contain same (or similar) code written multiple times. Good code follows DRY (Don’t repeat yourself) principle.
  • It contains loosely coupled classes and modules and there should not be more than one reason to change any piece of code. Remember SRP (Single Responsibility Principle)?
  • It’s easy to make the changes or extend your code by others without wasting much time on fixing unrelated things.
  • Both classes and methods are small and precise. A big method should be divided into smaller logical units of work and each unit should be extracted as private methods, making it easier to understand the code.
  • It has meaningful and precisely named classes, methods and variables. One should be able to figure out what they do just by reading the names.

Good code screams what it intends to do. It does the job in the most simplistic way. It doesn’t frustrate. It doesn’t take unproductive hours and days just for figuring out where is that method or line of code that I need to change. If someone is working on the code you have written, they should come to you to understand the functionality, not the code.

Summary

Writing good code is an art. It takes time. Just practice it, you will get better day by day. Make a habit of reading your own code once you are done with it. See if you can understand it without any hurdles. Refactor the pieces that are hard to understand. Follow the best coding practices.

We will touch upon the different aspects of writing good quality code in upcoming posts. Have anything to add in the list of Good vs Bad code? Leave your thoughts in the comments section. Share the post with others if you find it useful. Thanks for reading.

 

Share This:

Leave a Reply