DEV Community

Cover image for Variable Naming Formats & Usecases
Osazuwa J. Agbonze
Osazuwa J. Agbonze

Posted on

Variable Naming Formats & Usecases

Have you ever forgotten the name of a variable you declared and/or initialized ? Well, you're not alone. Read on to see how this could be reduced.

Just as data is important to an application so is how the data is stored. Variable temporarily stores data that can be reused/changed in a program.

So, what are Variable Name Formats ?

Variable name formats are simply naming conventions that are used to compose a variable name.  It should be noted that

  • Variable names cannot begin with digits but can comprise of it. 
  • Variable names cannot contain symbols except _ (underscores)
  • Variable names cannot be keywords e.g if , else , while, for e.t.c.

let’s consider a scenario to drive every points home.

in Snake 🐍 game, a player’s available life is reduced by 1 when life reducing event occurs. The game should continue until a player's life becomes 0

What data can you deduced from above scenario ???

.....

Alright, here are mine ( same/close to what you came up with, right ? )

  • Players available life data (Numeric)
  • Game playing state data (Boolean)

In this post Game playing state data (Boolean) will be considered as constant value and Player available life data (Numeric) as non constant value

🔔
This post uses Python programming language for all it's usecases, but all usecases can be replicated in other programming languages.

Variable Naming Conventions

Below are the common variable naming conventions and usecases

1. Uppercase variable naming convention

This naming convention is used for variable names that holds constant values in an application. Constant values are constant (pardon the repetition), meaning that they don’t change in an application. This naming convention could also be used for global variables.

example

# usecase 1
GAMEPLAYINGSTATE = True

# usecase 2
GAMESTATE = True
Enter fullscreen mode Exit fullscreen mode

The two names are nice and doesn't go against naming rules, but if we’re to follow python zen which says simple is better than complex then we’ll find that usecase 2 from above names wins. From above code example, variable names are all in uppercase, which is why the variable naming format is termed UPPERCASE

2. Lowercase variable naming convention

This naming convention is used for variables that store values that changes in a program. When a variable utilizes this convention, then the variable names will only be in lowercase.

example

# usecase 1
playeravailablelifedata = 5

# usecase 2
playerlife = 5
Enter fullscreen mode Exit fullscreen mode


 

Pain point of UPPERCASE and lowercase naming conventions

Variables could becomes very difficult to read when it’s all in uppercase or lowercase.

There have to be a way to keep the conventions and as well have a readable variable name. 

3. Snakecase variable naming convention

This naming convention lifts the pains of using all UPPERCASE or lowercase naming conventions.

As snake_case naming convention can fit into UPPERCASE and lowercase, we’ll see an example for both

example

# usecase 1 (uppercase convention)
GAME_PLAYING_STATE = True

# usecase 2 (uppercase convention)
GAME_STATE = True

# usecase 1 (lowercase convention)
player_available_life_data = 5

# usecase 2 (lowercase convention)
player_life = 5
Enter fullscreen mode Exit fullscreen mode

From above code, it’s seen that snake_case naming convention just includes an underscore between each words which in turn makes them more visible and readable. 

4. Camelcase variable naming convention

🔔
This variable naming convention doesn’t lift the pain point of using all UPPERCASE or lowercase naming convention. It’s a naming convention. 

This variable naming convention is used for variable names that stores value that can change in an application just like lowercase naming convention.

The reason it’s termed camelCase is because the beginning of every word after the first word is capitalized, making it look like the hump of a camel. 

example

# usecase 1 (camelcase convention)
playerAvailableLifeData = 5

# usecase 2 (camelcase convention)
playerLife = 5
Enter fullscreen mode Exit fullscreen mode

🏳 In Conclusion

  • Use UPPERCASE variable naming convention for constant value or for global variable e.g API_URL, GAME_STATE e.t.c.

  • For readability sakes, use camelCase for variable names or lift the pain point of using all lowercase convention by using snake_case.

  • Variable name should be kept short if possible without losing it's meaning - it helps in remembrance.

  • In a codebase choose a convention to use and stick to it. 

  • Variable name formats are simply naming conventions that are used to compose a variable name.

  • Variable name cannot begin with digits but can comprise of it.

  • Variable names cannot be keywords.

  • Variable names currently doesn't support symbols except for _ (underscores).

Do you use any other naming convention not mentioned, or did i miss anything ? If so, I'll love to know, kindly leave a comment below.
I’d be very grateful if you’d help it spread by emailing it to a friend or sharing it on Twitter or Facebook. Thank you!

Feel free to connect on Github, Twitter

Top comments (0)