Kickstart Clean Code
By: Niranjan Royalu Suresh Bapu
Tags: Best Practices, Kickstart, Clean Code, Coding Style, Programming

This blog is for beginners who want to kickstart clean code style of programming. As programmers, we will be coding in a language which suits the domain. For example, for machine learning and ai, we would use python. Based on the language and the problem we are going to solve we would code a solution for the problem either in object-oriented approach or functional approach. These two terms in itself is a big topic which has its own pros and cons. Let’s take the functional approach now and see how we can apply the clean code practices to it.

Why do we have to write clean code?

We as programmers code every day. Maybe we write new functions to bring in the functionality or modify the existing function to solutionize the problem. As days pass by, it is not the same developer who will be working on the code. So, the new developer should not be wondering when he sees the code like “Oh my god! What the hell is this? I understand nothing. It is not readable. It is cluttered.”. These thoughts will reduce the developer’s productivity. Now he/she has to go through the code to understand what it is doing and then only he can start work.

Now the problem still continues if the new developer does not clean the code and adds more clutter to the code. Clean code is a practice which should be followed from day one and a small refactor of the code when seen unclean could be cleaned to avoid problems in the long run.

Writing clean code:

Writing clean code is an art. It starts from naming the variables to adding comments to the code. We will now see some few steps to write clean code. Clean code can be written by following:

- Naming variables, functions, classes appropriately

- Writing shorter functions

- Reducing the number of arguments to a function

- Adding comments to the code

- Organizing the code

These are the few steps which can be followed first to write modular clean code. We will see one by one in brief.

Naming:

The first step is to name the variables, functions, classes clearly. Each programming language has its own naming styles for different attributes (private or public). For example, constants would be named in all caps. Now assume a variable is named “x” and it is used everywhere in the function. It doesn’t make sense to the new developer what that “x” will be holding and what is happening with the data in that variable. For example, if “x” holds the aws bucket name, it could be named “aws_bucket_name” so that it would make much sense and makes the code readable in a flow.

Writing shorter functions:

It would be better if the function has at most three lines and it does one and only thing. This is because it helps the developer to easily understand what the function is doing. For example, there can be nine lines of which each three lines do a specific job, refactor each three lines into its own functions so that now the parent function has three function calls each expressing its own need. This makes the function now easier to read.

Reducing arguments:

It would be better to limit the arguments to a function. There are many ways in which it can be done. One way could be to keep static variables to the class itself. Function arguments should be limited because the developer should also now remember the order of the arguments in which it should be passed for the function to work. As a developer, we would be writing unit tests to the function. Based on the number of arguments, the combination of arguments in which code needs to be tested also increases. So, try to minimise the number of arguments to the function.

Adding comments:

If the code is much readable it would also mean for us why a comment is needed. Adding comments to the code helps the other developer understand the why’s of the code. Now we should be careful when to add comments because if we add more comments to the code, it will also clutter the code. Add comments when necessary. Add small crisp comments which would be understandable.

Organizing the code:

Organizing the code is also the most important aspect in clean code. For example, the called functions should be below the calling function and the further called functions below that in the order. This helps in readability of moving from top to bottom in the order in which the function is called.

In this blog, I have tried to explain in brief a few steps to kickstart clean code. In my next blog, I will explain other steps to be followed to write clean modular code and in further blogs will explain each of them in detail with code examples.