[Good Programming habits] differentiate variables by naming rules

Source: Internet
Author: User
Tags define local

Using naming rules to differentiate Structure Variables, function parameters, and local variables helps improve program readability. For example, Figure 1 is a program written using the naming rules that I usually use. This rule is:
1) for the member variables in the structure or C ++ class, add "_" after the variable name.
2) Add "_" before the parameter name of the function.
3) local variables and global variables are not added with "_" before and after them.

Example. c
00123: typedef struct dll_node
00124 :{
00125: struct dll_node * prev _;
00126: struct dll_node * next _;
00127 :} dll_node_t;
00128:
00129: static bool mem_find_neighbor (dll_node_t * _ p_node, void * _ p_key)
00130 :{
00131: maddr_t addr = * (maddr_t *) _ p_key;
00132: mnode_t * p_mnode = (mnode_t *) _ p_node;
00133: mnode_t * p_next = (mnode_t *) p_mnode-> node _. next _;
00134:
00135: if (p_next = 0 | p_mnode-> addr _> addr ){
00136: return false;
00137 :}
00138:
00139: // we try to find the node that its address is less than [addr]
00140: // but the address of following node is bigger than [addr]
00141: if (p_mnode-> addr _ <addr) & (p_next-> addr _> addr )){
00142: return false;
00143 :}
00144:
00145: return true;
00146 :}
Figure 1

In many cases, you need to define local variables within the function, and sometimes the defined local variable name may have the same name as the input parameter name of the function. If you do not use naming rules to distinguish between function parameters and local variables, you may need to define a xxx_temp local variable, which is not readable. Using the author's method, if the function parameter is _ xxx, you can still define a local variable name xxx. When the reader of the program sees the two variables, it can quickly know that the functions of the two are the same, but one is the function parameter, and the other is the local variable inside the function.

This article from "to Jane Li cloud" blog, please be sure to keep this source http://yunli.blog.51cto.com/831344/259685

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.