Clean Code - Variables - Beau teaches JavaScript
Using Meaningful and Pronounceable Variable Names
The first thing to do when it comes to making your variables more readable, reusable, and refactored is to use meaningful and pronounceable variable names. As seen in the example, using variable names like `y y y y MMS DST` can be confusing and hard to understand. Instead, we should use variable names that are descriptive and easy to pronounce, such as `year month day`. This makes it easier for other developers to understand the code and also reduces the chances of errors.
Using ES6 Constants When Variable Values Do Not Change
Another important aspect of writing clean and readable code is using ES6 constants when variable values do not change. In the example, we have a variable named `first US president` which always has the value `George Washington`. Using ES6 constants in this case would make the code more concise and easier to read. Instead of having `VAR first US president = George Washington`, we can use `const firstUSPresident = "George Washington"`. This makes it clear that the value will not change, and also reduces the chances of errors.
Using the Same Vocabulary for the Same Type of Variable
When it comes to writing clean and readable code, it's also important to use the same vocabulary for the same type of variable. For example, when we have functions or variables like `get user info`, `get client data`, and `get customer record`, it can be confusing to keep using different words. Instead, we should use the same word consistently throughout the codebase. In this case, we can just use `get user` in every part of the program.
Searchable Names
When writing code, it's also important to consider the readability aspect from a search perspective. When we see an unfamiliar variable name, it's not clear what value that variable represents. To make our code more readable and searchable, we should put meaningful values next to our variables as shown in this example:
`VAR i = 525600`
By putting `i` next to `525600`, we can clearly understand the significance of the value.
Using Searchable Names
Another benefit of using searchable names is that we don't have to remember what some values represent. We should make sure to put our variables in a way where they are easy to search and understand for other developers who might be reading or maintaining our codebase. So, putting the variable name `year month day` next to `VAR y y y y MMS DST` would make it easier for others to find that information.
Explanatory Variables
When we have complex logic in our code that is hard to understand at first glance, using explanatory variables can help clarify things. For example, if we see this variable:
`const city state reject match citystate reject`
It's clear what each part of the variable represents. However, it would be more readable and easier to understand if we had an explanatory variable like `VAR cityStateMatch`. This makes our code easier for other developers to read and understand.
Avoiding Unneeded Context
When writing code, there are times when we might add context that is not necessary to our variables or functions. For example:
`var car make Honda car car model Accord car color blue`
Here, we have repeated `car`. It's clear what `car` means, so adding another instance of `car` doesn't provide any additional information. When it does, however, like in the case where we are trying to paint a car, we should call it by its name for clarity and readability.
The Benefits of Short Circuiting
Short circuiting is another technique that can make our code more readable and efficient. By comparing `i` with `525600`, we see that we have added unnecessary repetition in the loop. Instead of `for (VAR i = 0; i < 525600; i++)`, we could use short circuiting to get around this. The function would check if `i` is less than `525600`, and only then execute the rest of the code.
Here's an example:
```
function createMicroBrewery(name) {
var breweryName = name;
var breweryType;
if (name) {
breweryName = name;
} else {
breweryName = "hipster Brew Company";
}
return { breweryName, breweryType };
}
```
Short circuiting is a cleaner alternative to using long conditionals or nested if statements.