Thoughts on variable survival time

Source: Internet
Author: User

1.Thoughts on variable survival time

Code complete puts forward two concepts: one is span (expansion) and the other is live time (survival time), which can be quantified to measure the quality of code. What are the meanings of these two concepts? Let's look at an example:

A = 0;

B = 0;

C = 0;

A = B + C;

We can see that a appears in the first and 4th rows, so the span of a is 2. B Similarly, span is 1, and C is 0. What is the impact of span size on the program? If span is small, the value assignment and operation of the variable will be relatively concentrated, so the person who looks at the code will focus on this small segment, without looking back for the declaration or initial value of the variable, it facilitates code readers, as well as error detection and program modification.

What is live time? This concept does not mean the survival time of variables in the traditional sense. The beginning of a variable's life is the first statement that references the variable, and the end and the last statement that references the variable. See the following figure:

 

 


The concepts of live time and span are clearly explained. Similarly, if the live time is small, you can focus only on the small piece of code for a variable. For example, if a variable is only used in lines 44 and 45, you only need to focus on two lines of code. If it is used in lines 10 and 45, you must view all the 34 lines of code between them to prevent unexpected changes to the variable. The reduction of live time also increases code readability, reduces the possibility of errors, and facilitates error detection and modification.

It is very easy to reduce these two values. One is to assign values when using them, and the other is to assign values to the variables to be used before the loop, instead of assigning values to variables at the beginning of a function that contains this loop. This is because if loop A is modified and another loop B is initialized, not only the first loop A is initialized, but also the B is initialized.

The above explains why the program should use global variables as little as possible. The use of global variables is very convenient for programmers, and data can be read and modified at will for ease of use. However, it is very painful for others to read or modify the error. If you want to understand a function, you must understand all other functions, because the coupling is too tight. With well-encapsulated functions, using only variables with short live time and span will make it easy for users who want to read the code and facilitate future modification and maintenance. But this also increases the complexity of the program to some extent, but it must be much better than a program full of global variables.

2.Put logic-related operations together

First, let's look at the following example:

Void summarizedata (...){

...

Getolddata (olddata, & numolddata );

Getnewdata (newdata, & numnewdata );

Totalolddata = sum (olddata, numolddata );

Totalnewdata = sum (newdata, numnewdata );

Printolddatasummary (olddata, totalolddata, numolddata );

Printnewdatasummary (newdata, totalnewdata, numnewdata );

Saveolddatasummary (totalolddata, numolddata );

Savenewdatasummary (totalnewdata, numnewdata );

...

}

We can see that this program is poorly written and does not put the logic-related operations together. When we use old and new products, we are definitely in a bad mood, but it is easy to modify it.

Void summarizedaily (...){

Getolddata (olddata, & numolddata );

Totalolddata = sum (olddata, numolddata );

Printolddatasummary (olddata, totalolddata, numolddata );

Saveolddatasummary (totalolddata, numolddata );

...

Getnewdata (newdata, & numnewdata );

Totalnewdata = sum (newdata, numnewdata );

Printnewdatasummary (newdata, totalnewdata, numnewdata );

Savenewdatasummary (totalnewdata, numnewdata );

...

}

In this way, the logic is much clearer. This is essentially the same as the previous variables live time and span,It is only one thing at a time, allowing people to concentrate a little bit, so that they will have a clear idea. The same is true for writing programs and doing things.

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.