[Original]ios simplifies redundant code

Source: Internet
Author: User

A smart home in Shenzhen to develop the ipad version, the existing version of the iphone has been modified, the app's interface using Xib. Xib is fast and efficient compared to the code-to-write interface, but is poorly maintainable and readable. After seeing the code, I was in a very complicated mood.

Any programming area, anyone can participate, take iOS development, some people write code is able to execute, but not only requirements are able to execute, when the function of the project is constantly increasing, the untidy writing style will let people look to vomit, do not talk about maintainability, it is a mess.

 else if (devtypeid==13| |        devtypeid==14) {Xrdimmingviewcontroller *DIMMINGVC; if (iphone6p) {DIMMINGVC = [[Xrdimmingviewcontroller alloc]initwithnibname:@] xrdimmingviewcontroller-6p        [Bundle:nil]; } else if (iphone6) {DIMMINGVC = [[Xrdimmingviewcontroller alloc]initwithnibname:@] Xrdimmingview        Controller-6 "Bundle:nil"; } else {DIMMINGVC = [[Xrdimmingviewcontroller alloc]initwithnibname:@ "Xrdimmingviewcontroller" b        Undle:nil];        } Dimmingvc.devtypeid = Devtypeid;        Dimmingvc.devid = DevID;        [Self.navigationcontroller PUSHVIEWCONTROLLER:DIMMINGVC Animated:yes];    Return        } else if (devtypeid==15) {Xrcolorviewcontroller *COLORVC = [[Xrcolorviewcontroller alloc] init];        Colorvc.devtypeid = Devtypeid;        Colorvc.devid = DevID;        [Self.navigationcontroller PUSHVIEWCONTROLLER:COLORVC Animated:yes];    Return } else if (Devtypeid>=31&&devtypeid<=34) {Xrcurtainviewcontroller *CURTAINVC; if (iphone6) {CURTAINVC = [[Xrcurtainviewcontroller alloc]initwithnibname:@ ' XRCurtainViewController-6 ' b        Undle:nil]; } else if (iphone6p) {CURTAINVC = [[Xrcurtainviewcontroller alloc]initwithnibname:@] Xrcurtainvie                    wcontroller-6p "Bundle:nil"; } else {CURTAINVC = [[Xrcurtainviewcontroller alloc]initwithnibname:@ "Xrcurtainviewcontroller" b                    Undle:nil];        } curtainvc.devicetype = Devtypeid;        Curtainvc.curtaindeviceid = DevID;        [Self.navigationcontroller PUSHVIEWCONTROLLER:CURTAINVC Animated:yes];    Return }

It is entirely possible to write a common method to determine the device type to load the corresponding xib.

-(NSString *) Selectviewcontrollerxibname: (NSString *) className {    nsstring *str;    if (iphone6p) {        str = [ClassName stringbyappendingstring:@ " -6p"];    } else if (iPhone6) {        str = [ClassName strin gbyappendingstring:@ "-6"];    } else {        str = className;    }    return str;}

For example, the following code can be streamlined, so that the logic of the program is clear

Before streamlining: else if (devtypeid==13| | devtypeid==14)    {        Xrdimmingviewcontroller *DIMMINGVC;        if (iphone6p)        {            DIMMINGVC = [[Xrdimmingviewcontroller alloc]initwithnibname:@ ' xrdimmingviewcontroller-6p ' Bundle:nil];        }        else if (iphone6)        {            DIMMINGVC = [[Xrdimmingviewcontroller alloc]initwithnibname:@] XRDimmingViewController-6 [Bundle:nil];        }        else        {            DIMMINGVC = [[Xrdimmingviewcontroller alloc]initwithnibname:@ "Xrdimmingviewcontroller" Bundle:nil ];        }        Dimmingvc.devtypeid = Devtypeid;        Dimmingvc.devid = DevID;        [Self.navigationcontroller PUSHVIEWCONTROLLER:DIMMINGVC Animated:yes];        return;    }

After refinement: else if (devtypeid==13| | devtypeid==14) {    Xrdimmingviewcontroller *DIMMINGVC = [[Xrdimmingviewcontroller alloc]initwithnibname:[self Selectviewcontrollerxibname:nsstringfromclass ([Xrdimmingviewcontroller class])] bundle:nil];    Dimmingvc.devtypeid = Devtypeid;    Dimmingvc.devid = DevID;    [Self.navigationcontroller PUSHVIEWCONTROLLER:DIMMINGVC Animated:yes];    return;}

[Original]ios simplifies redundant code

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.