Improvements to programming styles

Source: Internet
Author: User

Improvements to programming styles

-- Reading C ++ programming specifications

 

1. if for while do and other statements occupy one row, and {} must be added regardless of the number of statements executed {}.

2. initialize the variable while defining the variable.

3. Add spaces as appropriate.

4. The program delimiters {And} should have an exclusive line.

5. You cannot add too many comments, which are usually on the top or right.

6. Do not use = or! = To compare with any number, try to convert it to> = or <=

7. Improve the efficiency of nested loops,Try to place the long cycle on the inner layer and move the logical judgment to the external body of the circulating body.

8. Circular variables cannot be modified in the circular body to prevent loops from being out of control.

9. Do not forget the default branch when using the Switch.

10. Use goto With caution when appropriate.

11. If the function does not have any parameters, fill in with void.

12. If the parameter is a pointer and only used for input, add const to the front to prevent the pointer from being accidentally modified in the function body.

13. Strictly check the function entrance and exit to improve the function quality.

14. functions must be single and small in size.

15. After applying for memory with malloc, check whether the pointer is NULL immediately.

16. Remember to assign an initial value to the dynamic memory.

17. After free memory, set the pointer to NULL immediately.

18. Use explicit data type conversion whenever possible.

19. Do not write highly skillful code.

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.