Giter Club home page Giter Club logo

ios-coding-style-guide's Introduction

Introduction

This style guide outlines the common coding conventions of the iOS Developers.

Table of Contents

Spacing

  • Indent must use 4 spaces. Be sure to set this preference in Xcode (defaults is 4 spaces).

  • Method braces and other braces (if/else/switch/while etc.) must open on the same line as the statement. Braces must close on a new line.

    For example: Objective-c

    if (user.isHappy) {
        // Do something
    }
    else {
        // Do something else
    }

    Swift

    if user.isHappy == true {
        // Do something
    }
    else {
        // Do something else
    }
  • Separate imports from the rest of your file by 1 space. Optionally group imports if there are many (but try to have less dependencies). Include frameworks first.

    For example: Objective-c

    #import <AwesomeFramework/AwesomeFramework.h>
    #import <AnotherFramework/AnotherFramework.h>
    
    #import "SomeDependency.h"
    #import "SomeOtherDependency.h"
    
    @interface MyClass

    Swift

    import AwesomeFramework
    import AnotherFramework
    
    class MyViewController: UIViewController {
        // class stuff here
    }
  • In Objective-C, use one empty line between class extension and implementation in .m file.

    @interface MyClass()
    
    // Properties - empty line above and below
    
    @end
    
    @implementation MyClass
    
    // Body - empty line above and below
    
    @end
  • When using pragma marks leave 1 newline before and after.

    For example: Objective-c

    - (void)awakeFromNib {
        [super awakeFromNib];
        // something
    }
    
    #pragma mark - Config Cell
    
    - (void)configCell {
        // something
    }

    Swift

    override func awakeFromNib() {
       super.awakeFromNib()
       // somthing
    }
    
    // MARK: -  Config Cell
    
    func configCell() {
       //something
    }
  • Prefer using auto-synthesis. But if necessary, @synthesize and @dynamic must each be declared on new lines in the implementation.

  • There should be exactly one blank line between methods to aid in visual clarity and organization.

  • Whitespace within methods should separate functionality, but often there should probably be new methods.

  • When doing math use a single space between operators. Unless that operator is unary in which case don't use a space. For example:

    index = index + 1;
    index++;
    index += 1;
    index--;
  • Colon-aligning method invocation should often be avoided. There are cases where a method signature may have >= 3 colons and colon-aligning makes the code more readable. Please do NOT however colon align methods containing blocks because Xcode's indenting makes it illegible.

    Good:

    objective-c

    // blocks are easily readable
    [UIView animateWithDuration:1.0 animations:^{
        // something
    } completion:^(BOOL finished) {
        // something
    }];

    swift

    UIView.animate(withDuration: 1.0, animations: {
        // something
    }, completion: { finished in
        // somthing
    })

    Bad:

    objective-c

    // colon-aligning makes the block indentation wacky and hard to read
    [UIView animateWithDuration:1.0
                     animations:^{
                         // something
                     }
                     completion:^(BOOL finished) {
                        // something
                     }];

    swift

    UIView.animate(withDuration: 1.0, 
                     animations: {
                        // something
                     }, completion: { finished in
                        // somthing
                     })

Comments

  • When they are needed, comments should be used to explain why a particular piece of code does something. Any comments that are used must be kept up-to-date or deleted.
  • Avoid block comments inline with code, as the code should be as self-documenting as possible. Exception: This does not apply to those comments used to generate documentation.

Developing....

References

ios-coding-style-guide's People

Contributors

vineetchoudhary avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

aman19ish

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.