Organize linear codes

Introduce

This section introduces the simplest control force flow: placing statements and code blocks in order

There must be a clear sequence of statements

java example: statements with dependencies before and after

data = ReadData();
results = CalculateResulttsFromDate(data);
PrintfResults(results);

In this example, the dependency between the preceding and following statements can be clearly seen from the name of the subroutine, which contains the underlying underlying concepts and dependencies.
VB example: hide the pre- and post-dependency of the statement

ComputeMarketingExpense
ComputeSalesExpense
ComputeTravelExpense
ComputePersonnelExpense
DisplayExpenseSummary

In this example, assume that ComputeMarketingExpense is a member variable of the initialization class. The subroutine call does not take any parameters, so you might guess that every subroutine here will access the class data, but you cannot confirm this by reading the code. If there are dependencies between statements, and these statements require you to arrange them in a certain order, try to make these dependencies obvious. Here are some simple principles for organizing statements:
1. Try to organize the code so that the dependencies become very obvious

ComputeMarketingExpense改写为InitializeExpenseDate进行初始化                 //清楚的表明,应该在调用其他子程序前调用初始化函数

2. Make the name of the subroutine highlight the dependency

ComputeMarketingExpense改写为InitializeExpenseDate进行初始化                 //清楚的表明,此函数的作用是初始化

3. Use the subprogram parameters to clearly show that the dependency relationship
uses the same input parameters, implying sequential dependency data and subprogram calls. All subprograms use expenseDate. You will get a hint from it that they may operate on the same data, so these The order of the statements may be important:

InitializeExpenseDate(expenseData)
expenseData = ComputeMarketingExpense(expenseData)
expenseData = ComputeSalesExpense(expenseData)
expenseData = ComputeTravelExpense(expenseData)
expenseData = ComputePersonnelExpense(expenseData)
expenseData = DisplayExpenseSummary(expenseData)

You can also use parameters to indicate that the order of execution is not important. The following example shows that there is no order relationship between the first few subroutines, and the last subroutine can follow the others:

InitializeExpenseData(expenseData)
ComputeMarketingExpense(markingData)
ComputeSalesExpense(salesData)
ComputeTravelExpense(travelData)
ComputePersonnelExpense(personnelData)
DisplayExpenseSummary(expenseData, markingDate, salesData, travelData, personnelData)

4. Use comments to explain unclear dependencies. Explain
clearly the code with unclear dependencies:

上例:计算exense数据。每个子程序都访问expenseDate数据成员。DisplayExpenseData应该最后调用,因为他依赖于其他子程序计算出的数据。

5. Use assertion or error handling code to check dependencies.
You can add a bool type variable isExpenseDataInited to class members, set the value to true in the InitializeExpenseData function, and use it in ComputeMarketingExpense to determine its value to be true before using it in these functions.

Note:
Try to write code without sequential dependencies, and then write code with obvious dependencies as much as possible. If a certain dependency is not clear, then use comments or documents to explain.

Order-independent statements

When there is a lack of execution order dependency, you can use the second criterion to judge the order of statements or code blocks. The guiding principle is the principle of proximity: Put related operations together.
1. Make the code easy to read from top to bottom

C++示例:跳来跳去糟糕代码
	MarkingDateData markingData;
	SlaesData salesData;
	
	markingData.ComputeQuarterly();
	salesData.ComputeQuarterly();
	
	salesData.ComputeAnnual();
	markingData.ComputeAnnual();
	
	salesData.Print();
	markingData.Print();
C++示例:组织良好的顺序代码,能从头到尾阅读
	MarkingDateData markingData;
	markingData.ComputeQuarterly();
	markingData.ComputeAnnual();
	markingData.Print();

	SlaesData salesData;
	salesData.ComputeQuarterly();
	salesData.ComputeAnnual();
	salesData.Print();

Put the references of each object together; "localize" them, and the fewer lines of code the object "lives", the clearer the code understanding.
2. Group related statements together.
Statements are related because they all process the same data, perform similar tasks, or have a dependency on the order of execution. A good program should be modular, and related statements are organized together. It may be found that they are strongly related. Statements with a strong degree of association can be reconstructed into independent subroutines.

Highlights of this chapter

1. The most important principle of linear code is to arrange it in accordance with the dependency relationship.
2. You can use good subroutine names, parameter lists, and comments to make the dependencies more obvious.
3. If there is no dependency between the codes, try to make the related statements as close as possible. The related statements are grouped together so that the code can be read in a top-down order. Relatively independent statement groups are placed in their respective subroutines.

Guess you like

Origin blog.csdn.net/weixin_37921201/article/details/88651698