Writing Code that Matters

Aman Bhawsar
Groww Engineering
Published in
5 min readMay 20, 2021

--

Image By Liam McKay via Picography

In this article, I’m going to talk about writing clean code in general with some examples.

What are the benefits of Clean Code?

As a Developer, you are responsible for the quality of your code. There are tons of benefits of writing clean code, like-

  • Quality of code Improves(The quality of code is inversely proportional to the effort it takes to understand it)
  • Better Readability of code
  • Makes Code Maintenance easier

“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” — Martin Fowler

1. Magic Number

A Magic number means when we are assigning a number with no clear meaning. If a new programmer comes and works on that codebase, then the person doesn’t know what that value refers to.

Always try to use variables in such cases, and you can use tools like ESLint to identify unnamed constants.

2. Avoid Larger Function

When a function becomes large then it is better to break it down into smaller functions so that these smaller functions can be used across the app.

You might have thought this when you came across a large function with so many things going on — “I don’t understand how this code works”, “this code is a mess”, “it’s hard to modify this code” etc.

It is easier to debug and understand smaller chunks of code.

Reusability in Large vs Small Functions

The code for a small function looks more than that of a large function in this example but if you will see functions in the smaller function they will be more reusable across the codebase.

3. Code Repetition

As a programmer, we should always follow DRY Principle (Do not repeat yourself) during coding.

Duplicate code is bad practice. If you need to make a change in that repeated code then you will feel significantly worse. You’ll be much more tired if you have to change your code in several files. So, try your best to avoid duplicate code.

So, it is better to make a common function for duplicates code (or code that is reusable and can be used by other developers) at the very first time we encounter them.

For example: you might need operations on number to convert it in Comma seperated format, converting values to Crores etc.

4. Proper Naming Convention + Meaningful Names

Follow the naming convention used by the language. If Camel case is used in naming convention for both naming variables as well as function. Then both of them should follow that rule:

const codingLanguages = ['c++','java','js'];const isPrimeNumber = (num) => {
// do something
};

The name given to the variables/methods should be both meaningful and specific so that one can understand just by looking at it what’s gonna happen in that method or what’s stored in that variable.

Always name your properties or variables in such a way that leaves absolutely no room for confusion/misunderstanding/ambiguity

5. Function arguments

We should minimize the number of arguments passed to a function. One or two is ideal, and three can be acceptable but never more than that. It is better to pass an object when the number of arguments is more than 3.

Less arguments = better function.

// BAD PRACTICE
const addStudent = (name, class, section, age) => {
// do something
};
// GOOD PRACTICE
const addStudent = (student) => {
const { name, class, section, age } = student;
// do something
};

6. Flags as function parameters are prohibited

If your function contains a flag then it means it does multiple tasks. It is better to split your function for every single task.

Prefer clear code over clever code

7. Type checking is not a good habit in an untyped language

We know that Javascript is untyped. So, we should not have conditions that need to check type. If you need type checking then go for Typescript.

Why not?
Because in javascript an integer might be denoted in the form of a string.
For example, when we use the toFixed() method on integer the result obtained is of string type. And adding the type of string that might introduce bugs when later someone modifies it.

8. Always remove dead code

Dead code may be unused comments, unreachable code, or deprecated code which is not required anymore in our codebase. It should be removed immediately when you see it. It’s like trash in your codebase. Also, someone might end up reviewing deprecated code or multiline comments if the developer does not observe that it is no longer used. As a developer, you might have emotions attached to the code(it might be used later), but it’s better to let it go than pile it up in our codebase. Anyways, you would have that code in version control if you need it later.

Leave the code better than you found it.

9. Comment why, not what

Comments should be good, but your code needs to be self-explanatory. Write proper comments where logic is complicated or some information regarding it has to be mentioned.

If you use VS Code, this extension can be super useful. Using it commented out code can also be styled as shown below. It is much clear and the extension can be configured according to our requirements.

Generally, Less comments = less maintenance

10. Prefer Loose Coupling

Coupling refers to the degree to which the different modules/classes depend on each other, it is suggested that all modules should be independent as far as possible, that’s why low coupling should be there.

Tight coupling makes code hard to extend and test.

Conclusion

At last, it is Practice, Practice, Practice. One can’t write clean code just because they know these practices. They need to follow them and review their code where they might find some more ways to make it look a lot smoother and clean.

A Good code should read like a story, not a puzzle

No one’s perfect. If you’ve found any errors, want to suggest enhancements, or expand on a topic, please feel free to send me a message. I will be sure to include any enhancements or correct any issues.

If you’ve enjoyed this story, please click the 👏 button and share it, so that others can find it as well! Also, feel free to leave a comment below.
Groww Engineering publishes technical anecdotes, the latest technologies, and better ways to tackle common programming problems. You can subscribe here to get the latest updates.

--

--

Software Development Engineer@Groww | HackWithInfy 2019 Winner | Competitive Coder | Intern@Appointy | CSE@SGSITS