Reconstruction of "Refactoring learning" 08-piece expression

Source: Internet
Author: User

The so-called conditional expression, is the branch statement, remove the branch statement

1. Decomposition condition expression

Modify point: You have a complex conditional statement (that is, if else statement)

Procedure: The three parts of a conditional expression are extracted separately from the Independent function

That

            if (Part A)            {                part B;            }             Else {                part C;            }    

All three of these parts are refined into functions.

2. Combining conditional expressions

Modify point: You have a series of tests that all get the same result

Practice: Combine these tests into a single conditional expression and refine the expression into a separate function

3. Merge duplicate condition fragments

Modify point: Has the same piece of code on each branch of the conditional expression

Practice: Move this repeating code beyond the conditional expression

4. Remove the control mark

Modify point: In a series of Boolean expressions, a variable has the function of control flag.

Practice: Replace a control tag with a break statement or a return statement

            BOOLisiloveyou=true; int[] lovenum=New int[ -];  for(intI=0; I < -; i++) {                if(isiloveyou) {if(for some reason) {isiloveyou=false;                } Console.WriteLine (Lovenum[i]); }            }        

And Isiloveyou is the control tag, so you can use the Continue,break,return to remove the control mark

5. Replace nested conditional expressions with Guardian statements

Modify point: Conditional logic in a function makes it difficult to see the normal execution path

Practice: Use the Guardian statement to represent all situations.

If a condition is extremely rare, the condition should be checked separately and returned immediately from the function when the condition is true, and such statements are called "guardian statements".

6. Replacing conditional expressions with multiple states

Modify point: You have a conditional expression on your hand that chooses different behavior depending on the type of object.

Practice: Place Each branch of the conditional expression into an overlay function within a subclass, and then declare the original function as an abstract function.

If you can't figure it out, you can take a look at my 14th code in 7, the refactoring of the data, and maybe you'll be clearer.

7. Introduction of NULL Objects

Modify point: You need to check whether an object is null repeatedly

Practice: Replace null values with a null object

Code with a null value:

classProgram {Static voidMain (string[] args) {            varRoomlist =NewList<room>(); Roomlist.add (New("Junior Room",NewPeople ("Mr. A"))); Roomlist.add (New("Superior Room")); foreach(varTheinchroomlist) {                if(A. Master! =NULL) {Console.WriteLine (.                Master.name);        }} console.readkey (); }    }     Public classThe hostel { Public(stringRoomname,people people=NULL)        {             This. Roomname =Roomname;  This. Master =people; }         Public stringRoomname {Get;Set; }  PublicPeople Master {Get;Set; } }     Public classpeople { PublicPeople (stringname) {             This. Name =name; }         Public stringName {Get;Set; } }

Switch

classProgram {Static voidMain (string[] args) {            varRoomlist =NewList<room>(); Roomlist.add (New("Junior Room",NewPeople ("Mr. A"))); Roomlist.add (New("Superior Room")); foreach(varTheinchroomlist) {Console.WriteLine. Roomname+":"+.            Master.name);        } console.readkey (); }    }     Public classThe hostel { Public(stringRoomname,people people=NULL)        {             This. Roomname =Roomname;  This. Master =people; }         Public stringRoomname {Get;Set; }  Publicpeople Master {Get{                return_master==NULL?Newnullpeople (): _master; }            Set{_master=value;    }} people _master; }     Public classpeople { Publicpeople () {} PublicPeople (stringname) {             This. Name =name; }         Public Virtual stringName {Get;Set; }  Public Virtual BOOLIsNULL () {return false; }         Public StaticPeople Createnullpeople () {return Newnullpeople (); }    }     Public classnullpeople:people{ PublicNullpeople ():Base() {                 }         Public Override BOOLIsNULL () {return true; }         Public Override stringName {Get            {                return "No"; }        }    }

Not inheriting people here may be a little better, all inheriting a custom interface Isnullpeople, the code is better to look at a bit.

Reconstruction of "Refactoring learning" 08-piece expression

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.