Code review Refactoring cases explained

Source: Internet
Author: User

Source: Peer system RepayCalc.cs, this code may refer to the system from the offline

A functional language usage problem

1) backdate Date type to datetime, you can not use string concatenation

2) Calculation error on repayment day (1 months! = 30 days)

3) Object-oriented programming, not all through function calculation

4) constructor instead of string concatenation, string concatenation and type conversion consume CPU

5) Redundant code deletion, error condition: The first repayment day is later than the end date of the loan, also cannot be monthly repayment, should be in the UI, or function at the beginning of processing, should not be inside the algorithm

5) Type refactoring, reducing type conversions, removing useless intermediate variables

At the same time the enddate variable is redefined, removing

A simple way to define two objects

Three calculation errors

After rounding the "principal per issue", the remaining principal is changed, and the "0.004" method is wrong.

To move into a loop, the algorithm changes to:

1) Return of principal = remaining principal/remaining period

2) This return of principal rounding

3) Modification of remaining principal and remaining period

Quad Repeat Code Elimination

1. Analysis

The code body is divided into 2 blocks,

The first block is the actual interest date equals the repayment date (5th interest, 5th repayment)

The second block is the actual interest date is not equal to the repayment date (5th interest, 10th repayment, the middle of the interest. 5th interest, 3rd repayment, the middle of no interest)

These two pieces are controlled by the if else, and the inside is flooded with countless duplicate codes

such as the following, resulting in a proliferation of code

2. Data errors should be handled at the beginning, so as to give hints early and reduce the internal execution time of the function

After repeating code extraction, merge with similar code outside

3. Processing the calculation head also calculates the tail demand, the original code two questions:

A If Else code block is calculated and repeated

B only deal with the actual number of days of borrowing, no processing repayment date, calculation of the situation, the repayment date need to advance one day

[ The revised interest date is the actual date of interest]

Original code:

Modified code:

The first period of interest calculation is incorrect when the five-day! = Repayment Date

Six business scenarios and thinking issues

1) The repayment date should be automatically generated according to the loan date.

2) Even in the following two cases

A) the first block is the actual interest date equal to the repayment date (5th interest, 5th repayment)

b) The second block is the actual interest date is not equal to the repayment date (5th interest, 10th repayment, the middle of the interest. 5th interest, 3rd repayment, the middle of no interest)

When actually implemented, the if else should also be swapped, because the first block is the simplification of the second block

Solve the first phase, modify the initial situation, the median algorithm equals the first block, and then the last phase, the last phase and the first phase algorithm is the same.

3) If you follow the first case, then the second complex situation will not be generated.

Delete if statement block

The calculation of the seven borrowing cycles

1) days/days of the month (actual days, and fixed to 30 days)

2) conversion based on the number of months borrowed

Eight refactoring as a strategy mode

Specific mode description: http://www.cnblogs.com/promise-7/archive/2012/05/29/2524357.html

Policy is also called policy mode "GOF95". Here is a schematic diagram of the policy pattern:

This pattern involves three characters:

    • Environment (context) Role: holds a reference to a strategy class.
    • Abstract policy (Strategy) role: This is an abstract role, usually implemented by an interface or abstract class. This role gives the interfaces required for all the specific policy classes.
    • Specific policy (concretestrategy) Role: Wraps the associated algorithm or behavior.
1) The current structure is this

There are many different algorithm functions for a class

2) We need to re-compose

An abstract class in which various algorithms inherit this abstract class, implementing classes using specific algorithms by instantiating abstract classes

A) Define the classes that need to be inherited

b) Define the algorithm that inherits the above class

c) Refactor the following code within the Calc () method

After refactoring, as below, why comment off Isint,isint's practical significance is convenient payment, if it is for the convenience of payment, then the principal and interest should be added after rounding

This is where the call is written.

Repaymentplancalc Rplancalc = new Averagecapital (myratecalc.borrowsum, Totalqishu, Myratecalc.borrowrate, time1);
backlist = Rplancalc.calc ();

Code review Refactoring cases explained

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.