We all know that naming is one of the hardest problems in programming, and probably most of us have written code like this when we just started programming:
// reading file signature
try
AssignFile(fp, path+sr.Name);
Reset(fp, 1);
if FileSize(fp) < sizeof(buf) then
continue
else
BlockRead(fp, buf, sizeof(buf));
CloseFile(fp);
except
on E : Exception do
begin
ShowError(E.Message+#13#10+'('+path+sr.Name+')');
continue;
end; // on
end; // try
// compare
for i:=0 to FormatsCnt do
begin
if AnsiStartsStr(Formats[i].Signature, buf) then
begin
// Check second signature
if (Formats[i].Signature2Offset>0) then
if Formats[i].Signature2Offset <> Pos(Formats[i].Signature2, buf) then
continue;
// Check extension
found := false;
ext := LowerCase(ExtractFileExt(sr.Name));
for j:=0 to High(Formats[i].Extensions) do
begin
if ext='.'+Formats[i].Extensions[j] then
begin
found := true;
break;
end; // if
end; // for j
if found then
break;
// ..
end;
end;
I wrote this code more than 20 years ago in Delphi, and, honestly, I don’t really remember what the app was supposed to do. It has it all: single-character names (i
, j
), abbreviations (...Cnt
, buf
), acronyms (E
, sr
, fp
). It has some comments though! (And I kept the original indentation for full immersion.)
I once worked with a very senior developer who used mostly very short names, and never wrote any comments or tests. Working with their code was like working with Assembler — very difficult. Often we were wasting days tracking and fixing bugs in this code.
Let’s look at these (and many other) naming antipatterns, and how to fix them.
Consider this method:
validateInputs(values) {
let noErrorsFound = true;
const errorMessages = [];
if (!values.firstName) {
errorMessages.push('First name is required');
noErrorsFound = false;
}
if (!values.lastName) {
errorMessages.push('Last name is required');
noErrorsFound = false;
}
if (!noErrorsFound) {
this.set('error_message', errorMessages);
}
return noErrorsFound;
}
I can say a lot about this code but let’s focus on this line first:
if (!noErrorsFound) {
The double negation, “if not no errors found…”, makes my brain itch, and I almost want to take a red marker and start crossing out !
s and no
s on my screen to be able to read the code.
In most cases we can significantly improve code readability by converting negative booleans to positive ones:
validateInputs(values) {
let errorsFound = false;
const errorMessages = [];
if (!values.firstName) {
errorMessages.push('First name is required');
errorsFound = true;
}
if (!values.lastName) {
errorMessages.push('Last name is required');
errorsFound = true;
}
if (errorsFound) {
this.set('error_message', errorMessages);
}
return !errorsFound;
}
Positive names and positive conditions are usually easier to read than negative ones.
By this time we should already notice that we don’t need the errorsFound
variable at all: its value can always be derived from the errorMessages
array:
validateInputs(values) {
const errorMessages = [];
if (!values.firstName) {
errorMessages.push('First name is required');
}
if (!values.lastName) {
errorMessages.push('Last name is required');
}
if (errorMessages.length > 0) {
this.set('error_message', errorMessages);
return false;
}
return true;
}
I’d also split this method into two to isolate side effects and make the code more testable, then remove the condition around this.set()
call — setting an empty array when there are no errors seems safe enough:
getErrorMessages(values) {
const errorMessages = [];
if (!values.firstName) {
errorMessages.push('First name is required');
}
if (!values.lastName) {
errorMessages.push('Last name is required');
}
return errorMessages;
}
validateInputs(values) {
const errorMessages = this.getErrorMessages(values);
this.set('error_message', errorMessages);
return errorMessages.length === 0;
}
Let’s look at another example:
const noData = data.length === 0;
$(`#${bookID}_download`).toggleClass('hidden-node', noData);
$(`#${bookID}_retry`).attr('disabled', !noData);
Here, again, every time we read noData
in the code, we need to mentally unnegate it to understand what’s really happening. And the negative disabled
attribute makes things even worse. Let’s fix it:
const hasData = data.length > 0;
$(`#${bookID}_download`).toggleClass(
'hidden-node',
hasData === false
);
$(`#${bookID}_retry`).attr('disabled', hasData);
Now, it’s much easier to read.
Info We talk about names like data
later in this chapter.
My rule of thumb: the shorter the scope of a variable, the shorter should be its name.
I’m okay, and even prefer, very short variable names for one-liners. Consider these two examples:
const inputRange = Object.keys(TRANSITION).map(x => parseInt(x, 16));
const breakpoints = [
BREAKPOINT_MOBILE,
BREAKPOINT_TABLET,
BREAKPOINT_DESKTOP
].map(x => `${x}px`);
Here, it’s clear what x
is in each example, and a longer name would bloat the code without making it more readable, likely less. We already have the full name in the parent function: we’re mapping over the TRANSITION
object keys, and parsing each key; or we’re mapping over a list of breakpoints, and converting them to strings. It also helps that here we only have a single variable, so any short name will be read as “whatever we’re mapping over”.
I usually use x
in such cases. I think it’s clear enough that it’s a placeholder and not an acronym of a particular word.
Some developers prefer _
, and it’s a good choice for any programming language that’s not JavaScript, where _
is often used for Lodash utility library.
Another convention I’m okay with is using a
/b
names for sorting and comparison functions:
const sortedDates = dates.toSorted(
(a, b) => new Date(a).valueOf() - new Date(b).valueOf()
);
However, when the scope is longer, or when we have multiple variables, short names could be confusing:
const hasDiscount = customers => {
let result = false;
const customerIds = Object.keys(customers);
for (let k = 0; k < customerIds.length; k++) {
const c = customers[customerIds[k]];
if (c.ages) {
for (let j = 0; j < c.ages.length; j++) {
const a = c.ages[j];
if (a && a.customerCards.length) {
result = true;
break;
}
}
}
if (result) {
break;
}
}
return result;
};
Here, it’s totally impossible to understand what’s going on, and meaningless names are one of the main reasons for this.
Let’s try to refactor this code a bit:
const hasDiscount = customers => {
return Object.values(customers).some(customer => {
return customer.ages?.some(
ageGroup => ageGroup.customerCards.length > 0
);
});
};
Not only the refactored code is three times shorter but it’s also much clearer: are there any (some) customers with at least one customer card in any (some) age group?
I’ve seen someone using _
name for something that’s used across the whole module, possibly dozens or even hundreds of lines or code, an Express router (the example is from Express docs but I changed the name):
const express = require('express');
const _ = express.Router();
// middleware that is specific to this router
_.use((req, res, next) => {
console.log('Time: ', Date.now());
next();
});
// define the home page route
_.get('/', (req, res) => {
res.send('Birds home page');
});
// define the about route
_.get('/about', (req, res) => {
res.send('About birds');
});
module.exports = _;
I cannot imagine the logic behind this convention, and I’m sure it’s going to be confusing for many developers working with the code. It’ll be much worse when the code grows to do something useful.
Let’s bring back the original names:
const express = require('express');
const router = express.Router();
// middleware that is specific to this router
router.use((req, res, next) => {
console.log('Time: ', Date.now());
next();
});
// define the home page route
router.get('/', (req, res) => {
res.send('Birds home page');
});
// define the about route
router.get('/about', (req, res) => {
res.send('About birds');
});
module.exports = router;
Now, I don’t have trouble understanding what’s going on here. (Using req
for request and res
for response is an Express convention: huge adoption makes it a good idea to keep using it.)
So, x
, a
, and b
are pretty much all single-character variable names I ever use.
On the other hand, long names in a short scope make code cumbersome:
const index = purchaseOrders.findIndex(
purchaseOrder =>
purchaseOrder.poNumber === purchaseOrderData.poNumber
);
Here, long names make the code look more complex than it is:
const index = purchaseOrders.findIndex(
po => po.poNumber === purchaseOrder.poNumber
);
I think the second version is easier to read.
One of the most common cases for short names is loops: i
, j
, and k
are one of the most common variable names ever, and are usually used to store loop indices. They are moderately readable in short not nested loops, and only because programmers are so used to seeing them in the code. However, in nested loops, it’s getting difficult to understand which index belongs to which array:
const keys = Object.keys(pizzaController);
for (let i = 0; i < keys.length; i += 1) {
pizzaController[keys[i]].mockReset();
}
I used to use longer names for index variables for a very long time:
const keys = Object.keys(pizzaController);
for (let keyIdx = 0; keyIdx < keys.length; keyIdx += 1) {
pizzaController[keys[keyIdx]].mockReset();
}
Surely, keyIdx
is way more readable than i
but, luckily, most modern languages allow us to iterate over things without coding artisan loops, and without the need for an index variable:
const keys = Object.keys(pizzaController);
keys.forEach(key => {
pizzaController[key].mockReset();
});
Info See the Avoid loops chapter for more examples.
We talked a bit about the scope in the previous section. The length of the variable’s scope affects readability too. The shorter the scope the easier it is to keep track of what’s happening with a variable.
The extreme cases would be:
[8, 16].map(x => x + 'px')
).Usually, the shorter the scope, the better. However, religious scope shortening has the same issues as splitting code into many teeny-tiny functions: it’s easy to overdo it and make the code less readable, not more.
Info We talk about splitting code into functions in the Divide and conquer, or merge and relax chapter.
I found that reducing the lifespan of variables works as well, and doesn’t produce lots of tiny functions. The idea here is to reduce the number of lines between the variable declaration and the line where the variable is accessed for the last time. The variable’s scope might be a whole 200-line function, but if the lifespan of a particular variable is three lines then we only need to look at these three lines to understand how this variable is used.
function getRelatedPosts(
posts: {
slug: string;
tags: string[];
timestamp: string;
}[],
{ slug, tags }: { slug: string; tags: string[] }
) {
const weighted = posts
.filter(post => post.slug !== slug)
.map(post => {
const common = (post.tags || []).filter(t =>
(tags || []).includes(t)
);
return {
...post,
weight: common.length * Number(post.timestamp)
};
})
.filter(post => post.weight > 0);
const sorted = weighted.toSorted((a, b) => b.weight - a.weight);
return sorted.slice(0, MAX_RELATED);
}
Here, the lifespan of the sorted
variable is only two lines. This kind of sequential processing is a common use case for the technique.
Info See a larger example in the Avoid Pascal-style variables section in the Avoid reassigning variables chapter.
By introducing a constant instead of a magic number we give it a meaningful name. Consider this example:
const getHoursSinceLastChange = timestamp =>
Math.round(timestamp / 3600);
A seasoned developer would likely guess that 3600 is the number of seconds in an hour, but the actual number is less important than what this code does, and we can make it clear by moving the magic number to a constant:
const SECONDS_IN_AN_HOUR = 3600;
const getHoursSinceLastChange = timestamp =>
Math.round(timestamp / SECONDS_IN_AN_HOUR);
I like to include a unit in a name if it’s not obvious otherwise:
const FADE_TIMEOUT_MS = 2000;
Another perfect example where constants make code more readable is days of week:
<Calendar disabledDaysOfWeek={[1, 6]} />
Is 6 a Saturday, Sunday or Monday? Are we counting from 0 or 1? Does week start on a Monday or Sunday?
Defining constants for these values makes it clear:
const WEEKDAY_MONDAY = 1;
const WEEKDAY_SATURDAY = 6;
<Calendar disabledDaysOfWeek={[WEEKDAY_MONDAY, WEEKDAY_SATURDAY]} />
Another common use case for magic numbers, that is somehow widely accepted, is HTTP status codes:
function getErrorMessage(error) {
if (error.response?.status === 404) {
return 'Not found';
}
if (error.response?.status === 429) {
return 'Rate limit exceeded';
}
return 'Something went wrong';
}
I know what 404 status is, but who remembers what 429 means?
Let’s replace magic numbers with constants:
const STATUS_NOT_FOUND = 404;
const STATUS_TOO_MANY_REQUESTS = 429;
function getErrorMessage(error) {
if (error.response?.status === STATUS_NOT_FOUND) {
return 'Not found';
}
if (error.response?.status === STATUS_TOO_MANY_REQUESTS) {
return 'Rate limit exceeded';
}
return 'Something went wrong';
}
Now, it’s clear which status we’re handling.
Personally, I’d use a library like http-status-codes here if I needed to work with status codes often or use not-so-common codes:
import { StatusCodes } from 'http-status-codes';
function getErrorMessage(error) {
if (error.response?.status === StatusCodes.NOT_FOUND) {
return 'Not found';
}
if (error.response?.status === StatusCodes.TOO_MANY_REQUESTS) {
return 'Rate limit exceeded';
}
return 'Something went wrong';
}
However, having a clear name is sometimes not enough:
const CHARACTERS_IN_ISO_DATE = 10;
const dateWithoutTime = date.slice(0, CHARACTERS_IN_ISO_DATE);
Here, we remove the time portion of a string containing date and time in ISO format (for example, 2023-03-22T08:20:00+01:00
) by keeping only the first 10 characters — the length of the date part. The name is quite clear but the code is still a bit confusing and brittle. We can do better:
const DATE_FORMAT_ISO = 'YYYY-MM-DD';
const dateWithoutTime = date.slice(0, DATE_FORMAT_ISO.length);
Now, it’s easier to visualize what the code is doing, and we don’t need to count characters manually to be sure that The Very Magic number 10 is correct.
Code reuse is another good reason to introduce constants. However, we need to wait for the moment when the code is actually reused.
Sometimes, people replace absolutely all literal values with constants, ideally stored in a separate module:
const ID_COLUMN_WIDTH = 40;
const TITLE_COLUMN_WIDTH = 120;
const TYPE_COLUMN_WIDTH = 60;
const DATE_ADDED_COLUMN_WIDTH = 50;
const CITY_COLUMN_WIDTH = 80;
const COUNTRY_COLUMN_WIDTH = 90;
const USER_COLUMN_WIDTH = 70;
const STATUS_COLUMN_WIDTH = 50;
const columns = [
{
header: 'ID',
accessor: 'id',
width: ID_COLUMN_WIDTH
}
// …
];
However, not every value is magic, some values are just values. Here, it’s clear that the value is the width of the ID column, and a constant doesn’t add any information that’s not in the code already, but makes the code harder to read: we need to go to the constant definition to see the actual value.
Often, code reads perfectly even without constants:
<Modal title="Out of cheese error" minWidth="50vw" />
Here, it’s clear that the minimum width of a modal is 50vw. Adding a constant won’t make this code any clearer:
const MODAL_MIN_WIDTH = '50vw';
<Modal title="Out of cheese error" minWidth={MODAL_MIN_WIDTH} />
I’d avoid such constants unless the values are reused.
Sometimes, such constants are even misleading:
const ID_COLUMN_WIDTH = 40;
const columns = [
{
header: 'ID',
accessor: 'id',
minWidth: ID_COLUMN_WIDTH
}
];
Here, the name is not precise: instead of minimum width it only has width.
Often, zeroes and ones aren’t magic, and code is easier to understand when we use 0
and 1
directly instead of constants with inevitably awkward names:
const DAYS_TO_ADD_IN_TO_FIELD = 1;
const SECONDS_TO_REMOVE_IN_TO_FIELD = -1;
const getEndOfDayFromDate = date => {
const nextDay = addDays(startOfDay(date), DAYS_TO_ADD_IN_TO_FIELD);
return addSeconds(nextDay, SECONDS_TO_REMOVE_IN_TO_FIELD);
};
This function returns the last second of a day. And here 1 and -1 really mean “next” and “previous”. They are also an essential part of an algorithm, not configuration. It doesn’t make sense to change 1 to 2 because it will break the function. Constants make the code longer and don’t help with understanding it. Let’s remove them:
const getEndOfDayFromDate = date => {
const nextDay = addDays(startOfDay(date), 1);
return addSeconds(nextDay, -1);
};
Now, the code is short and clear, with enough information to understand it.
We often use constants for ranges of values:
const SMALL = 'small';
const MEDIUM = 'medium';
These constants are related — they define different values of the same scale, size of something, and likely could be used interchangeably. However, it’s not clear from the names that they are related. We could add a suffix:
const SMALL_SIZE = 'small';
const MEDIUM_SIZE = 'medium';
Now, it’s clear that these values are related, thanks to the _SIZE
suffix. But we can do better:
const SIZE_SMALL = 'small';
const SIZE_MEDIUM = 'medium';
Here, the common part of the names, the SIZE_
prefix, is aligned. I call this parallel coding.
Info We talk more about parallel coding in the Don’t make me think chapter.
Another option is to use an object:
const Size = {
Small: 'small',
Medium: 'medium'
};
It has some additional benefits over separate constants:
import { Size } from '...'
vs import { SIZE_SMALL, SIZE_MEDIUM } from '...'
).Size.
And yet another option is to use a TypeScript enum:
enum Size {
Small = 'small',
Medium = 'medium'
}
Tip Usually, enum names are singular nouns in PascalCase, like Month
, Color
, OrderStatus
, or ProductType
.
Which is essentially the same as an object but we can also use it as a type:
interface ButtonProps {
size: Size;
}
The latter would be my choice for TypeScript.
The road to hell is paved with abbreviations. What do you think are OTC, RN, PSP, SDL? I also don’t know, and these are just from one project. That’s why I try to avoid abbreviations almost everywhere, not just in code.
There’s a list of dangerous abbreviations for doctors prescribing medicine. We should have the same for programmers.
I’d even go further and create a list of approved abbreviations. I could only find one example of such a list: from Apple, and I think it could be a great start.
Common abbreviations are okay, we don’t even think of most of them as abbreviations:
Abbreviation | Full term |
---|---|
alt | alternative |
app | application |
arg | argument |
err | error |
info | information |
init | initialize |
lat | latitude |
lon | longitude |
max | maximum |
min | minimum |
param | parameter |
prev | previous (especially when paired with next ) |
As well as common acronyms:
And possibly a few very common ones used on a project but they still should be documented (new team members will be very thankful for that!), and shouldn’t be ambiguous.
I like to use a few prefixes for variable and function names:
is
, are
, has
, or should
for booleans (examples: isPhoneNumberValid
, hasCancelableTickets
).get
for functions that return a value (example: getPageTitle
).set
for functions that store a value or update React state (example: setProducts
)fetch
for functions that fetch data from the backend (example: fetchMessages
).to
for functions that convert the data to a certain type (examples: toString
, hexToRgb
, urlToSlug
).on
and handle
for event handlers (examples: onClick
, handleSubmit
).These conventions make code easier to read and distinguish functions that return values from the ones with side effects.
However, don’t combine get
with other prefixes: I often see names like getIsCompaniesFilterDisabled
or getShouldShowPasswordHint
, which should be just isCompaniesFilterDisabled
or shouldShowPasswordHint
, or even better isCompaniesFilterEnabled
. On the other hand, setIsVisible
is perfectly fine when paired with isVisible
:
const [isVisible, setIsVisible] = useState(false);
I also make an exception for React components, where I prefer to skip the is
prefix, similar to HTML properties like <button disabled>
:
function PayButton({ loading, onClick, id, disabled }) {
return (
<ButtonStyled
id={id}
onClick={onClick}
loading={loading}
disabled={disabled}
>
Pay now!
</ButtonStyled>
);
}
And I wouldn’t use get
for class property accessors (even read-only):
class User {
#firstName;
#lastName;
constructor(firstName, lastName) {
this.#firstName = firstName;
this.#lastName = lastName;
}
get fullName() {
return [this.#firstName, this.#lastName].join(' ');
}
}
In general, I don’t like to remember too many rules, and any convention can go too far. A good example, and fortunately almost forgotten, is a Hungarian notation, where each name is prefixed with its type, or with its intention or kind. For example, lAccountNum
(long integer), arru8NumberList
(array of unsigned 8-bit integers), usName
(unsafe string).
Hungarian notation made sense for old untyped languages, like C, but with modern typed languages and IDEs that show types when you hover over the name it clutters the code and makes reading each name harder. So, keep it simple.
One of the examples of Hungarian notation in the modern frontend is prefixing TypeScript interfaces with I
:
interface ICoordinates {
lat: number;
lon: number;
}
Luckily, most TypeScript developers prefer to drop it these days:
interface Coordinates {
lat: number;
lon: number;
}
I would generally avoid repeating information in the name that’s already accessible in its type, class name, or namespace.
Info We talk more about conventions in the Code style chapter.
Imagine a function that allows us to build a new version of an object based on a previous version of the same object:
setCount(prevCount => prevCount + 1);
Here, we have a basic counter function that returns the next counter value. The prev
prefix makes it clear that this value is out of date.
Similarly, when the value is not yet applied and the function either lets us modify it or prevent the update:
class ReactExample extends Component {
shouldComponentUpdate(nextProps) {
return this.props.code !== nextProps.code;
}
render() {
return <pre>{this.props.code}</pre>;
}
}
Here, we want to avoid unnecessary component rerenders when the code
hasn’t changed. The next
prefix makes it clear that this value is going to be applied to the component after the shouldComponentUpdate
call.
Both of these conventions are widely used by React developers.
Incorrect names are worse than magic numbers. With magic numbers, we can make a correct guess but with incorrect names, we have no chance to understand the code.
Consider this example:
// Constant used to correct a Date object's time to reflect
// a UTC timezone
const TIMEZONE_CORRECTION = 60000;
const getUTCDateTime = datetime =>
new Date(
datetime.getTime() -
datetime.getTimezoneOffset() * TIMEZONE_CORRECTION
);
Even a comment doesn’t help to understand what this code does.
What’s actually happening here is getTime()
returns milliseconds and getTimezoneOffset()
returns minutes, so we need to convert minutes to milliseconds by multiplying minutes by the number of milliseconds in one minute. 60000 is exactly this number.
Let’s correct the name:
const MILLISECONDS_IN_MINUTE = 60_000;
const getUTCDateTime = datetime =>
new Date(
datetime.getTime() -
datetime.getTimezoneOffset() * MILLISECONDS_IN_MINUTE
);
Now, it’s much easier to understand the code.
Info Underscores (_
) as separators for numbers were introduced in ECMAScript 2021, and make long numbers easier to read: 60000
versus 60_000
.
Types (like TypeScript) could help us see when names don’t represent the data correctly:
type Order = {
id: number;
title: string;
};
type State = {
filteredOrder: Order[];
selectedOrder: number[];
};
By looking at the types, it’s clear that both names should be plural (they keep arrays) and the second one only contains order IDs but not whole order objects:
type State = {
filteredOrders: Order[];
selectedOrderIds: number[];
};
We often change the logic but forget to update the names to reflect that. This makes understanding code much harder and could lead to bugs when we later change the code and make wrong assumptions based on incorrect names.
Abstract and imprecise names are probably more unhelpful than dangerous, like incorrect ones.
Abstract names are too generic to give any useful information about the data they hold:
data
;list
;array
;object
.The problem with such names is that any variable contains data, and any array is a list of something. These names don’t say what kind of data it is, or what kind of things the list holds. Essentially, such names aren’t better than x
/y
/z
, foo
/bar
/baz
, New Folder 39
, or Untitled 47
.
Consider this example:
const currencyReducer = (state = new Currency(), action) => {
switch (action.type) {
case UPDATE_RESULTS:
case UPDATE_CART:
if (!action.res.data.query) {
return state;
}
const iso = _.get(
action,
'res.data.query.userInfo.userCurrency'
);
const obj = _.get(action, `res.data.currencies[${iso}]`);
return state
.set('iso', iso)
.set('name', _.get(obj, 'name'))
.set('symbol', _.get(obj, 'symbol'));
default:
return state;
}
};
Besides using Immutable.js and Lodash’s get()
method, which already makes the code hard to read, the obj
variable makes the code even harder to understand.
All this code does is reorganizes the data about the user’s currency into a neat object:
const currencyReducer = (state = new Currency(), action) => {
switch (action.type) {
case UPDATE_RESULTS:
case UPDATE_CART:
const { data } = action.res;
if (data.query === undefined) {
return state;
}
const iso = data.query.userInfo?.userCurrency;
const { name = '', symbol = '' } = data.currencies[iso] || {};
return state.merge({ iso, name, symbol });
default:
return state;
}
};
Now, it’s clearer what shape of data we’re building here, and even Immutable.js isn’t so intimidating. I kept the data
name though because that’s how it’s coming from the backend, and it’s commonly used as a root object for whatever the backend API is returning. As long as we don’t leak it to the app code, and only use it during the initial processing of the raw backend data, it’s okay.
Such names are also okay for generic utility functions, like array filtering or sorting:
function findFirstNonEmptyArray(...arrays) {
return (
arrays.find(array => Array.isArray(array) && array.length > 0) ||
[]
);
}
Here, arrays
and array
are totally fine since that’s exactly what they represent: generic arrays, we don’t yet know what they are going to hold, and for the context of this function it doesn’t matter, it could be anything.
Imprecise names are names that don’t describe the object enough. One of the common cases is names with number suffixes. Usually, it happens for three reasons:
In all cases, the solution is to clarify each name.
For the first two cases, try to find something that differentiates the objects, and makes the names more precise.
Consider this example:
test('creates new user', async () => {
const username = 'cosmo';
await collections.users.insertMany(users);
// Log in
const cookies = await login();
// Create user
const response = await request(app)
.post(usersEndpoint)
.send({ username })
.set('Accept', 'application/json')
.set('Cookie', cookies);
expect(response.headers).toHaveProperty(
'content-type',
expect.stringContaining('json')
);
expect(response.status).toBe(StatusCode.SuccessCreated);
expect(response.body).toHaveProperty('data');
expect(response.body.data).toEqual(
expect.objectContaining({
username,
password: expect.stringMatching(/^[a-z]+-[a-z]+-[a-z]+$/)
})
);
// Log in with the new user
const response2 = await request(app)
.post(loginEndpoint)
.send({
username,
password: response.body.data.password
})
.set('Accept', 'application/json');
// Fetch users
const response3 = await request(app)
.get(usersEndpoint)
.set('Accept', 'application/json')
.set('Cookie', response2.headers['set-cookie']);
expect(response3.body).toHaveProperty('data');
expect(response3.body.data).toEqual(
expect.arrayContaining([
expect.objectContaining({ username: 'chucknorris' }),
expect.objectContaining({ username })
])
);
});
Here, we’re sending a sequence of network requests to test a REST API. However, the names response
, response2
, and response3
make the code a bit hard to understand, especially when we use the data returned by one request to create the next one. We could make the names more precise:
test('creates new user', async () => {
const username = 'cosmo';
await collections.users.insertMany(users);
// Log in
const cookies = await login();
// Create user
const createRes = await request(app)
.post(usersEndpoint)
.send({ username })
.set('Accept', 'application/json')
.set('Cookie', cookies);
expect(createRes.headers).toHaveProperty(
'content-type',
expect.stringContaining('json')
);
expect(createRes.status).toBe(StatusCode.SuccessCreated);
expect(createRes.body).toHaveProperty('data');
expect(createRes.body.data).toEqual(
expect.objectContaining({
username,
password: expect.stringMatching(/^[a-z]+-[a-z]+-[a-z]+$/)
})
);
// Log in with the new user
const loginRes = await request(app)
.post(loginEndpoint)
.send({
username,
password: createRes.body.data.password
})
.set('Accept', 'application/json');
// Fetch users
const usersRes = await request(app)
.get(usersEndpoint)
.set('Accept', 'application/json')
.set('Cookie', loginRes.headers['set-cookie']);
expect(usersRes.body).toHaveProperty('data');
expect(usersRes.body.data).toEqual(
expect.arrayContaining([
expect.objectContaining({ username: 'chucknorris' }),
expect.objectContaining({ username })
])
);
});
Now, it’s clear which request data we’re accessing at any time.
For the new version of a module, I’d try to rename the old one to something like ModuleLegacy
instead of naming the new one Module2
or ModuleNew
, and keep using the original name for the new implementation. It’s not always possible but it makes using the old, deprecated, module more awkward than the new, improved, one — exactly what we want to achieve. Also, names tend to stick forever, even when the original module is long gone. Names like Module2
or ModuleNew
are fine during development though, when the new module isn’t yet fully functional or well tested.
It’s a good idea to use well-known and widely adopted terms for programming and domain concepts instead of inventing something that might be cute or clever but likely will be misunderstood. This is especially problematic for non-native English speakers — we don’t know many rare and obscure words.
A “great” example of this is React codebase where they used “scry” (which means something like peeping into the future through a crystal ball) instead of “find”.
Using different words for the same concept is confusing: a person reading the code may think since the words are different then these things aren’t the same and will try to understand the difference between the two. It will also make the code less greppable, meaning it would be harder to find all usages of the same thing
Info We talk more about greppability in the Write greppable code section of the Other techniques chapter.
Tip Having a project dictionary, or even a linter, might be a good idea to avoid using different words for the same things. CSpell allows us to create a project dictionary, as well as ban certain words that shouldn’t be used. I use a similar approach for writing this book: I use Textlint terminology plugin to make sure I use the terms consistently and spell them correctly in my writing.
Most APIs and programming languages use US English, and it makes a lot of sense to use US English for naming in our project as well. Unless we’re working on a British, Canadian, or Australian project that may prefer a local language.
In any case, consistency is more important than language choice. On several projects, I’ve seen US and UK terms used interchangeably. For example, canceling
(US) and cancelling
(UK). Curiously, cancellation
is the correct spelling in both.
Some common words that are spelled differently:
US English | UK English |
---|---|
behavior | behaviour |
canceling | cancelling |
center | centre |
color | colour |
customize | customise |
favorite | favourite |
license | licence |
math | maths |
optimize | optimise |
Tip CSpell allows us to choose between US and UK English and will highlight inconsistencies in code and comments, though some words are present in both dictionaries.
Often we create pairs of variables or functions that do the opposite operations or hold values that are on the opposite ends of the range. For example, startServer
/stopServer
, or minWidth
/maxWidth
. When we see one, we expect to see the other, and we expect it to have a certain name because it either sounds natural in English (if one happened to be a native speaker) or has been used by generations of programmers before us.
Some of these common pairs are:
Term | Opposite |
---|---|
add | remove |
begin | end |
create | destroy |
enable | disable |
first | last |
get | set |
increment | decrement |
insert | delete |
lock | unlock |
minimum | maximum |
next | previous |
old | new |
open | close |
read | write |
show | hide |
start | stop |
target | source |
Typos in names and comments are very common. They don’t cause bugs most of the time but could still reduce readability a bit, and code with many typoses look sloppy. So having a spell checker in the code editor is a good idea.
Info We talk more about spell checking in the Spell checking section of the Learn your code editor chapter.
Each programming language has its own conventions and idiomatic way of doing certain things, including the way programmers spell names of variables, functions, and other symbols in the code: naming conventions.
The most popular naming conventions are:
Tip There are also lowercase, UPPERCASE, and SpoNGEcAsE, but I wouldn’t recommend them because these conventions make it hard to distinguish separate words.
Most JavaScript and TypeScript style guides suggest the following:
Tip One of the benefits of naming conventions that use an underscore (_
) or nothing to glue words together over conventions that use a dash (-
) is that we can select a full name using a double click or Alt+Shift+Left or Alt+Shift+Right hotkeys (these hotkeys expand the selection to the end of the word).
The code that doesn’t follow the established naming conventions for a particular language looks awkward for developers who are used to these conventions:
const fruits = ['Guava', 'Papaya', 'Pineapple'];
const loud_fruits = fruits.map(fruit => fruit.toUpperCase());
console.log(loud_fruits);
Now, compare it with the same code using camelCase:
const fruits = ['Guava', 'Papaya', 'Pineapple'];
const loudFruits = fruits.map(fruit => fruit.toUpperCase());
console.log(loudFruits);
However, in Python, where kebab_case is common, it looks fine:
fruits = ['Guava', 'Papaya', 'Pineapple']
loud_fruits = [fruit.upper() for fruit in fruits]
print(loud_fruits)
Also, JavaScript’s own methods, and browser APIs are all using camelCase: forEach()
, toUpperCase()
, scrollIntoView()
, and so on.
One thing that developers often disagree on is how to spell acronyms (for example, HTML) and words with unusual casing (for example, iOS). There are several approaches:
dangerouslySetInnerHTML
, WebiOS
;XMLHttpRequest
, DatePickerIOS
, HTMLHRElement
;WebIos
, HtmlHrElement
.Unfortunately, the most readable approach, normalization, seems to be the least popular. Since we can’t use spaces in names, it can be hard to separate words: WebiOS
could be read as webi os
instead of web ios
, and it takes extra time to read it right. Such names also don’t work well with code spell checkers: they mark webi
and htmlhr
as incorrect words.
The normalized spelling doesn’t have these issues: dangerouslySetInnerHtml
, WebIos
, XmlHttpRequest
, DatePickerIos
, HtmlHrElement
.
Often we end up with awkward names for intermediate values, like function parameters or function return values:
const duration = parseMs(durationSec * 1000);
// Then later we access the values like so:
console.log(duration.minutes, duration.seconds);
Here, the duration
variable is never used as a whole, only as a container for minutes
and seconds
values we use in the code. By using destructuring we could skip the intermediate variable:
const { minutes, seconds } = parseMs(durationSec * 1000);
Now, we could access minutes
and seconds
directly.
Functions with optional parameters grouped in an object are another common example:
function submitFormData(action, options) {
const form = document.createElement('form');
form.method = options.method;
form.action = action;
form.target = options.target;
if (options.parameters) {
Object.keys(options.parameters)
.map(paramName =>
hiddenInput(paramName, options.parameters[paramName])
)
.forEach(form.appendChild.bind(form));
}
document.body.appendChild(form);
form.submit();
document.body.removeChild(form);
}
Here, options
object is never used as a whole (for example, to pass it to another function), only to access separate properties in it. We could use destructuring to simplify the code:
function submitFormData(action, { method, target, parameters }) {
const form = document.createElement('form');
form.method = method;
form.action = action;
form.target = target;
if (parameters) {
Object.keys(parameters)
.map(paramName => hiddenInput(paramName, parameters[paramName]))
.forEach(form.appendChild.bind(form));
}
document.body.appendChild(form);
form.submit();
document.body.removeChild(form);
}
Here, we’ve removed the options
object, that was used in almost every line of the function body, which made it shorter and more readable.
Often we add intermediate variables to store the result of some operation before passing it somewhere else or returning it from the function. In many cases, this variable is unnecessary.
Consider this example:
const result = handleUpdateResponse(response.status);
this.setState(result);
And this one:
const data = await response.json();
return data;
In both cases, the result
, and data
variables don’t add much to the code. The names aren’t adding new information, and the code is short enough to be inlined:
this.setState(handleUpdateResponse(response.status));
Or for the second example:
return response.json();
Here’s another example:
render() {
let p = this.props;
return <BaseComponent {...p} />;
}
Here, the alias p
replaces a clear name this.props
with an obscure one. Again, inlining makes the code more readable:
render() {
return <BaseComponent {...this.props} />;
}
Destructuring could be another solution here — we’ve covered it already.
Sometimes, intermediate variables can serve as comments, explaining the data they hold, that otherwise might not be clear:
function Tip({ type, content }) {
const shouldBeWrapped = hasTextLikeOnlyChildren(content);
return (
<Flex alignItems="flex-start">
{shouldBeWrapped ? <Body type={type}>{content}</Body> : content}
</Flex>
);
}
Another good reason to use an intermediate variable is to split a long line of code into multiple lines:
const borderSvg = `<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12'><path d='M2 2h2v2H2zM4 0h2v2H4zM10 4h2v2h-2zM0 4h2v2H0zM6 0h2v2H6zM8 2h2v2H8zM8 8h2v2H8zM6 10h2v2H6zM0 6h2v2H0zM10 6h2v2h-2zM4 10h2v2H4zM2 8h2v2H2z' fill='%23000'/></svg>`;
const borderImage = `url("data:image/svg+xml,${borderSvg}")`;
We’ve talked about how to avoid number suffixes by making names more precise. Let’s talk about a few other cases where we may have clashing names, and what can we do to avoid them.
Most often I struggle with clashing names for two reasons:
const isCrocodile = isCrocodile()
).const User = (props: { user: User }) => null
).Let’s start with function return values. Consider this example:
const crocodiles = getCrocodiles({ color: 'darkolivegreen' });
Here, it’s clear which one is the function, and which one is the array with the returned from the function value. Now consider this:
const _o_0_ = isCrocodile(crocodiles[0]);
Here, our naming choices are limited:
isCrocodile
is a natural choice but clashes with the function name;crocodile
would mean that this variable holds one element of the crocodiles
array.So, what can we do about it? Not a lot:
shouldShowGreeting
);isFirstItemCrocodile
or isGreenCrocodile
);isCroc
).Unfortunately, all options are somewhat not ideal:
I usually use domain-specific names or inlining (for very simple calls used once or twice):
function UserProfile({ user }) {
const shouldShowGreeting = isCrocodile(user);
return (
<section>
{shouldShowGreeting && (
<p>Welcome back, green crocodile, the ruler of the Earth!</p>
)}
<p>Name: {user.name}</p>
<p>Age: {user.age}</p>
</section>
);
}
Here, the name describes how the value is used (domain-specific name) — to check whether we need to show a greeting, as opposed to the value itself — whether the user is a crocodile. This has another benefit: if we decide to change the condition, we don’t need to rename a variable.
For example, we could decide to greet crocodiles only in the morning:
function UserProfile({ user, date }) {
const shouldShowGreeting =
isCrocodile(user) && date.getHours() < 10;
return (
<section>
{shouldShowGreeting && (
<p>Guten Morgen, green crocodile, the ruler of the Earth!</p>
)}
<p>Name: {user.name}</p>
<p>Age: {user.age}</p>
</section>
);
}
The name still makes sense, when something like isCroc
would require a change.
Unfortunately, I don’t have a good solution for clashing React components and TypeScript types. This usually happens when we create a component to render an object or a certain type:
interface User {
name: string;
email: string;
}
export function User({ user }: { user: User }) {
return (
<p>
{user.name} ({user.email})
</p>
);
}
Though TypeScript allows us to use a type and a value with the same name in the same scope, it makes code confusing.
The only solution I see is renaming either the type or the component. I usually try to rename a component, though it requires some creativity to come up with a name that’s not confusing. For example, names like UserComponent
or UserView
would be confusing because other components don’t have these suffixes, but something like UserProfile
may work in this case:
interface User {
name: string;
email: string;
}
export function UserProfile({ user }: { user: User }) {
return (
<p>
{user.name} ({user.email})
</p>
);
}
This only matters when either the type or the component is exported and reused in other places. Local names are more forgiving since they are only used in the same file and the definition is right here.
Start thinking about:
Read other sample chapters of the book:
If you have any feedback, drop me a line at artem@sapegin.ru, @sapegin@mastodon.cloud, @iamsapegin, or open an issue.
Preorder the book now with 20% discount!