Clean Code

What Is Clean Code?

When we talk about clean code, we talk about a reader-focused development style that produces software that’s easy to write, read, and maintain. Clean code is code that is easy to understand and easy to change.

  • Clearness

  • Best Practices

  • Maintenance

  • Easy to Test

  • Simplicity

  • Consistency

Optimisation vs. Readability

Always write code that is simple to read and which will be understandable for developers. Because time and resources that will be spent on hard readable code will be much higher than what you get from optimisation.

Structure your code such that its readable then it requires less comment s.

Reading your code should be pleasant

Avoid Obvious Comments

More comments means more to keep up to date

Consistent indentation

Code should have an indentation of 4 characters

Code Grouping & Code Spacing

Often the code in your methods can be arrange into a series of actions:

  1. Get data

  2. Filter data

  3. Manipulate data

  4. Return data

Adding a comment at the beginning of each block of code also emphasise the visual separation.

Avoid Deep Nesting

Too many levels of nesting can make code harder to read and follow. Change your code to reduce the level of nesting.

Pick One Word per Concept

Pick one word for one abstract concept and stick with it.

For instance, it’s confusing to have fetch, retrieve, and get as equivalent methods of different classes.

Variables

Naming

Use Camel Case: First letter of each word is capitalised, except the first word.

Length

Avoid shorten variable names

Arrays or Lists

Pluralisation of the variable name makes sense or appending List

Boolean

Boolean's can hold only 2 values, true or false. Given this, using prefixes like is, has, and can will help the reader infer the type of the variable.

Numbers

For numbers, think about words that describe numbers.

Prefix words like max, min, numOf or total

Constants

Use upper case with under scores:

Use pronounceable names and searchable names

When a name is pronounceable mostly it is searchable and searchable names is a must to enhance find operations in refactoring. Avoid using a single letter named variable.

Function names should be verbs

Last updated