Some of the best programming styles that programmers have to follow

Source: Internet
Author: User

Whether you are an amateur or a professional programmer, the correct programming style, not only helps to improve the functionality of software products, but also significantly reduce the production of software disasters. Today, let's explore some of the best rules that will help us get a better programming style.

  Code readability is paramount

To be readable and understandable, the code needs to be formatted in a consistent manner. The naming of functions and variables should be meaningful, and the expression of annotations should be concise and accurate. And it is important to accurately record all the tricky parts of the code. You have to understand why the software program works and why it works well in all possible situations.

  It is necessary to follow the correct naming convention

When you need to name classes, functions, and variables, you need to follow these guidelines:

    1. Ensure that the first letter of a particular class name is capitalized;

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

    3. Capitalize constant names and use underscores to separate words;

    4. Ensure that the first letter of a particular function and variable name is lowercase;

    5. Note Use abbreviations correctly. For example, use Max instead of maximum.

  Use a space if you need to

While whitespace is meaningless to the compiler, it can be used to improve the readability of your code. For example, you can leave three blank lines between functions. You can also use a separate blank line within the function to detach the critical code snippet.

  Ensure that your code has some maintainability

We need to make sure that the code is written and replaced by another programmer to tweak the functionality, fix the bug, and be clear and understandable. The key values in the function are marked with constants so that we can change these constant values as needed at any time. In summary, the code must be strong enough to handle any type of input and then provide the expected results without crashing.

  Annotations must be easy to understand

Comments should be meaningful and clearly explain all the content of the software program. The number of comments does not matter, quality is the key. You need to use the/* comment */style to write the comments to make sure that the top of each source file is located. In addition, you can choose to include your name in the comments, the date of writing the code, and a concise description of the actual use of the program. However, you can choose to omit some noticeable comments. You need to follow the in-line comment format AS//comment.

  Proper use of functions

Each function contains a snippet of code that must be short and complete for a specific task. Consider a function as a "black box"-independent and effectively handle any type of input. Don't forget this rule of thumb-the so-called "Ten line Rule"-that is, a function, usually, if it's more than 10 lines, it needs to be simplified in the most refined way. Also, any repetitive code fragment should be set to a separate function. This approach not only shortens the length of the program, but also greatly improves its readability.

  Neat Code Indentation

Indentation plays a critical role in the process control of software programs. Each new while, for, if statement, and switch structure requires indenting code. This can also be used in cases where parentheses have been omitted in a line of statements. For example, if you have a false if statement, the corresponding Else statement must be indented together.

  Conclusion

If you are familiar with these correct programming styles, then writing a software program becomes a very interesting activity. Finally, I hope this article will be of benefit to you. If there are different views, welcome to explore together.

Link: http://www.codeceo.com/article/programming-style.html

Blog Yuan original link: http://news.cnblogs.com/n/519537/

Some of the best programming styles that programmers have to follow

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.