Skip to content

Instantly share code, notes, and snippets.

@eonil
Last active August 29, 2015 14:12
Show Gist options
  • Save eonil/ad2bbab4e126d4b427b5 to your computer and use it in GitHub Desktop.
Save eonil/ad2bbab4e126d4b427b5 to your computer and use it in GitHub Desktop.

Swift Coding Style

I have specific coding style for Swift.

  • Follow Apple's Cocoa coding style. (Camel case, uppercases for abbreviations, etc.)

  • Openign braces { at same line.

        func exampleFunction1() -> Int {
        }
    

    This is specifically because Swift does not support to put { at new line for closure syntax.

  • Fully descriptive name for any public interfacing elements.

  • You can use shortest name for local (that means will not be referred from another places) variable if their roles are (1)temporary, or (2)obvious and super-clean.

        func exampleFunction1() -> Int {
            let a   =   123
            let a1  =   a + 100
            return  a1
        }
    

    It is too inefficient to figure out formal name for all local variables. Anyway sometimes you need to use formal descriptive names if roles of the variables are not super-obvious.

  • Use tabs to align assignment.

        func exampleFunction1() -> Int {
            let a   =   123
            let a1  =   a + 100
            return  a1
        }
    

    Usually one tab is enough because most variable names are shorter then 4 letters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment