To be a professional programmer, there’re many things that we’ve to do to be a good one.
In this article, we’ll look at the most basic requirement to be a good programmer, which is to clean up after ourselves by naming things better.
Writing Clean Code
Boy scouts are always told to leave their campground cleaner than they’ve found it.
We can apply the same principle to code. We leave them cleaner than we found it.
This way, we’ll always have a clean code that is pleasant to read and work with.
Naming is the start of writing code that’s easy to work with.
Using Intention-Revealing Names
We can start cleaning up code by naming things in an intention-revealing manner. Names should be descriptive enough that we can understand it by reading its name.
For instance, instead of writing:
let x;
We write:
let elapsedTimeInMinutes;
There’s a big difference in both names.
We know what elapsedTimeInMinutes
means right away, but we’ve no clue with x
means.
Therefore, we should name things in ways that we understand. The code should tell us what it’s doing explicitly rather than doing everything implicitly.
We don’t know what x
so we don’t know what it holds.
Likewise, if we have a function like:
const getItems = () => {
const list = [];
for (const a of arr) {
if (a % 2 === 0){
list.push(arr)
}
}
return list;
}
We don’t know what list
and arr
have.
Therefore, we just don’t know what it’s trying to do, even though the spacing and indentation are good.
No Disinformation
We should never leave false clues with our names. For instance, we should only prefix our variables with num
if it’s actually a number.
Also, we should keep our spelling consistent so that there’s no chance that we can mislead people.
Meaningful Distinctions
We should name our things in our programs so that we can distinguish between them. Otherwise, we’ll be confused by things with similar names but act differently.
For instance, if we have 2 variable names called productInfo
and productData
, then they’re too similar. If they’re different, then they should have more distinguishable names.
Use Pronounceable Names
Pronounceable names should be used so that we can actually remember them and talk about them.
If we can’t pronounce the names, then it’s hard for us to discuss them.
Use Searchable Names
Names should be searchable so that we can find them later. Therefore, we should use descriptive names as we mentioned before.
Also, we should avoid magic values and assign them to named constants. This way, we can find them easily and only have to change them in one place.
Member Prefixes
We don’t need member prefixes since we can tell that they’re members of a class or object by their location.
For instance, instead of writing:
const obj = {
mName: 'joe',
//...
};
We can just write:
const obj = {
name: 'joe',
//...
};
Mental Mapping
We shouldn’t make our brains more work when we don’t have to.
Therefore, we should just write out everything clearly so that we don’t have to map them with our brains.
Class Names
Class names should be noun phrases like Car
or Dog
.
Method Names
Method names should be a verb or verb phrases like save
or speak.
One Word per Concept
We should have one word per concept instead of having synonyms.
For instance, we can use fetch
instead of using fetch
, retrieve
or get
.
Context
We should add some context to our names so that we can understand why they’re named the way they are.
For instance, if something is part of the address, then we should prefix them with something that indicates that it’s so.
So instead of writing state
, we can write addrState
so we’re sure that we refer to the state in the address instead of some other state.
Conclusion
We can make our code clearer by naming things properly.
Names should be descriptive, easy to pronounce, and consistent.
Also, we should give some context in the names so that we won’t be confused by them.
Disinformation should be corrected also to avoid confusion.