Jonathan Boccara's blog

A Simple Habit to Avoid Complex Names and Typos in Code

Published December 27, 2021 - 0 Comments

Don’t you find it a little unsettling when you encounter a typo in code?

std::unordered_map<int, Value> MyClass::getInedxedValues() const
{
    // ...
}

And the code looks even more careless when that typo is repeated several times across the codebase, in code that depends on the butchered symbol:

auto const table1 = x.getInedxedValues();
auto const table2 = y.getInedxedValues();

As we’ll see below, there can be more serious problems in code, but typos don’t contribute to making the reading of code easier.

When you encounter a typo in a piece of code, it was introduced by somebody. Do you think this person had a spelling problem, or was intentionally careless about the way they typed?

Probably not. We all make typos when we type (by the way, there is — at least — one typo that sneaked into this blog post. Can you spot it?). But modern word processing applications immediately outline incorrect words, or correct them before we even finish typing them.

But when writing code, it’s different: the first time you write a symbol, the IDE has no idea whether it is correctly typed or not. So it lets typos in without a blink.

Here is a simple trick to compensate for the lack of help of our IDEs on this matter.

Don’t copy-paste the first time

As symbols get used multiple times in code, we have to write the same terms over and over when we create new code. For longer names, we copy and paste them without even thinking about it, in order to be more efficient.

In the opening example of this post, if the typo propagated across the codebase unnoticed by its author, it is probably because the author mistyped it the first time (which can happen to everyone), and then copy-pasted it to other locations, bringing the typo along.

Therefore, a simple technique to avoid typos spreading without us noticing is to avoid copy-pasting symbols the first time we need them after creating them.

Indeed, if we manually rewrite a symbol a second time, we get another chance to get it right. And if we do, the compiler won’t let the code pass, because it will notice the mismatch between the incorrectly typed symbol and the correctly typed one. This is spell-check, by the compiler.

In theory, you could type in the same spelling error twice. But the chances of this happening are much lower than typing it one time. Therefore, after typing out the term two times, it sounds raesonable to use copy-pasting for the following usages.

If you can’t type a term twice, it means that it has a problem

While using this trick to avoid typos in my code, I realized it has another benefit, arguably more beneficial than the mere correct spelling of symbols.

If you do refrain from copy-pasting the first time you need a term you wrote, and try to type it out again, it happens sometimes that you just can’t remember exactly what that term was.

If it is the case, this suggests that this name is too complex and that it’s worth spending a bit of time to make it more rememberable, and therefore simpler.

If a term is so unnatural that you can’t remember it after a few seconds or minutes after writing it yourself, chances are it will look totally alien to another reader, or even to you in the future.

Not copy-pasting the first time is thus also a way to check if the terms you create are expressive.

To be effective, it has to become a habit. Next time you write a new term in code (soon, hopefully) don’t copy-paste it the first time you need it again.

At the beginning it feels like more work, but you will quickly get used to it, especially by thinking that it will increase the quality of your code.

Found the typo yet?

You might also like

Don't want to miss out ? Follow:   twitterlinkedinrss
Share this post!Facebooktwitterlinkedin