Sign in Registration

How to quickly come up with the correct names for variables, functions, classes, properties, methods, etc. in programming?

When writing program code, it is often necessary to come up with many different names for all kinds of entities in programming (examples in this article will be in PHP), be it variables, functions, classes, properties, methods, etc. (hereinafter simply "variable"). This applies to a wide variety of programming languages, because the correct names will allow you to read and edit the code much more productively.


Beginner developers often ask how to start programming correctly. They often name variables in a bad way, often they are just meaningless names, because of which the readability of the code significantly decreases. As the developer gains experience, it becomes clear that he needs to carefully design his code - to come up with the right names for his variables. Coming up with the right names can take time, but with experience a programmer can do it much faster.

In textbooks and various courses, variables look simple and without much meaning, so a beginner programmer can learn such bad practice and write variables like this.

For example, in PHP it can write like this:

- variables like $ a =, $ b = 1;

- functions as function get ($ a) {…}, function set ($ b) {…};

- class names class a {...}, class b {...}, etc.

Obviously, the code will be incomprehensible, because the naming is meaningless. You should also avoid abstract naming conventions like $ data, $ value, etc. Names variables must be thought of with meaning so that a specific element in the code strictly denotes something. The names should not be too long or short, it is best to use English meanings, since programming is based on a foreign language. Using transcription when writing code is not the best option.

How to learn to come up with correct names on the fly? You can use a phased approach:

- First, you need to think about what the created variable will mean and whether it is needed at all. It often happens that a variable can be easily dispensed with;

- If a variable is needed and its purpose is invented, you can mentally choose 2-3 suitable words in your native language. Then recall or see analogs in English language ;

- When words are received, you should opt for a more appropriate word - such that it is easy to read and the code looks concise.

When the name of the variable is invented, you can start writing it in code. But often the variables must denote the value to be composed, that is, one word in the name of the variable is indispensable.

It is important to understand that variable naming can be different for different programming paradigms, for example, the object-oriented style makes it much easier to write names. The procedural style requires coming up with prefixed names, variable suffixes, etc.

For example, you need to come up with a name for a variable that will denote an object and its varieties. For a procedural-style variable that will represent a student, you can think of the word "learner", in English you can use the word "learner". But usually you need to not only come up with one variable, but also similar variables. It could be:

- $ learner_id - learner ID,

- $ learner_name - student name,

- $ learner_age - learner's age, etc.

In object-oriented programming (OOP), this task is easier to solve, the learner object is created and its properties are used:

- $ learner- & gt; id;

- $ learner- & gt; name;

- $ learner- & gt; age etc.

It is also important to adhere to the used in the project coding style . If the code is written from scratch, then it is necessary to decide in advance which style will be used and apply it to all variables. You shouldn't mix different styles, for example, so that the variables don't look like this:

- $ learnerId,

- $ learnerAGE,

- $ learner_Name etc.

Better to use fully qualified names and not use abbreviations like:

- $ learn_id,

- $ learn_ag, etc.

Each language has its own pros and cons including PHP. Although in PHP itself there are abbreviated names of functions and variables (there are abbreviations even on the keyboard), such abbreviations are generally accepted and appropriate for narrow fields of application.

Thus, the quality of the program code directly depends on many parameters, the most important of which is choosing a programming paradigm and correct naming of variables . The article mainly covers examples for variables, but the same rules are true for other entities.

Comments (0)
For commenting sign in or register.

Latest articles

Popular sections

Eqsash (Tools)

Android app - VK LAST USER ID, отучитель от зависимости и т.д.:
Available on Google Play

Amessage (Communication)

Login to the web version
Android app:
Available on Google Play

Share this

Subscribe to



IT notes - In simple language about the most necessary things (HTML, CSS, JavaScript, PHP, databases, Drupal, Bitrix, SEO, domains, security and more), PDF, 500 p.