Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write Code in programming Development

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to write code in programming development. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. Identifiers (naming rules)

The identifier should be intuitive and spellable, and it is expected to know the meaning. * English words or their combinations are used to facilitate memory and reading, and do not use Hanyu pinyin to name them. Long names can better express meaning, so it is not surprising that function names, variable names, and class names are more than ten characters long, such as:

Good named int student_age,teacher_age;, bad named int age1,age2

But is the name as long as possible? No, take a look at the following example:

Struct student {int student_age; / * Bad naming * / char * student_name;} struct student {int age; / * good naming * / char * name;}

Why is the former not good? because a lot of the structure's name student has already expressed the meaning of student in front of student_age. Another example is the string copy function: void StringCopy (char * str1, char * str2); it's hard to know whether to copy str1 into str2 or just the other way around. You can make the parameter names more meaningful, such as strSource and trDestination. So you can see from the name that strSource should be copied to strDestination. Single-character names are also useful, such as the common ones such as iPeragenjMagol, krecovermrecovernrecoverxreyrez, and so on, which can usually be used as local variables within functions.

two。 Precedence of operator

If there are too many operators in the line of code, you should use parentheses to determine the order of operation of the expression and avoid using the default priority. Because it is difficult to memorize the precedence of each operator, even if you memorize it and use it correctly, the written code can easily be ambiguous and make it less readable.

Good style if ((a | b) & & (a & c)) Bad style if (a | b & & a & c)

Although the latter has the same function as the former, the latter is scary and difficult to read.

3. Don't write complex compound expressions.

The use of compound expressions in appropriate situations can make the code more concise, but it should not lead to the complexity of understanding.

For example:

Max = a > b? (a > c? A: C): (B > c? B: C) / / the compound expression is too complex

It should be modified to:

Max = a; if (max < b) {max = b;} if (max < c) {max = c;}

The above if execution statement only one line also added {}, because it follows the "regardless of the number of if, for, while execution statements should be added {}" rule, which can prevent writing errors, when such statements are nested layer by layer, you will know the benefits of doing so.

4. Comparison of various data types and zero values

In JAVA, for the Boolean variable flag, the way to compare to a zero value (note: not 0) is naturally if (flag== TRUE) or if (flag== FALSE), but this is not the right choice in Cmax Cure +. The correct choice should be if (flag) or if (! flag), because there is no uniform standard for what the value of TRUE is. For example, Visual C++ defines TRUE as 1, while Visual Basic defines TRUE as-1. If (flag = = TRUE), if (flag = = 1), if (flag = = FALSE) and if (flag = = 0) all belong to bad styles.

Integer variables should be compared directly to 0 with "=" or "!".

If (value = = 0) if (value! = 0)

It can't be written as

If (value) / / misleading that value is a Boolean variable if (! value)

The zero value of the pointer variable is NULL. Although the value of NULL is the same as 0, the two have different meanings. For the pointer variable p, the if statement that compares it to zero is as follows:

If (p = = NULL) if (p! = NULL)

Don't write it as

If (p = 0) / / it is easy to misunderstand that p is an integer variable if (p! = 0)

5. Multi-tier if statement

Don't have such a structure:

If (condition1) {… If (condition2) … If (condition3) … ... }

Instead, it should be replaced by an if-else-if structure:

If (condition1) {… } else if (condition2) {… } else if (condition3) {… }...

With such a well-organized structure, the former can easily lead to the fact that you don't know what you wrote later. You can replace nested if statements with switch statements to achieve multi-branch selection.

6. Improve cycle efficiency

For the string name, look at the following loop:

The efficiency of for (I = 0; I < strlen (name); iTunes +) is significantly lower than that of the following cycles: n = strlen (name); for (I = 0; I < n; iTunes +)

The latter only needs to calculate the length of the name once.

7. Use less, use the Goto statement cautiously, and do not disable it

Goto statements can jump out of a multi-loop body or code heap at once, such as:

{... {... {... On error goto errorhandler;} errorhandler: …

This trick is commonly used in Visual Basic.

8. Eliminate the devil number

Devil number, there is no name constant, if you look at the English materials, they say magic data, some of our works translate it into "magic number", I prefer to translate it into "devil number", because it is a "devil" that leads to poor readability of the code.

Suppose you write the following code in the program:

For (iTun0; I < 100; iTunes +); for (iTun0; I < 99; iTunes +)

No one knows what 100 or 99 is. You may mean that 100 is the boundary of the range (* value). You should give a definition so that the reader of the code can understand what you mean:

The meaning of the macro constant of # define MAX 100 / * C language * / const int MAX = 100; / / the const constant for (ionom0; I < MAX; iConstant +) of C++ language; the meaning of for (iTun0; I < MAX-1; iTunes +) is clear.

And if a constant is closely related to other constants, the relationship should be included in the definition instead of giving some isolated values.

For example:

Const float RADIUS = 100; const float DIAMETER = RADIUS * 2

9. Function return value

The function name and the return value type can not conflict semantically, and the C standard library function getchar violates this rule.

For example:

Char c; c = getchar (); if (c = = EOF)

According to the name getchar, it is natural to declare the variable c as a char type. Unfortunately, getchar is not a char type, but an int type. The prototype is as follows:

Int getchar (void)

10. A pointer that points at random

The "wild pointer" refers to a random pointer, which is not a NULL pointer, but a pointer to "junk" memory. Wild pointers are dangerous and are often the cause of bug, which can be caused by two main causes:

One is that the pointer variable is not initialized. Any pointer variable does not automatically become a NULL pointer when it is first created in C _ pointer +, and its default value is random. Therefore, the pointer variable should be initialized at the same time as it is created, either setting the pointer to NULL or letting it point to legitimate memory. For example:

Char * p = NULL; char * str = (char *) malloc

Second, the pointer p is not set to NULL after being free or delete, which makes people think that p is a legal pointer.

Thank you for reading! This is the end of this article on "how to write code in programming development". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report