Summary of function variable layout in C + +

Source: Internet
Author: User

use the layout as a belief (layouts as religion).
                                                                                                                    -- Steve McConnell (author of the Book of Code Encyclopedia)

In earlier versions of the C language, the assignment of the specified variable must precede the declaration of all variables, so you can often see the following form of code:
void Func1 () {
int A1, a2, A3;
Double B1, B2, B3;
double* C1;
.....
B1 = B2 = B3 = 0.0;
......
C1 = NULL;
.....
a1 = 3+A2;
.....
}

One of the trouble with reading the above code is to remember where all the variables were first assigned, such as the assignment of the A1 in the preceding code, which would need to go to the previous location where it first appeared, and then view its assignment information in turn. In the Code encyclopedia there is an image of the metaphor,The declaration and use style of the early C function variables is like listing all the actors ' tables in the first episode, and no longer providing the cast after the second episode ., if you want to find the information of the actor, you have to find it in the first episode. Obviously, this is quite inconvenient for the audience. corresponding to the program, the person reading the code will be a headache to read this code.
Back to the analogy there,an appropriate scenario is that the actors appearing in each episode will only be in the cast of the episode, so they don't have to go to the first episode to find the actors ' information. Similarly, subsequent versions of C and C + + have taken this approach, giving up the assignment of variables in C that must be declared after the declaration of all variables, but not until the variable is used., in C + +, the layout format of the code is:
void Func1 () {
Double B1 = 0.0, b2 =0.0, b3 = 0.0;
......
double* C1 = NULL;
......
int a1 = 0, A2 =0, a3 = 0;
a1 = 3+A2;
.....
}
Read the revised code, it has a lot of distinct layers, summed up isThe principle of variable initialization:declare and use the variable at the location close to the first time you use the variable[1].
The variable initialization principle sometimes changes if the debugging factor is taken into account. If the results of the intermediate operation are observed during debugging, assuming that the Showtempresult () function is used to express, in order to avoid frequent comments and uncomment the code operation, it is advisable to use a variable bool Isshowres to control the call of this function, in accordance with the aforementioned rules, The layout format of the code is:
      void Func1 () {
Double B1 = 0.0, B2 =0.0, B3 =0.0;
          ......
double* c1 = NULL;
          ......
 bool isshowres = FALSE;
if (isshowres = = TRUE) {
Showtempresult ();
          }
int a1 = 0, A2 =0, a3 = 0;
a1 = 3+a2;
          .....
      }
Contemporary code ratio is less, such as less than a screen display (generally, about 50-150 lines, IBM has been the length of the program is limited to 50 lines or less [1]), this time the main program from the top down to find it. However, when the code size of the function is relatively large, debugging is a bit of a hassle. For example, the function I used recently has hundreds of lines, but unfortunately, the place to display is in the second half of the sub-function, and every time I remember to display the name of the function, go to search, find the location to call the function, and modify the value of the variable isshowres that controls the function call. An appropriate method is to refer to the debugging related variables to the front, and, if necessary, the corresponding annotation information, each time you want to invoke the display function directly before the function of the change, the layout of the code format is:
          void Func1 () {
              bool isshowres = FALSE;//To control whether intermediate results are displayed
Double B1 = 0.0, B2 =0.0, B3 =0.0;
              ......
double* c1 = NULL;
              ......
if (isshowres = = TRUE) {
Showtempresult ();
              }
int a1 = 0, A2 =0, a3 = 0;
a1 = 3+a2;
              .....
          }

careful children's shoes may ask, why not limit the line of code in a function to a screen, so that you do not violate the principle of variable initialization? In fact, not do not want, but can not. The reason is that if you are debugging on legacy code, and the code for the function of the legacy code itself is very long, it is easier to modify this long line of code to a shorter line of code. Even if you don't think about the specifics of the implementation, it's pretty scary to think about the number of parameter lists in the function, which can theoretically be overcome by a struct (truct) or C + + class, but the amount of code modification is still huge. When the modification time is higher than the quality priority of the code, it has to sacrifice the code quality in exchange for the robustness and correctness of the code. Therefore, if it is the first Coding, please follow the use of C + + classes in order to reduce the function of the parameter list and the number of child functions as short as possible , it will be a great convenience for future debugging.
 
References:
[1] Code Encyclopedia (2nd edition), Steve McConnell, Jinge, electronic industry Press, March 2006:10.3 Variable Initialization principle 7.4 subroutine can write how long         

Summary of function variable layout in C + +

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.