Clean code principles

Incora
4 min readAug 1, 2019

Clean code should be easy to read and maintain. If it’s difficult for you to comprehend the code you wrote a few days ago, your coding style definitely needs some improvement.

Before any of your applications enter the market, each line of code will be read multiple times. Your teammates should be able to read the code and understand your logic to fix bugs and add new features quicker. You don’t write code solely for computer consumption, it should be understood by humans.

There are a few core principles that considerably elevate the quality of your coding style. All these principles were coined by the most outstanding professionals in the industry and our team strongly recommends you to follow them.

You ain’t gonna need it (YAGNI)

YAGNI is one of the key principles of Extreme Programming that emphasizes the importance of implementing things when you actually need them.

With this principle, you minimize the amount of unnecessary work, which is crucial for the developer’s productivity and software simplicity. YAGNI is about just-in-time manufacturing. Don’t implement features that may be needed in the future, they are expensive, time-consuming, and probably will be a waste of resources.

Don’t repeat yourself (DRY)

It is a great practice to apply the DRY principle because it saves much time in development. The modification of an element doesn’t require a change in any other logically unrelated elements in a system.

Keep it simple, stupid (KISS)

The simpler your code is, the more maintainable it will be in the future. Avoid fancy features and unnecessary complexity, use them only they have huge benefits for the design.

Use Design Patterns and Coding Standards

There are numerous design patterns over the Internet, including AirBnB style, Google JavaScript Style, and others. Sharing a common coding style makes it much easier to collaborate with your team and the community.

There are a great variety of libraries that can help solve any problem. It is a good practice to follow existing patterns to write necessary functionality with a nice structure. At the same time, keep in mind, that adding libraries is not always the best solution. Sometimes, it is more efficient to write your own code which will increase the loading speed.

Careful and Efficient naming

When we create a class, it is very important to check the naming convention. It should follow some logic and give a nice description of the responsibility of that class.

It is also common when sloppy developers name a function the same way in different files. While creating a file or directory, we have to check the naming convention. If there are too many, rename it or create a subdirectory.

In addition, it is a good practice to set clear indexes when creating a new database request.

Good naming conventions and pretty formatting play a significant role in inefficient development. Nice documentation and comments will make the life of an engineer much easier.

Quality management

To elevate code quality and maintainability, you can use a variety of practices including refactoring, unit and acceptance tests.

At Incora we often use Eslint for checking potential incorrectness in the dependencies of your code. This configurable linter tool is great for identifying and reporting patterns in JavaScript. It helps to maintain your code quality with ease and keep the style. Also, it is great when the code is well-documented or even self-documented. It makes it much easier to follow and test.

Refactor as soon as possible (RASAP)

Even though sometimes it is tempting to cut corners, we should not ignore best practices. Being sloppy will definitely slow down the performance in the long run due to the increased frequency of bugs and the long-term costs of maintenance. Our experience at Incora has shown it’s very unlikely you’ll find time to clean up your code later.

Poor technical decisions are especially detrimental at the initial stage of development. Technical debt significantly cuts the chance of reuse and sets a low bar for quality throughout the rest of the codebase.

SOLID Design principles

S — Single responsibility principle states that each functional unit (class, method or other entities) should only be responsible for a single aspect of a system’s requirements and remain undisturbed. Diving your code into smaller elements is very especially helpful in testing and refactoring. Many programmers tend to create long run-on functions that perform many loosely related tasks. It is not the best approach because such code is quite hard to read and usually has many bugs in it.

O — Open/closed principle states that software entities (classes, modules, functions,…) should be open for extensions, but closed for modification.

L — Liskov substitution principle states that objects in a program should be replaceable with instances of their subtypes without affecting the correctness of that program.

I — Interface segregation principle states that you shouldn’t add additional functionality to an existing interface by adding new methods. Instead, create a new interface and let your class implement multiple interfaces if necessary.

D — Dependency inversion principle states that high-level modules should not depend on low-level modules. Both of them should depend on abstractions. Abstractions, at the same time, shouldn’t depend on details. It is better when details depend on abstractions.

Our team hopes that these tips were helpful and you will strive to write clean and maintainable code.

Originally published at incorainc.com/blog/

--

--

Incora

A software development company that turns ideas into products. https://incora.software/