[Reconstruction learning] 08 reconstruction of conditional expressions and reconstruction of 08 conditional expressions

Source: Internet
Author: User

[Reconstruction learning] 08 reconstruction of conditional expressions and reconstruction of 08 conditional expressions

The so-called conditional expression is the branch statement. Remove the branch statement.

1. decomposition condition expression

Modify: You have a complex conditional statement (if else Statement)

Practice: extract the three parts of the conditional expression into independent functions.

That is

If (Part A) {Part B;} else {Part C ;}

All three parts are extracted into functions.

2. Merge conditional expressions

Modify: You have a series of tests and get the same results.

Practice: combine these tests into a conditional expression and extract the expression into an independent function.

3. Merge duplicate condition fragments

Modify point: Each branch of the conditional expression has the same code segment.

Practice: Move the duplicate code out of the conditional expression.

4. Remove control flag

Modification point: in a series of boolean expressions, a variable carries a control flag.

Practice: Replace the control mark with a break or return statement

Bool isILoveYou = true; int [] loveNum = new int [100]; for (int I = 0; I <100; I ++) {if (isILoveYou) {if (for some reason) {isILoveYou = false;} Console. writeLine (loveNum [I]);}

IsILoveYou is the control mark, so you can use the jump statements such as continue, break, and return to remove the control mark.

5. Replace nested condition expressions with Wei statements

Modification point: the conditional logic in the function makes it difficult for people to see the normal execution path.

Practice: Use the Wei statement to show all situations.

If a condition is extremely rare, it should be checked separately and immediately returned from the function when the condition is true. Such a statement is called a "Wei statement ".

6. Replace conditional expressions with Polymorphism

Modify: You have a conditional expression that selects different behaviors based on different object types.

Practice: place each branch of the conditional expression into the override function of a subclass, and declare the original function as an abstract function.

If you do not know clearly, you can take a look at the 14th-point code in Data Reconstruction. Maybe you will be more clear.

7. Introduce NULL objects

Modify: You need to check whether an object is NULL.

Practice: replace NULL with a null Object

Code with a NULL value:

Class Program {static void Main (string [] args) {var roomList = new List <Room> (); roomList. add (new Room ("low-level Room", new People ("Mr. A"); roomList. add (new Room ("Advanced room"); foreach (var room in roomList) {if (Room. master! = Null) {Console. writeLine (room. master. name) ;}} Console. readKey () ;}} public class Room {public Room (string roomName, People people = null) {this. roomName = roomName; this. master = people;} public string RoomName {get; set;} public People Master {get; set ;}} public class People {public People (string name) {this. name = name ;}public string Name {get; set ;}}

Change

Class Program {static void Main (string [] args) {var roomList = new List <Room> (); roomList. add (new Room ("low-level Room", new People ("Mr. A"); roomList. add (new Room ("Advanced room"); foreach (var Room in roomList) {Console. writeLine (room. roomName + ":" + room. master. name);} Console. readKey () ;}} public class Room {public Room (string roomName, People people = null) {this. roomName = roomName; this. master = people;} p Ublic string RoomName {get; set;} public People Master {get {return _ master = null? New NullPeople (): _ master;} set {_ master = value;} People _ master;} public class People {public People () {} public People (string name) {this. name = name;} public virtual string Name {get; set;} public virtual bool IsNULL () {return false;} public static People CreateNullPeople () {return new NullPeople () ;}} public class NullPeople: People {public NullPeople (): base () {} public override bool IsNULL () {return true ;} public override string Name {get {return "unmanned ";}}}

It may be better not to inherit people here. They all inherit a custom interface IsNULLPeople, and the code looks better.

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.