Code maintenance: some ways to improve the code

zhaozj2021-02-16  61

Code maintenance: some ways to improve the code

0 Preface

The maintenance of the entire life cycle of the application is usually underestimated. However, maintenance is undoubtedly the most cost, and it is also the most difficult phase. Therefore, the code quality of the initial development phase directly affects the cost of maintenance. Some of the following numbers explain this:

· A project's average life is 4 to 5 years, and the development phase is approximately one year.

· At this stage, 40% to 50% of the code need to be modified, or error correction, or update.

· The cost of modifying a line of code in the maintenance phase is 2 to 3 times the initial development phase.

These numbers indicate:

· The code must be clear and correct text annotation, which can still be understood in 4 or 5 years, even when the developer of the code is not.

· The code must be updated and properly structures to face future changes.

· Errors in the project must be checked as early as possible to restrict costs that have an impact in the future.

We will see how to follow some simple rules, developers to promote maintenance through limiting costs and risks. To provide a specific example, we will improve the following code, use the code of the PowerBuilder in PowerBuilder, which is extracted from the itemchanged event from the data window.

Long a

If dwo.name = "amount" then

IF dw_1.getitemnumber (Row, "Type") = 5 or dw_1.getitemnumber (Row, "Type" = 6 THEN

Dec B

For a = 1 to dw_1.rowcount ()

IF dw_1.getitemnumber (a, "type") = 5 or dw_1.getitemnumber (A, "Type") = 6 &

And a <> row kil

B = dw_1.getitemnumber (a, "amount")

END IF

NEXT

IF DEC (DATA) B> 450000 THEN

MessageBox ("Error", "The Total Amount of this Sales Order IS TOO BIG")

Return 1

END IF

END IF

END IF

If dwo.name = "type" then

// If the data window object is listed as "Type", then ...

END IF

If dwo.name = "amount" then

// If the data window object is listed as "Amount", then ...

END IF

1 increase the easy-comprehensibility of the code

How many times have you studied (or even your own code), how difficult it is? Although it is easy to judge whether the code is easy to read, it is more difficult to explain the reason. There are some rules here. If you follow, you will guarantee the minimum readability.

1.1 Correct naming code elements

The correct factor in naming is the object, function, and variables. Naming correctly means that the name must represent the main properties of the project. In other words, the name must clearly describe what the object can do, how to use it.

For example, if the variable is named A, it does not represent what it is used, what is the data type. To achieve this, you must search for the definition of variables and analyze the code to learn how to use it. Let us reconsider this variable, named ll_rowcount, so that there is no connection between context, we can also know that it is to process a local variable, long integer long, used to store the number of records. If you comply with Sybase's naming rules, use a regular language to assign a name, you can greatly enhance the readability of the code. 1.2 Definitions and complex expressions must be annotated

In order to enhance the understandability of the code, you must add an annotation to the code. Some developers do not add annotations at all, and some are annotated. According to my opinion, we must choose a balance in these two limits. In fact, the annotations are cost, it is difficult to keep annotations to maintain consistency with the modified code (because the compile time is not checked). That is why I think the annotation should only be added to the place where the code can be understood:

· Variable definition, if their names are not fully described;

· It is not possible to think of an express expression (such as mathematical formula, complex conditions, etc.).

1.3 Complex conditions must be easy to understand

If the conditions are complicated, it is difficult to grasp what it is when reading. Therefore, it is necessary to make it easy to read as much as possible, so try to avoid using a function call, replace an unclear value with a fixed type variable.

Example:

Take the code

Long a

IF dw_1.getitemnumber (Row, "Type") = 5 or dw_1.getitemnumber (Row, "Type") = 6 and a <> row

...

END IF

Change to:

LI_PRODUCTTYPE = dw_1.getitemnumber (Row, "Type")

If li_producttype = book or li_producttype = video and ll_currentrow <> row then

...

END IF

1.4 Local variables must be defined at the beginning of the code

PowerBuilder's compiler does not require a variable definition, so you can define all variables at the beginning of the code. This greatly simplifies the search process when we look for definitions.

2 code planning and documentation is as important as

This rule can be implemented well because the PowerBuilder code editor greatly simplifies this task by providing automatic indentation and color indication. But still suggest that you re-read your code and try to improve its plan:

· Cooperate with automatic indentation principles

· Correctly define variables

· Leave a blank line between each part of the program

· If a line of instructions are too long, you can't read it in a row, start a new line.

If we apply these principles to the sample code, you will get the following code:

Long: ll_currentrow, ll_rowcount

INT Li_ProductType

Dec ldc_total

Constant int book = 5, Video = 6

Constant Dec maxamount = 450000 / / Points the maximum number of Books and VIDEO

Choose Case dwo.name

Case "AMOUNT"

// Check the total number of Books and VIDEOs without exceeding the maximum number

/ / =========================================================================================================================================================================================== ====================== l_ProductType = dw_1.getitemnumber (Row, "Type")

IF li_producttype = book or li_producttype = video

// Do not consider the number of current line data to calculate sales orders

/ / =========================================================================================================================================================================================== ==================

LL_ROWCOUNT = dw_1.rowcount ()

For ll_currentrow = 1 to LL_ROWCOUNT

LL_ProductType = dw_1.getitemnumber (LL_Currentrow, "Type")

IF li_producttype = book or li_producttype = video and &

LL_Currentrow <> row then

LDC_Total = dw_1.getitemnumber (LL_Currentrow, "Amount")

END IF

NEXT

IF (DATA LDC_TOTAL)> MaxAmount Then

MessageBox ("Error", "The Total Amount for Books and Video Is Too Big")

Return 1

END IF

END IF

// If the data window object is listed as "Amount", then ...

Case "Type"

// If the data window object is listed as "Type", then ...

End chaoose

3 code documentation

Documentation allows us to easily understand the structure of the application. In addition to the cost of editing, the issues faced by the document are maintained a document during a change. In fact, there is no direct connection between code and documentation. Without automatic verification, it can check if the document is updated correctly, but no.

In order to realize the automatic verification function, make the document as effective as possible, I think it should be as close as possible to the question. The use of the header in the code is a valid approach.

A good document must be able to answer the following questions:

· What is the purpose of code?

· What kind of business rules apply? · How are they applied?

· Why choose a specific technical solution?

· What is the restriction in the code application if any?

· Who is in, why do the reason for this code?

After adding the header, the code is as follows:

/ * ================================================================================================================================================================ ==========================================================================================================================================================

Description: Verify data

Verification rules:

* R000051: Check the total number of books and video without exceeding the maximum number

* ....

Note:

This data window can only be updated by the salesperson user, and other users have no right to modify the data.

============================================================================================================================================================================================================= ==========================================================================================================================================================

Modify record:

V1.00 2000-01-01 WINBOY creation

V1.02 2000-05-11 WINBOY Add Business Law R000051.

============================================================================================================================================================================================================= ============================================== * /

Long ll_currentrow, ll_rowcount

INT Li_ProductType

Dec ldc_total

Constant int book = 5, Video = 6

Constant Dec Maxamount = 450000 / / Points the maximum number of books and video Choose Case dwo.name

Case "AMOUNT"

// Check the total number of Books and VIDEOs without exceeding the maximum number

/ / =========================================================================================================================================================================================== ======================

LI_PRODUCTTYPE = dw_1.getitemnumber (Row, "Type")

IF li_producttype = book or li_producttype = video

// Do not consider the number of current line data to calculate sales orders

/ / =========================================================================================================================================================================================== ==================

LL_ROWCOUNT = dw_1.rowcount ()

For ll_currentrow = 1 to LL_ROWCOUNT

LL_ProductType = dw_1.getitemnumber (LL_Currentrow, "Type")

IF li_producttype = book or li_producttype = video and &

LL_Currentrow <> row then

LDC_Total = dw_1.getitemnumber (LL_Currentrow, "Amount")

END IF

NEXT

If you have different views, or have something good comments and suggestions, you can send me email: winboy20@sina.com. Thank you.

转载请注明原文地址:https://www.9cbs.com/read-28082.html

New Post(0)