Naming things in programming:
How to do it
When you develop, you spend a lot of time writing code, but did you know you spent even more reading it? This fact has been proven, so to minimize the amount of time spent on reading the code, developers should take care of naming.
We can easily make an analogy between writing a code and writing a text. When the text is wrong or misspelled, it is difficult for the reader to read it and understand. The same goes for the code: The code design can be complicated and tiring to understand. In addition, bugs are much easier to hide there. However, by using the appropriate names, you can make your code more correct and, above all, readable for you and other developers who will work with it in the future.
Go higher than the
body one level of abstraction
On the one hand, for code to be easy to read, it must be well-structured and well-presented. On the other hand, the names of variables and functions should be chosen with care. Before giving a name, think about the responsibility of this piece of code. When introducing variables into the code, it is worth remembering that its name suggests the purpose of the variable but not be so detailed as to indicate possible values. For example, if your variable is a measurement unit, you might want to use a unit instead of kilometers_or_miles.
The name should
describe the responsibilities of the class
Class names are an exception to the rule of naming things at one level of abstraction higher than their content. The name of the class should indicate its current purpose and assumptions, and at the same time, be legible. Therefore, it is worth avoiding names that may cause confusion. For example, the high-rise will not become a small house, so choosing a skyscraper is better than the name building.
One name for one
variable
When choosing variable names, be consistent and stick to the rule that each module or class should only be responsible for one thing. Otherwise, you risk getting lost in the names and therefore introduce unnecessary errors in your code. For example, as a rule, a mug has only one responsibility: to be an object for liquids. So if you need to create a moving cup, create a general Cup class and a separate CupCarrier for a movable mug.
Break the problems
down into smaller pieces
One of the most effective techniques for helping to build a better architecture is breaking down each part of the task into small pieces. There is no doubt that good architecture goes hand in hand with good nomenclature. The more minor elements are easy to name, and their purpose is much easier to determine.
Caring for the readability of the code is not just a fad. We often think we’re wasting a lot of time watching for our code and constructing it properly, but the truth is that neat code means that it can be reread faster. Moreover, it’s easier to find and fix bugs. In addition, paying a little attention to abstractions, hierarchies, and respecting the single responsibility rule, you will speed up work on the code in the future. If you need to find out more, check out this Programmer’s guide: Naming 101: How to Name Things