Auto-Complete Naming Convention. How to give better names to things in…
By: Yossi Yaari
Tags: Code, Naming Conventions

Auto-Complete Naming Convention

How to give items in your code more meaningful names, by referring to the context in which they are used. Yossi Yaari Follow Dec 23, 2020 · 3 min read

There are only two hard things in Computer Science: cache invalidation and naming things. — Phil Karlton

Remember Hungarian notation?

In the days before intellisense, some developers used name encoding that added a variable’s type as a prefix to the variable name.

A boolean for feature enabled would be bFeatureEnabled, and the number of items would be u16NumberOfItems for uint.

This convention has been pretty much abandoned since the wide use of smart IDEs.

It seems like we have evolved into a new kind of Hungarian notation. The Auto-Complete Naming convention.

When your class has a text field for the instance name, you can call the field “name”, and the Auto-Complete can generate a getter named “getName”. And that’s great and it’s clear. There’s rarely anything you would need to add to this method name to explain it’s function or meaning. And you achieved it with minimal effort, as the IDE helped you out.

When you have a class named SomethingThatGoesBumpInTheNight, your instance will invariably be generated as somethingThatGoesBumpInTheNight, and not scaringLittleTimmy. Here is a short example to illustrate a common situation where Auto-Complete Naming (ACN) naming makes sense.

Simple Auto-Complete Naming example

As you can see in the example, BankAccount and BusinessTransaction both have instances with the exact same name as the class name. And the code is clear enough.

In most cases, this can work. But, making this a developer reflex can sometimes lead to code that is hard to understand, as there can be a lack of explicit intent.

The following example can demonstrate some of the lost intent I refer to.

Auto-Complete Naming can lose contextual meaning

In this example, we have a bank account that is used for deferred VAT payments. But the use of ACN convention hides some of the intent of the members and variables.

If we look at the sell() method body, we have little understanding of the nature of the differedPayments field.

Of course, if we were to add a second deferred payments field for a different purpose, this naming issue would be apparent, and no one would accept the following additions to the previous code:

Auto-Complete-Naming failure when adding a second instance of the same type

Explicit Intent Naming Strategy

I hope that by now, you are convinced why you should use a name that contains the intent of the specific instance. Using a name that conveys the contextual meaning and usage of the specific instance will be valuable the next time the code is read by a human.

An explicit intent naming strategy could result in extremely long names if we were to merely append or prepend the intent to the ACN convention. In my opinion, that would not lead to an easily readable code. A variable named businessExpensesBankAccountForDeferredPayments is a nice example of something that is too long.

So I suggest using names that convey the intent but do so in the shortest way. In our example, it might be something like this:

Explicit Intent Name example

The account in use is for business expenses, and now the variable name states that.

The payments are for VAT, and the variable name states that. If the developer needs to know that the VAT payments are deferred, then you would probably need to add that to the field name (vatDeferredPayments).

Also, we now understand that the business transaction passed to sell() is a sale, even if there is no specific type for that kind of transaction, or if the method name was less descriptive (e.g. when implementing an interface).

thisIsTheEnd

To wrap it up, we can conclude that using Auto-Complete Naming for your variables and other code entities may pass the full intent in some cases, but not always.

You should always check for explicit intent in your code. Check where you can change the names generated by Auto-Complete Naming so that you don’t lose your intent. Just think - the next confused reader might be you.