TechNews

How to name things in programming?

Naming is an activity that most of us associate with marketing. However, proper naming plays a significant role in many other sectors, including programming. And just as everywhere else, naming in programming has but one purpose–to make your work more effective and straightforward. Today, we are going to take a closer look at naming in programming.

Now, what’s the reason we decided to write this post? As the experience of hundreds of software and web developers shows, the majority of work is usually related to reading the code, not writing it. Therefore, it would be helpful if you could limit the time spent on this task to a minimum. One of the best ways to do this is to master naming in programming.

You see, it doesn’t really matter whether you work on an object, method, class, or any other piece of code. If it’s not named properly, you (or any web developer working on the same project for that matter) will have a hard time reading and understanding it. In other words, by naming specific sections of code properly, you facilitate your future work, which allows you to save a lot of time. Thanks to proper, transparent naming, your code is clean and easy to understand; in a word – better.

How to name elements of the code?

Now, we are going to talk about two significant elements of code:

  • Methods and variables
  • Classes

METHODS AND VARIABLES

In the first situation, things are quite straightforward. There is one rule of the thumb: The method/variable should be named at one level of abstraction higher than its values. This way, it’s still useful when these values change. You should concentrate on the purpose of the variable, but not its possible values. In order to devise this higher level of abstraction, you have to think primarily about the variable’s purpose. Knowing the specific purpose of this particular chunk of code, you can come up with a general, transparent name.

You ought to find the happy medium here. Your name can’t be too detailed nor too vague.

CLASS NAMES

Now, let’s concentrate on class names. Here, the situation is slightly different. In short, you have to be more specific. The class itself is unlikely to change in the future, so you don’t need to think about what it can or cannot become in the future. Present assumptions are perfectly sufficient. Therefore, suppose that you need a class code that describes a car, maybe for some car manufacturer’s mobile app.

These companies are unlikely to start making planes all of a sudden. We can expect that in the foreseeable future, it will still be a car. Therefore there’s no need to name your class “vehicle” or “product”. “Car” is both sufficient and precise.

It’s the same story with so-called child classes. After all, our manufacturer makes more than one car type, correct? There are pickup trucks, coupes, sedans, and SUVs. These names are perfect for customers and your code alike.

Of course, all of that is true given that you follow one of the basic rules of programming, which says that every module or a class should be responsible for only one thing. It will be extremely difficult to devise a solid, transparent name for your class if you ignore this rule, and things will quickly get confusing.

To sum up quickly, how should you name things in programming?

  • Always think about the purpose first
  • Go one level of abstraction higher than a body in case of methods and variables
  • Be as specific as possible when it comes to class names

For more information, read this Programmer’s Guide on How to Name Things.

Shares: