Variable Names: Avoiding Conjunctions

Using conjunctions in your variable names shows we are attempting to store too much data in a single variable. The English language defines conjunction as a word (e.g. words like and, or, but) that connects two clauses into the single phrase. If you spot a conjunction in a variable name, it should raise a red flag that something is wrong. Let's look at an example where a variable uses a conjunction.


# Bad example
score_and_player_count = { score: 100, player_count: 2 }

Here we have a variable that contains a hash with two data points: current score and player count. While the name score_and_player_count is descriptive, the word "and" shows that the variable is holding data for two different purposes. This means we are trying to do too much with one variable which will lead to problems.

If we added more key/value pairs to the hash, the variable name might need to change, so it continues to describe the data. Changing a variable name because its data changed is bad. If the variable name changes, any places in the code that references the variable will need to change. This workflow will lead to bugs and extra work.

Also, as the variable name grows in size, it will become harder to read. Let's look at a better approach.


# Good example
score = 100
player_count = 2

Here we split our single variable into two. Each variable only holds a single data point, and the names show this. Both variables are short and easy to read. Now that each variable holds a specific data point, future developers will be less likely to append unrelated data to it. Since the variables contain precise data, it's also unlikely we will need to change the name.