How to give a good name to a variable in a program

Source: Internet
Author: User

Novice programmers always spend a lot of time in learning programming languages, learning grammar, technology and development tools, and they think that if mastered these are a good programmer. However, the actual programming is not only to master the technology and tools, the key is to give a solution to a particular area of the problem, and usually to work with other programmers to complete. Therefore, it is important that programmers use code to express their ideas accurately, so that others can understand the meaning of the program.

In clean code, the programmer, Robert C. Martin, said, "The use of annotations is to compensate for our lack of coding." This means that your code is not good enough if you need to add a comment to your code. At the same time, it also shows that if you cannot demonstrate your understanding of a problem or algorithm in a simple code, you can only rely on some comments to illustrate your thoughts, not just the code. Good code allows people to understand and understand without comments, as well as to make all the necessary information visible in the code.

In programming theory, there is a concept called "self-describing source code", especially in environments where there are more loosely named rules. The introduction of this concept is to increase the readability of the code, making it easier to maintain and extend the code.

Below I will compare some "good code" and "unclear code".

To show your intentions when naming

How to name in code has always been a problem, with some programmers using simplified, short or coded names so that only they can understand them. See some examples:

Bad code:
int D; Elapsed time in days
int ds;
int DSM;
int faid;

The name "D" can represent anything, the programmer can only use comments to indicate his intentions, not only in the code display. and "Faid" can easily lead to misunderstanding as ID.

Clear code:

int elapsedtimeindays;
int dayssincecreation;
int dayssincemodification;
int fileageindays;

Avoid the naming that will be misunderstood

The information that leads to misunderstanding is worse than no information, some programmers like to "hide" some important information, but worse, they sometimes write misleading code.

Bad code:
Customer[] customerlist;
Table thetable;

The variable "customerlist" is actually not a list. It's just an ordinary Array (collection). In the second line, "Thetable" is the object of a table class, and the word "the" is an unnecessary distraction.

Clear code:
Customer[] Customers;
Table customers;

Suitable name length

In modern programming languages, long variable names are allowed, and you can name them virtually unrestricted, but this can lead to confusing naming.

Bad code:
var thecustomerslistwithallcustomersincludedwithoutfiler;
var list;

A good name should contain enough words to express the meaning, but any unnecessary words will make the name longer and become incomprehensible. The shorter the name, the better, the premise is to be able to express the full meaning in the context.

Clear code:
var allcustomers;
var Customersinorder;

Conforms to the code specification, which can better help understand

All programming languages have their own "style", called notation. Programmers should write code that conforms to this notation, because other programmers know this and write programs in this style. Let's look at a bad code example that doesn't conform to the notation. The following code does not follow any code standards (such as Pascalcase, CamelCase, Hungarian specifications). To make it worse, there is a meaningless change to the bool variable, which is a verb describing the action, but the bool value here should represent a state, so the variable should be named with an adjective.

Bad code:
const int maxcount = 1;
BOOL Change = true;

public interface Repository;
private string Name;
public class personaddress;

void Getallorders ();

Because of the code specification, when you look at only a subset of the code, you can understand the variable type and meaning of the face, for example, you see a variable "_name", you can know that this is a private variable in the current class. Therefore, any code you write must conform to the specification.

Clear code:
const int MAXCOUNT = 1;
bool ischanged = true;

public interface IRepository;
private string _name;
public class personaddress;

void Getallorders ();

A concept should not be expressed in multiple words, and a word should not be expressed in multiple concepts

It is difficult to define concepts in a scene, and in software development, programmers spend a lot of time analyzing a scene and naming various elements in the scene, which is always a headache for programmers.

Bad code:

1.

void Loadsingledata ();
void Fetchdatafiltered ();
void Getealldata ();

2.
void Setdatatoview ();
void Setobjectvalue (int value);

In the first piece of code, the programmer wanted to express the concept of "get data", but he used a lot of different words "load", "Fetch", "get". In a scene, the concept should be expressed in a uniform word. In the second paragraph of the Code, the word "set" is used as two concepts, the first is "take out the data display", the second is "assign a value to an object", the two different concepts should be expressed in different words.

Clear code:

1.

void Getsingledata ();
void Getdatafiltered ();
void Getalldata ();

2.

void Loaddatatoview ();
void Setobjectvalue (int value);

Use a meaningful name in a field context

All of the code written by the programmer is related to the background of a particular field, and in order to make the written code more understandable, it is best to use the name in the context of that domain.

Bad code:

public class Entitiesrelation

{
Entity O1;

Entity O2;

}

When you are writing code for a field, you should use a name that is relevant to the domain background. If someone else (not just a programmer, perhaps a tester) touches your code in the future, he can easily understand your background-related code. So, the first thing programmers should consider is the domain background problem, then how to draw the solution.

Clear code:

public class Productwithcategory

{
Entity product;

Entity category;

}

Use a meaningful name in context

The name in the code has its own context, and the context is important for understanding a code because it provides additional information. Let's look at a typical "address" context:

Bad code:

String addresscity;

String Addresshomenumber;

String Addresspostcode;

In most cases, the "ZIP Code" (postcode) is part of the address, and it is clear that the ZIP code cannot be used alone (unless you are developing an application that specializes in postal code). Therefore, there is no need to add "address" in front of "postcode". Moreover, all of this information should have a context, in object-oriented programming, where an "address" class should be used to express this information.

Clear code:

Class Address

{

String City;

String Homenumber;

String postcode;

}

Summarize

To summarize, as a programmer, you should:

1. The name is meaningful and can be expressed as a concept;

2. To consider the length of the name, only the necessary information in the name;

3. Conform to the "coding specification" to help understand;

4. A concept should not be mixed with multiple names;

5. Use names that are meaningful in the context and context.

How to give a good name to a variable in a program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.