The lonely constant

Pragmatic Maciej - Jan 28 '21 - - Dev Community

All software projects have differences, but there are also things which never change, and one of them are utils - where we put all staff we don't know where to put, and constants file where we put all constants which we don't know where to put. There are also many variations of above, but still you will always find such place, or places in every project.

Among these constants we can find so-called "lonely constants".

The lonely message

In some famous constants file there is a lonely constant.

const MESSAGE_TITLE = "Welcome to the 'Some Site'"
Enter fullscreen mode Exit fullscreen mode

After some codebase search looks like constant is used only in one module of the application. Why is it then shared and not inside the module? I assume the author or A) thought somebody will need it, B) author didn't know where to put this constant. No matter what was the reason, constant which belongs to one specific domain of the application, should be kept there and not be exposed. As I am not a fan of telling people about magical programming patterns or principles, you need to forgive me for this one. This situation is exactly violation of "You aren't gonna need it" principle.

The lonely cutter

In some famous constants file there is a lonely constant.

const CUTTER = 'cutter'
Enter fullscreen mode Exit fullscreen mode

After some codebase search looks like constant is used cross the whole app. It is not a local thing, as in our previous example. This is properly made global, but after some while you see that there is something more, another constant used close to this one.

const BATTLESHIP = 'battleship'
Enter fullscreen mode Exit fullscreen mode

Now the code which uses them:

if (ship.kind === BATTLESHIP) {
 ...
} else if (ship.kind === CUTTER) {
 ...
} else {
 ...
}
Enter fullscreen mode Exit fullscreen mode

Now it should be clear. We have two constants, which are tightly connected, and even more, they are one thing - type of vessel.

enum VESSEL_TYPE {
  BATTLESHIP,
  CUTTER,
  OTHER
}
Enter fullscreen mode Exit fullscreen mode

The very dry digit

In some module there is a lonely constant.

const TWO = 2
Enter fullscreen mode Exit fullscreen mode

It is a local constant of the module, so at least it is not exposed. After code investigation, it was clear that the reason why it was a constant, was avoiding repetition. Developer have seen few '2' in the code, and as was taught to do, followed the DRY principle, and instead of repetition of '2', code had repetition of 'TWO' 😅, what a win...

In the code the incredible 'TWO' was used as number of digits after dot. So instead of naming it as just value synonymous, lets use name which defines the purpose.

const DECIMAL_PART = 2
Enter fullscreen mode Exit fullscreen mode

Don't be so lonely my constant

To not make constants lonely, we need to follow common sense in naming, sharing and grouping of constants. If we will not share local things, if we will put proper names and if we will gather constants in domain enums/objects, then no constant will be lonely in our codebase.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player