Every programmer must abide by some excellent programming styles and every programmer

Source: Internet
Author: User

Every programmer must abide by some excellent programming styles and every programmer

Whether you are an amateur or professional programmer, the correct programming style will not only help improve the functions of software products, but also significantly reduce the generation of software disasters. Today, we will discuss some of the best rules that help us get better programming styles.

Code readability is paramount

To be readable and understandable, you must format the code in the same way. The name of functions and variables should be meaningful, and the expression of comments should be concise and accurate. Furthermore, it is important to accurately record all the tricky parts of the Code. You must understand why software programs can work and why they can work smoothly under all possible circumstances.

Correct naming conventions are required.

To name classes, functions, and variables, follow these guidelines:

  1. Make sure that the first letter of a specific class name is in uppercase;

  2. Use Case to separate the names of multiple words;

  3. Uppercase constant names and separated words with underscores;

  4. Make sure that the first letter of the specific function and variable name is in lower case;

  5. Use the abbreviation correctly. For example, use max instead of maximum.

Use spaces when necessary

Although spaces are meaningless to the compiler, they can be used to improve code readability. For example, you can leave three blank rows between functions. You can also use separate empty lines in the function to separate key code segments.

Make sure the Code has a certain degree of maintainability

We need to ensure that the written code is clear and easy to understand by replacing it with another programmer to adjust functions and fix bugs. We need to mark the key values in the function with constants so that we can change these constant values at any time as needed. All in all, the Code must be robust, be able to process any type of input, and then deliver the expected results without crashing.

Annotations must be easy to understand

Annotations should make sense and be able to clearly explain all content about the software program. Quality is the key to the number of annotations. You need to use the/* annotation */style to write the annotation to ensure that it is located at the top of each source file. In addition, you can include your name in the comments, the date of code writing, and briefly describe the actual use of the program. However, you can omit some comments with obvious functions. The line comment format you need to follow is // comment.

Use functions correctly

The code snippets contained in each function must be short and can complete specific tasks. Consider the function as a "black box"-independent, and can effectively process any type of input. Don't forget such an empirical Rule: the so-called "Ten Line Rule". That is to say, a function, generally speaking, if there are more than 10 rows, needs to be simplified in the most refined way. In addition, any repetitive code snippet should be set as a separate function. The above practice can not only shorten the length of the program, but also greatly improve its readability.

Neat code indentation

Indentation plays a vital role in the process control of software programs. Every new while, for, if statement, and switch structure requires code indentation. This can also be used when the parentheses in a row of statements have been omitted. For example, if an if statement exists, the corresponding else statement must be indented together.

Conclusion

If you are familiar with these correct programming styles, writing software programs will become a very interesting activity. Finally, I hope this article will be helpful to you. If you have different opinions, please join us.

Original ENGLISH: Want A Proper Programming Style? Here Are Rules You Need To Follow Translation: codeceo

 

Http://mp.weixin.qq.com/s? _ Biz = mjm5mzmynz1_ma ==& mid = 204698092 & idx = 2 & sn = 87536dc0ab233a4e8a2018c633f743f4 # rd

Related Article

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.