top of page

How to Write Clean Code?

Clean Code is easy to understand and easy to change. It is the Code that another developer can Read, Understand , and Write easily.

Your code should be as efficient, readable, and maintainable as possible, and instead of only solving the problem, you should always put a bit of extra time in to focus on the design of your code, on architecture. Your code should be understandable, should be clean.

Characteristics of Clean Code:

  1. Clean code should be readable. If someone is reading your code they should have feeling of reading a poetry or prose.

  2. Clean code should be elegant. It should be pleasing to read and it should make you smile.

  3. Clean code should be simple and easy to understand. It should follow single responsibility principle (SRP).

  4. Clean code should be easy to understand, easy to change and easy to taken care of.

  5. Clean code should run all the tests

Writing Clean Code

Clear Variable and Function Names

Code will become easily readable if you write out full, descriptive variable and function names.

Short Functions

Functions are more understandable, readable, and maintainable if they do one thing only. If there’s a bug when writing short functions, it is usually easier to find the source of that bug. Also, the code will be more reusable.

Good Documentation:

Your code should have good documentation so that future developer can understand your code easily about "what your code is doing" and why.

Meaningful Names

Do not use comments to explain why a variable is used. If a name requires a comment, then you should take your time to rename that variable instead of writing a comment.

Avoid Disinformation

Be careful about words that mean something specific. Do not refer to a grouping of accounts as accountList unless its type is actually a List. The word has a specific meaning and it may lead to false conclusions

Avoid Noise Works

Noise words are the words that do not offer any additional information about the variable. They are redundant and should be removed.

Some popular noise words are:

  • The (prefix)

  • Info

  • Data

  • Variable

  • Object

  • Manager

Be Consistent

When writing code, consistency is key. People shouldn’t be able to look at a code base and tell exactly who wrote each line of code without a git blame! If you are using semicolons in JavaScript, use them at the end of each statement. Use ” vs ‘ consistently as well!

Encapsulation + Modularization.

Group like variables and functions in order to make your code more reusable and understandable. Break long programs into different files so that your code is more modular and digestible. Long files are often hard to sift through, and you may want to use small chunks of code from project to project. Group like items in your code so that it is more reusable.

Avoid to much Comments:

Developers use comments to specify the purpose of line in their code which is helpful in explaining the program or code. But sometimes it can create a problem. It can create confusion among the developers and get distracted due to useless comments. You should always use comments where they are needed otherwise don't use comments.

Readable Code:

Beginners write code in a single line without any whitespace, line break etc. This makes the code messy and difficult to read. While writing the code you should always use whitespace, line break, Comments etc.

Unit Tests

Writing Unit test is very important in development. It makes your code clean, flexible and maintainable. Making changes in code and reducing bugs becomes easier. There is a process in software development which is called Test Driven Development (TDD) in which requirements are turned into some specific test cases then the software is improved to pass new tests.

Organized Code:

Sometimes we add and delete so many files or directories in our project that sometime it become complicated and annoying for other developers to understand the project and work on it. A well-structured folder and file make everything clear and it becomes easier to understand a complete project, search some specific folder and make changes in it. So make sure that your directory or folder structure should be in an organized manner.

Principles of Clean Code:

  • DRY — Don’t Repeat Yourself. Opposite is WET.

  • KISS — Keep It Simple Stupid.

  • YAGNI — You Aren’t Gonna Need It

  • Composition over Inheritence

DRY: Don't Repeat Yourself.

Closely related to KISS and the minimalist design philosophy. It states that every piece of knowledge (code, in this case) must have a single, unambiguous, authoritative representation within a system (codebase). Violations of DRY are referred to as WET: We Enjoy Typing, Write Everything Twice, Waste Everyone's Time.

KISS: Keep It Simple Stupid.

A design principle originating from the U.S. Navy that goes back to 1960 already. It states that most systems should be kept as simple as possible (but not simpler, as Einstein would have said). Unnecessary complexity should be avoided. The question to ask when you're writing code is "can this be written in a simpler way?"

YAGNI: You Aren't Gonna Need It.

A developer should not add functionality unless deemed necessary. YAGNI is part of the Extreme Programming (XP) methodology, which wants to improve software quality and increase responsiveness to customer requirements. YAGNI should be used in conjunction with continuous refactoring, unit testing, and integration.

Composition over inheritance:

It's a principle where you design your types over what they do instead of over what they are. One of the ways to implement this principle is with the Object.assign() method in ES6.

The Tech Platform



bottom of page