These apply to name everything: variable, class names, methods, package, etc.

When you have the semantic model fresh in your mind it’s easy to map your code onto it

It’s not hard to remember that a variable named “x” is the date a record was created and “y” the date it was deleted.

Leave clues that will allow you to rebuild the semantic model when you come back at a later time.

Source: Why your code is so hard to understand

Comments

Comments can lie. The code does not.

Ideally, your code should be so good that you do not require comments.

This might not be true in one case which is when you code something weird and required domain-specific explanation.

Like in Machine Learning, a choice of certain parameters or a certain step can be understood better by putting the link to explanation/research paper in the comment. While reading just the code, it might not be obvious for the reader to understand what to make of it. (Source: Jeremy Howard fast.ai).

Intent giving names

Use pronounceable names

Variables should be named so that they make it clear what it contains.

Variable names should always be easy-to-read, be short yet meaningful, mixed case with a lower-case first letter English words.

b)Use plural names for arrays/collections of objects.

c) Constant (static final) variable names should be in all capital letters and separate words in the name with the underscore, i.e., PRIORITY_NORMAL,

Source: java coding-convention and cleancode

For matrix operations use row columns instead of y and x because in some languages matrix is treated as [y,x]

Source: Gayle Laakmann McDowell (Cracking the Coding Interview Book)

Abbreviations and short forms

Many people try to optimize for vertical space of the screen but forget horizontal space.

This is kind of an exception to the above rule: for things that are very very common, short forms are better. (You also type less).

Just don’t short everything to be a single letter. Be small but descriptive.

Also, having a list of abbreviations used in your project readme/documentation can be helpful.

Examples,

  1. repo for repository

  2. util for utility.

  3. serv for service

  4. len for length

  5. dto for Data Transfer Object (pretty common)

Example based on Spring Boot (Java)

Make sure you use the same abbreviations everywhere in your code aka one word per concept in all classes.

(Below examples are for method names)

  • get/fetch/find

  • set/update

In case you are trying machine learning, I guess this article is much better suited: Jeremy’s notes on fastai coding style

Magic numbers and strings

Any number or string in your code that isn’t immediately obvious to someone with very little knowledge.

Source: StackOverflow

Better to declare them in a separate constants file.

Constants should be snake_case and CAPITAL.

List of HTTP status codes

In the case of Java, enums can also be used to achieve something similar but they will contain a lot of java boilerplate.

There are also language-specific naming conventions involved like:

  1. Java and Typescript prefer camelCase

  2. Python and SQL prefer snake_case

For Unit test cases

My aim is not so much about reducing the lines of code. Tests are usually small. (In case if your tests are huge and complex then you must have written a bad code)

I prefer to keep the end of each method the same. Like:

Code was taken and modified from Unit tests with Mockito — Tutorial

I use Mockito so I skip the first line. The start of the function then is with all stub actions.

The last 3 lines are usually similar.

  1. A variable expected: expected value

  2. A variable actual: calls the function/method

  3. assertion

I find that for most tasks it is easier to make changes to expected and actual variables rather than put everything into assertions. The assertion is always clean.

An alternative to the above assertion is to write like below which I find to be messy.

Code was taken and modified from Unit tests with Mockito — Tutorial

Previous: Flat is better than nested

Next: Avoid creating god classes and long methods #NotEnoughChunks

Index: Tips on writing Clean Code