Step by step. NET code refactoring study note 13

Source: Internet
Author: User

1. Replace Exception with Test (Replace Exception with Test)

Motivation)

You throw an exception in the face of a [condition that the caller can check in advance. Modify the caller so that the caller can check the caller before calling the function.

Example

01 private Dictionary <int, string> _ values;

02

03 public double GetValueForPeriod (int periodNumber)

04 {

05 try

06 {

07 return _ values [periodNumber];

08}

09 catch

10 {

11 return 0;

12}

13}

 

Change

View sourceprint? 1 private Dictionary <int, string> _ values;

2

3 public double GetValueForPeriod (int periodNumber)

4 {

5 if (_ values. ContainsKey (periodNumber ))

6 return _ values [periodNumber];

7 return 0;

8}

 

 

Ii. Pull Up Field (value range Up)

Motivation)

The two subclasses have the same value range and move this value to the superclass.

Example

01 public abstract class Emplayee

02 {

03

04}

05

06 public class Salesman: Emplayee

07 {

08 private string _ name;

09

10 public string Name

11 {

12 get {return _ name ;}

13 set {_ name = value ;}

14}

15}

16

17 public class Engineer: Emplayee

18 {

19 private string _ name;

20

21 public string Name

22 {

23 get {return _ name ;}

24 set {_ name = value ;}

25}

26}

 

Change

01 public abstract class Emplayee

02 {

03 private string _ name;

04

05 public string Name

06 {

07 get {return _ name ;}

08 set {_ name = value ;}

09}

10}

11

12 public class Salesman: Emplayee

13 {

14

15}

16

17 public class Engineer: Emplayee

18 {

19

20}

 

3. Pull Up Method (function move Up)

Motivation)

Some functions generate identical results in each subclass. Move this function to superclass.

Example

01 public abstract class Emplayee

02 {

03

04}

05

06 public class Salesman: Emplayee

07 {

08 public string GetName ()

09 {

10 return "spring yang ";

11}

12}

13

14 public class Engineer: Emplayee

15 {

16 public string GetName ()

17 {

18 return "spring yang ";

19}

20}

 

Change

01 public abstract class Emplayee

02 {

03 public string GetName ()

04 {

05 return "spring yang ";

06}

07}

08

09 public class Salesman: Emplayee

10 {

11}

12

13 public class Engineer: Emplayee

14 {

15}

 

4. Pull Up Constructor Body (Constructor ontology moving Up)

Motivation)

There are some constructors in each subclass, and their ontology code is almost the same. Create a New constructor in the base and call it in the subclass constructor.

Example

01 public abstract class Emplayee

02 {

03 private string _ ID;

04 private string _ name;

05

06 public string ID

07 {

08 get {return _ ID ;}

09 set {_ ID = value ;}

10}

11

12 public string Name

13 {

14 get {return _ name ;}

15 set {_ name = value ;}

16}

17}

18

19 public class Salesman: Emplayee

20 {

21 public Salesman (string id, string name)

22 {

23 ID = id;

24 Name = name;

25}

26}

 

Change

01 public abstract class Emplayee

02 {

03 private string _ ID;

04 private string _ name;

05

06 public string ID

07 {

08 get {return _ ID ;}

09 set {_ ID = value ;}

10}

11

12 public string Name

13 {

14 get {return _ name ;}

15 set {_ name = value ;}

16}

17

18 public Emplayee (string id, string name)

19 {

20 _ ID = id;

21 _ name = name;

22}

23}

24

25 public class Salesman: Emplayee

26 {

27

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.