Step by step. NET code refactoring study note 12

Source: Internet
Author: User

1. Preserve Whole Object (keep the Object complete)

Motivation)

To pass several values to a method, you can use it to pass the entire object.

Example

View sourceprint? 1 int low = DaysTempRange (). GetLow ();

2 int high = DaysTempRange (). GetHigh ();

3 withinPlan = Plan. WithinRange (low, high );

 

 

Change

View sourceprint? 1 withinPlan = Plan. WithinRange (DaysTempRange ());

 

 


2. Replace Parameter with Methods (Replace parameters with functions)

Motivation)

The object calls a function and passes the result as a parameter to another function. The function that receives this parameter should directly call this function.

Example

View sourceprint? 01 public double GetPrice ()

02 {

03 int basePrice = _ quantity * _ itemPrice;

04 double discountLevel = GetDiscountLevel ();

05 double finalPrice = DiscountedPrice (basePrice, discountLevel );

06}

07

08 private double DiscountedPrice (int basePrice, double discountLevel)

09 {

10

11}

 

 

Change

View sourceprint? 01 public double GetPrice ()

02 {

03 int basePrice = _ quantity * _ itemPrice;

04 double finalPrice = DiscountedPrice (basePrice );

05}

06

07 private double DiscountedPrice (int basePrice)

08 {

09 double discountLevel = GetDiscountLevel ();

10}
View sourceprint? 1
View sourceprint? 1

3. Introduce Parameter Object (introducing Parameter objects)

Motivation)

Replace these parameters with an object.

Example

View sourceprint? 1 public string GetUserInfo (int ID, string name, string age, string sex)

2 {

3

4}

 

 

Change

View sourceprint? 01 public string GetUserInfo (User user)

02 {

03

04}

05

06

07

08 public class User

09 {

10 public int ID {get; set ;}

11

12 public int Name {get; set ;}

13

14 public int Age {get; set ;}

15

16 public int Sex {get; set ;}

17

18}

 

Iv. Remove Setting Method (Remove the Setting function)

Motivation)

Remove all setter functions of this value field. A Value Field in the class should be set at the beginning of the object, and then it will not change.

Example

View sourceprint? 01 public class Account

02 {

03 private string _ ID;

04 public Account (string id)

05 {

06 SetID (id );

07}

08

09 private void SetID (string id)

10 {

11 _ ID = id;

12}

13}

 

 

Change

View sourceprint? 1 public class Account

2 {

3 private string _ ID;

4 public Account (string id)

5 {

6 _ ID = id;

7}

8}

 

 

5. Hide Method (Hide a function)

Motivation)

A function has never been used by any other class. Change this class to private.

Example

View sourceprint? 1 public string GetUserInfo (User user)

2 {

3 return null;

4}

 

 

Change

View sourceprint? 1 private string GetUserInfo (User user)

2 {

3 return null;

4}

 

 

6. Replace Constructor with Factory Method (Replace constructors with Factory functions)

Motivation)

When creating an object, not only do simple construction, but replace constructor (constructor) with factory method (factory function)

Example

View sourceprint? 1 public Account (string id)

2 {

3 _ ID = id;

4}

 

 

Change

View sourceprint? 1 public static Account Create (string id)

2 {

3 return new Account (id );

4}

 

 

VII. Encapsulate Downcast (encapsulation of [downward Transformation] Action)

Motivation)

An object returned by a function must be executed by the function caller to perform the [downcast] (downcast) action.

Example

View sourceprint? 1 public object LastReading ()

2 {

3 return Readings. LastElement ();

4}

 

 

Change

View sourceprint? 1 public Reading LastReading ()

2 {

3 return (Reading) Readings. LastElement ();

4}
View sourceprint? 1
View sourceprint? 1

8. Replace Error Code with Exception (replacing Error codes with exceptions)

Motivation)

A function returns a specific code (special code) to indicate an error.

Example

View sourceprint? 01 private int _ balance;

02

03 public int Withdraw (int amount)

04 {

05 if (amount> _ balance)

06 return-1;

07 else

08 {

09 _ balance-= amount;

10 return 0;

11}

12}

 

 

Change

View sourceprint? 1 private int _ balance;

2

3 public int Withdraw (int amount)

4 {

5 if (amount> _ balance)

6 throw new Exception ("error ");

7 _ balance-= amount;

8}

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.