Skip to content

Sérgio Kopplin

Clean Code - Naming

Notebook, Clean Code2 min read

Intention-Revealing and Pronounceable Names

Avoid things that are not "words". Like genymdhms, which means generation date, year, month, day, hour, minute and second, btw), also getFlaggedCells, isFlagged, fileAgeInDays, elapsedTimeInWeeks instead of d, x, y or z.

Bad:

1const yyyymmdstr = moment().format("YYYY/MM/DD");

Good:

1const currentDate = moment().format("YYYY/MM/DD");

Avoid Desinformation

Try not to give names that represent the meaning of the variable. accountList that is not a "list". account or bounchOfAccount would be better. Also, try to avoid names that vary in small ways, like ControllerForEfficientStorageOfStrings and Controller ForEfficientHandinlgOfStrings; They are both so similiar, that is more disinformation than information.

Bad:

1const list = { data: true };

Good:

1const list = [];

Make Meaningful Distinctions

Avoid making variables with small changes in the name, due to similarity in meaning. getAccount vs getAccountData vs getAccountInfo. If the name is different, it must mean something different too.

Avoid Number-Series naming, like a1, a2, a3. They are not disinformative, but noninformative.

A void naming variables in the same place like message and another one theMessage. They should mean something different.

Bad:

1const list = [];
2const list1 = [];
3
4const a1 = {};
5const a2 = {};
6
7const message = "";
8const theMessage = "";
9const xMessage = "";

Good:

1const usersList = [];
2const usersFilteredList = [];
3
4const account = {};
5const profile = {};
6
7const message = "";
8const formattedMessage = "";
9const transatedMessage = "";

Use Searchable Names

Avoid naming variables with single letters because they're hard to find.

Bad:

1const x = 1;
2const b = "";

Good:

1const userId = 1;
2const userLabel = "";

Avoid Encodings on names

  • Just don't.

Bad:

1const sérgio = {};

Good:

1const sergio = {};

Interfaces and Implementations

IShapeFactory is for Interface, better to use ShapeFactory, or even ShapeFactoryImp. Better to avoid this on naming.

Bad:

1IShapeFactory {}

Good:

1ShapeFactory {}

Avoid Mental Mapping

Try to not create something that shows mental abilities, like naming "r" something that is an url with host and scheme removed. Professional programmers create things that are clear, not just smart.

Bad:

1const locations = ["Austin", "New York", "San Francisco"];
2locations.forEach((l) => {
3 doStuff();
4 doSomeOtherStuff();
5 // ...
6 // ...
7 // ...
8 // Wait, what is `l` for again?
9 dispatch(l);
10});

Good:

1const locations = ["Austin", "New York", "San Francisco"];
2locations.forEach((location) => {
3 doStuff();
4 doSomeOtherStuff();
5 // ...
6 // ...
7 // ...
8 dispatch(location);
9});

ClassNames

Avoid names like Manager, Processor, Data or Info. A class name should not be a verb. Names like Customer, WikiPage, Account and AddressParser are better.

Bad:

1class Manager {}

Good:

1class Account {}

Method Names

They should have verb or verb phrase like postPayment, deletePage or save. It can also use get, set and is.

Bad:

1loadPayments = ();

Good:

1getPayments = ();

Don't be cute

Avoid cute names, or even joke names. Like HolyHandGranade that means DeleteItems.

Bad:

1const bestObjectEver = {};

Good:

1const userData = {};

Pick One Word per Concept

Avoid naming different classes with methods like get, retrieve and fetch. Use a single one for all of them. Also, avoid using classes like controller together with manager and drive. Why are not all of them controllers? If they have different meanings, so them shoulb be have better names.

Bad:

1class Controller {}
2class Manager {}
3class Principal {}

Good:

1class MainController {}

Don't Pun

Having different classes that have same method "add" that does different things can lead to a pun. Two purposes must have different words. Add and Append on a list is better than a single add that does different things depending on the class you call.

Bad:

1class User {
2 static add() {}
3}
4
5class Profile {
6 static addCreate() {}
7}

Good:

1class User {
2 static add() {}
3}
4
5class Profile {
6 static add() {}
7}

Use Solution Domain Names

Better to use words that are more usual to developers without nowing any past patterns. AccountVisitor contains a pattern Visitor. A more technical word is better than a customer word.

Bad:

1const realStateCustomerRule = {};

Good:

1const customerSettings = {};

Don't Add Gratuitous Context

Avoid adding extra context for eveything. Like "CashierDeluxeApp". Try to not add CDP as a prefix for classes, like CDPMailingAddress or CDPUserListing.

Bad:

1class TWITCHMainController {}

Good:

1class MainController {}
© 2020 by Sérgio Kopplin.