How const may Confuse Developers

Let’s learn how const can be confusing for developers.

Description

We may often hear that const and its excessive use confuses developers.

Let’s think about this from a different perspective…

First, let’s focus on where const is used.

It is important to keep in mind that:

  • const does not introduce an overly complex structure that is often referred to as smart code.

  • const reveals the intentions of the developers both to the reader and to the compiler.

Relevant choices when using const

When we decide to use const in our code wherever it makes sense, we must make a choice:

  • Will we use const only in the code we write?
  • Will we go in and update all the existing codebase?

In most corporate circumstances, the second option is not viable. We can only focus on the code we add and edit. Updating the codebase also means that it will lose its consistency in using const.

The lack of const or any similar keyword can be because of the following reasons:

  • The author of the code omitted it on purpose.
  • The author did not use it because they are not used to const.

We can infer the reason behind their choice from the surrounding code. . If const is widely used, we can assume that it was omitted on purpose. On the other hand, if it barely appears in our code, its lack has no purpose.

When we start using it in an existing codebase where it was not used before, we lose the ability to infer the intention behind its lack.

Get hands-on with 1400+ tech skills courses.