Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

clean code
programming

What is clean code in programming?

Umme Ammara

A code that is easy to understand and modify is called a clean code. In Robert Cecil Martin’s book Clean Code, he mentions that a clean code is simple, elegant, direct, literate, expressive, and well-written.

The most relevant parts of a clean code in a program include:

Meaningful names

Use intention-revealing, meaningful names for variables, functions, methods, and objects, etc.

For example, if a function adds two numbers, name the function sum_func instead of an arbitrary, random name like function1. Similarly, in the case of variables, use names like num1 and num2 rather than random alphabetic letters like a and b.

It is also essential that these names are pronounceable and searchable to avoid any misinformation. Usually, class names should be noun phrases, while method names should be verb phrases.

Functions

The function names should be small and descriptive in a way that conveniently reveals their purpose.

Functions should be ordered so that the programmer can read the code easily in a top-down fashion.

It is also essential that each function does only one thing, and that there is minimal duplication among the functions in a program. The function parameters should be minimalthe ideal number is zero to avoid high-level abstractions and keep the code simple and easy to understand.

Comments

Programmers use comments to express themselves in the code.

Comments should be concise and informative, revealing the programmer’s intent and clarifying any confusing parts of the code.

Comments might also include legal comments, documentations, or to-do lists.

A lousy comment has the following attributes: it is unclear, redundant, misleading, and it might consist of a commented-out code or other irrelevant information.

Formatting

Formatting the code improves readability.

Programmers should use formatting tools and options in their text editors. The program files should not be more than 200-300 lines long. Use blank lines to separate functions and classes, and don’t let the lines run too long.

Other good formatting tips include a proper indentation in the code and horizontal alignment.

Classes and objects

For classes and objects, programmers should think carefully about the data they want to abstract and hide in the objects. Preferably, call the methods of your own class/object rather than future methodsi.e., Law of Demeter.

Error handling

Programmers commonly use error handling in programs. However, this must not hinder the intentions of the code.

Preferably, use exceptions rather than returning error codes in functions. The programmers should clearly define exceptions to provide a clear intent, context, and details of the error type.

RELATED TAGS

clean code
programming

CONTRIBUTOR

Umme Ammara
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring