Ios-iphone Development refactoring: From hard coding to model to law

Source: Internet
Author: User

Whether in the iphone development or learning process will see some not very ideal code, it is undeniable that they are constantly "contributing" to this kind of code. In the face of some code "bad taste", refactoring is obviously an effective way to solve. The "reconstruction of the iphone" series wanted to summarize and supplement some of the refactoring that was going on in the development of the iphone, which might cite some open source and actual project code, with a quest for technology, and a lot of apologies for the author's pardon.

In the iphone development process often encounter the need to push different controllers according to different table rows or identifiers, one of the most straightforward implementations is hard coding, via If...else if...else or switch...case ... Implementation, the code is as follows:

Before refactoring:

-(void) Pushviewcontrollerwithidentifier: (NSString *) identifier animated: (BOOL) animated {if ([identifier    isequaltostring:@ "article"]) {viewcontroller = [[Wharticleviewcontroller alloc] initwithidentifier:identifier]; } else if ([identifier isequaltostring:@ "Survivalkit"]) {viewcontroller = [[Whsurvivalkitviewcontroller alloc] I    NIT]; } else if ([identifier isequaltostring:@ "Search"] {viewcontroller = [[Whsearchviewcontroller alloc] Initwithident    Ifier:identifier]; } else if ([identifier isequaltostring:@ "Featured"]) {viewcontroller = [[Whfeaturedviewcontroller alloc] Initwithi    Dentifier:identifier]; } else if ([identifier isequaltostring:@ "Image"]) {viewcontroller = [[Whimageviewcontroller alloc] Initwithidentif    Ier:identifier];    } else if ([identifier isequaltostring:@ "Settings"]) {viewcontroller = [[Whsettingsviewcontroller alloc] init];    } [self Pushviewcontroller:viewcontroller animated:animated]; [ViewcontrolleR Release];} 

The "bad taste" is the excess of conditional branching and the code that is identical in each branch. It is forced to modify the code when it needs to be expanded, thus undermining the principle of open closure. After summarizing the rules, I think we should construct a table or dictionary model that maps from identifier to corresponding controller names. The implementation of the time through the Setupmodel to build the mapping table, and then the specific use of the table mapping, refactoring code as follows:

Refactoring One:

-(void) Setupmodel {if (Self.model = = nil) {Self.model = [nsdictionary dictionarywithobjectsandkeys:@ "WHA                      Rticleviewcontroller ", @" article ", @" Whsurvivalkitviewcontroller ", @" Survivalkit ",                      @ "Whsurvivalkitviewcontroller", @ "Search", @ "Whfeaturedviewcontroller", @ "Featured",                      @ "Whimageviewcontroller", @ "Image", @ "Whsettingsviewcontroller", @ "Settings",    NIL]; }}-(void) Pushviewcontrollerwithidentifier: (NSString *) identifier animated: (BOOL) Animated {NSString *    Identifiernamespace = [identifier identifiernamespace];    NSString *controllerclassname = [Self.model objectforkey:identifiernamespace];    Class Klass = nsclassfromstring (controllerclassname);    Uiviewcontroller *viewcontroller = [[Klass alloc] initwithidentifier:identifier];    [Self Pushviewcontroller:viewcontroller animated:animated]; [Viewcontroller release];} 

Refactoring not only avoids excessive conditional branching but also cleans up the identical code in each branch. The lack of the table model data or the inclusion in the code, so you want to hide these and code-independent data through the plist file. The refactoring code is as follows:

Refactoring two:

-(void) setupmodel{    if (Self.model = = nil) {        nsstring *plistfilepath = [[NSBundle mainbundle] pathforresource:@ " Model "oftype:@" plist "];        Self.model = [Nsdictionary Dictionarywithcontentsoffile:plistfilepath];    }} -(void) Pushviewcontrollerwithidentifier: (NSString *) identifier animated: (BOOL) Animated {    //implementation with "refactoring One"}

Now, everything looks pretty. However, it may be some time later that the mapping between the original indentifier and the controller name is regular, so the mapping table may not be needed at all. Try it!

Refactoring Three:

-(void) Pushviewcontrollerwithidentifier: (NSString *) identifier animated: (BOOL) Animated {    NSString * Identifiernamespace = [identifier identifiernamespace];    NSString *controllerclassname = [NSString stringwithformat:@ "wh% @ViewController", Identifiernamespace];    Class Klass = nsclassfromstring (controllerclassname);    Uiviewcontroller *viewcontroller = [[Klass alloc] initwithidentifier:identifier];    [Self Pushviewcontroller:viewcontroller animated:animated];    [Viewcontroller release];}

After several stages of refactoring, the code is not only "slimming", but logic is clearer. Through the process of hard coding to the model to the law, you should see not just the code that is constantly improving, but also the iterative nature of refactoring and endless. Any design and implementation can only be justified in a certain situation and stage, and there is no perfect answer for ever.

Source: http://blog.csdn.net/lbj05/archive/2011/04/20/6336851.aspx

Ios-iphone Development refactoring: From hard coding to model to law

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.