15 good programming habits

Source: Internet
Author: User

1. Before coding, you need to have a formal or rough design of the solution for coding implementation. Never start coding without any design, unless the code is not important.
2.
Good code documentation is as important as programming language knowledge. In the code source file, add comments to each major code segment to explain the basic logic of the Code. It is best to specify the build and modification date of the program and the reasons for the modification.
3. Maintenance Program versions are equally important. Currently, some programming tools come with a version management tool. Whenever you change your program, they will save it as a. bak file.
My approach is to maintain three different versions for each program. For example, I have a file named program. c, which is also used by other project team members. I copied this file as program. c. old as a backup file, and when I modified it, I would back up another copy file named program. c. wrk. Replace the program. c. wrk file when the modification is completed successfully.
You can also add a date or some comments to your program version, such as program260505.c or programReadFnWrking. c.
4. If the project contains multiple source files, create a README file, indicating the role of each source file, data file, temporary file, and log file (if any. You can also specify the compilation and running steps.
5.
Sometimes, you must know why the IF statement does not get the expected result. Maybe you are using an equal sign, that is, "=", rather than the condition criterion symbol "= ". A better way is to write conditional statements in reverse order. Therefore, your condition statement should be as follows:
If (10 = I )... Therefore, if you mistakenly write a single equal sign, you can also check it during compilation and report an error.
6. When loop and condition statements are used, the left and right parentheses are matched, and other statements are written in it. That is:
Code:
1 for (int I = 0; I <10; I ++)
 
2 {
 
4 printf ("I = % DN", I );
 
3} 3}

Note: Numbers starting with each line indicate the order in which the circular code is written.
7. Avoid using magic numbers ). For example, do not write
Code:
Circlearea = 3.14 * POW (radius, 2 );

Use the following code:
Code:
# Define PI 3.14
Circlearea = pI * POW (radius, 2 );

8.
Use meaningful variables and function names. For example, use 'radius 'instead of 'r' to represent the radius of the circle. Similarly, the function name 'calculateearea 'is much better than any other obscure abbreviation. In a hurry, we may use the abbreviated variable name, but if we save time at the beginning, we will waste more time to guess what the abbreviated variable name represents. (Editor's note :)
9.
It is a good habit to use print statements for subsequent debugging. However, removing these statements after the final code is also a dangerous task. Add a method to output debugging information. When the final version is generated, you only need to comment out this method. Therefore, you only need to make modifications in one place.
10.
After the code is compiled, optimize the code. Some previously declared variables may be useless now. Similarly, declarations that do not depend on loops can be moved out of the loop module. Solid compilation knowledge will also be helpful for future code optimization.
11. have a good understanding of your operating system and hardware. You can improve program performance in terms of resource usage and other aspects.
12. Use indentation properly when writing code to make the code clear and readable.
13. Put the project file in different folders, such as source, headers, make, and exes.
14. Study the code written by others. This allows you to learn new programming technologies and the methods they use to solve the same tasks as you do.
15. The last one (but not the least one) backs up the source code file so that when a hard disk error or the same problem occurs, it will not be discarded.
Additional: add one by sticking to a naming mode. If you plan to use the Hungarian naming convention, stick to it and use it extensively. Otherwise, it will be counterproductive. For more information, see Eric lippert, senior Microsoft engineer.
This article "reading the code is not simple".
Editor's remarks
There are more than 15 good habits of programming. Maybe you don't agree with some of the above ideas. Please mark the corresponding serial number and describe its shortcomings. In addition, you are welcome to share your good habits.
Link to the original article: 15 good computer programming habits

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.