DEV Community

Nic
Nic

Posted on • Originally published at coderscat.com on

How To Write Readable Code

write-readable-code.jpeg

We spend more time on reading than on writing code. Readable code is essential. Here are my tips for writing readable code.

Naming

Naming is the first thing we can improve on writing readable code.

If we put proper names to variables, functions, classes, code will easy to be understood. Remember to choose the more self-descriptive names for variables or functions.

Naming is not easy and needs some practice.

Keep simple

Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.

– Antoine de Saint-Exupery

Keep simple on every file or function. A class or function should do one thing or one similar thing only.

A function doesn’t contain too many lines of code.

Organize code

Reading code is similar to reading a book. If a book is organized well, we know the subject of each chapter, we can skip some sections if necessary.

Organize code well means each file contains and only contains related lines of code; each module or directory contains related source code files.

Write comments, but not too many …

Comments are helpful for understanding code, but too many comments will distract us.

It’s better to make comments not only restate the code.

So, in comments, we need to explain something outside of code.

Imagine others will read your code

Imagine you are not writing code, but explain your purpose to others.

Try to read the code after you finished it.

The post How To Write Readable Code appeared first on CodersCat.

Top comments (0)