A beginner’s guide to writing good code!

Arpit Gupta
Analytics Vidhya
Published in
5 min readOct 2, 2019

There are many benefits to writing good code. It not only makes it easy to understand the code but also easy to debug and modify. In this article, I would introduce you to some of the fundamentals to be kept in mind to write good code. I have divided these fundamentals into rules that could be used as a check-list to evaluate your code after it's done.

1 — Use meaningful function and variable names.

Have you ever been in a situation where you are reading someone’s code (or maybe your own) and you end up pulling your hair, to make sense of what the code does? If yes, you can already understand the importance of our rule #1. As obvious as it sounds, choosing meaningful names for your functions and variables is the first step you can take in your journey of writing better code. Though it might seem a little time-consuming in the beginning, it gets easier with practice. It is better explained by the example given below.

Besides, both the function does the same thing, the second function makes its purpose more clear.

2 — Decentralise your code!

Ever seen a code with few functions each having like 50–100 lines of code? There is nothing wrong with writing such code but it makes your code harder to test and debug. It also scares away the people willing to understand your code. It's always a good idea to divide your huge code into small functions. So when you are testing your code you have to make sure that all the functions do their job properly.

Decentralisation also improves the reusability of code. So the next time I need to use the factorial logic, I have a function which does that for me.

3 — DRY(Don’t Repeat Yourself!)

EVERY PIECE OF KNOWLEDGE MUST HAVE A SINGLE, UNAMBIGUOUS, AUTHORITATIVE REPRESENTATION WITHIN A SYSTEM.

— The Pragmatic Programmer

While writing code, many times you come across a situation where you find yourself writing the same piece of code that you have written already. Writing it again and again, may not sound a great effort, in the beginning, but as changes to that part of the logic are introduced, you have to change the code in multiple places. And If you have duplicated that code in 20 other places and you remember only 19 places while changing, May the almighty help you to find the bug when the code breaks next time. All this can be prevented if we use our rule#2 i.e. “Decentralisation of code”. So the next time you find yourself duplicating the code, be lazy, write a function that performs that logic. Functions are reusable and the make any modification to code easier.

4 — Avoid global variables!

Global variables may seem like an easy fix to most of our problems, believe me when I say they are not. Why? because it’s hard to track their changes. For example in a scenario where you have a huge codebase and a global variable is being used across15 functions in it. Due to a small misinterpretation, if you or someone changes that global variable in an unintended way in any one of those functions.

After you are done coding, you are excited to see what your hours of hard work have resulted in and you run your code. BAAM! The code breaks. With a great effort and hours of debugging, you found the error is due to unexpected value of the global variable. Now you have to find out where did it go wrong. It may not sound so horrible while you read it but go ahead and try to face this situation. You will know!

I am not completely against using global variables. They are very useful in declaring constants, configs etc but try not to use them in a situation where they aren't required.

5 — Write test cases for your code!

Before you begin to code a function or a project. Be sure about what exactly is expected with it and think of various test cases satisfying those requirements. Being sure about the requirements reduces the chances for any ambiguity and you end up coding exactly what you wanted. You can write different functions with these cases and check if the output matches. If the requirements change modify those test cases.

Test cases can also assure nothing which was working previously broke while you added new code. Many languages provide you with test libraries or you can use something as simple as assert statements, these are supported in many languages

6 — Keep coding standards consistent!

Select a coding style and be consistent about it. Coding style includes a lot of things such as

  1. Which casing to use for declaring variables and functions (camel case, snake case, etc). read about them here
  2. How you declare constants. Name your constants in a different way than your other variables, to make easy to differentiate. (eg variable, CONSTANT)
  3. No of blank lines between functions.
  4. Testing functions name must always end with “test”.
  5. The tab must be 4 spaces long.

These are just some of the examples of various code standards you can have. It serves as your signature, a unique way you write code. However, don’t be a sucker about them, be ready to adopt other coding standards when you have to contribute code in their projects.

Many big companies have their coding standards well documented and well defined. This makes their codebase consistent and working together less difficult. so you don’t have to scratch your head when you read someone else's code. Some languages have their prefered coding standards too.

7 — Write meaningful comments!

This is the most widely misunderstood rule of all. A well-commented code doesn't mean that you write a comment for every line in your code describing it in English. If you use rule #1, rule #2 well, your code becomes self-explanatory. You don’t have to write a comment on that. However in a situation where your code cannot describe your decisions well, use comments.

These are some of the must be followed steps to write better code. These may sound a lot to start with but once you get a hang of these, it will help you in the long run.

Most of these are form the book “The Pragmatic Programmer” and I would recommend it to everyone who wish to improve further. If I missed anything, I would be happy to know it, feel free to comment below!

--

--