Continuing our study of the Clean Code series, we will now talk about the importance of using the right names for our variables, functions, classes, etc.
Are the names we use really that important? In one word: Yes!
Why names are important
Over time I have come to appreciate just how important the names we give something is. The name we give something shows what we perceive that thing is and how we expect it to behave. If I tell you something is a vehicle you might think about a car, bus or motorcycle, buy just the name gives you a good idea about what it is and what it can or should do. Our code works the same way
I now prefer to take a few minutes (or longer) choosing the right names for my variables, classes, etc. because choosing the right name will make my coding a lot easier.
Let’s go back to the vehicle example, what do you expect a vehicle to have? What do you expect it to do?
Many people would expect a vehicle to have at the very least a steering wheel, engine, wheels and a seat. They would also expect the vehicle to somehow be able to turn on, move, honk and some other basic things a vehicle does. Likewise, they wouldn’t expect a vehicle to brew you a cup of coffee.
What about names in our code?
Our code works the same way. The name you use for something will direct your thoughts about what information it should have and the functionality that it’s responsible for.
If you have ever written a module or class that ended up more than one thing (hello god objects!) then you know why names are important. If you have a class called CurrencyConverter then you can be pretty sure that it’s job will be receiving a value representing one currency and it returning the converted value in a different currency. You would expect it to store the conversion rate for those currencies somewhere and so on. You probably wouldn’t expect that class to send emails.
Now that we’ve established the importance of using the right names in our code, lets look at some guidelines that will help you do a better job at picking them.