iOS Code Style Guideline Best Practice

Let’s tidy up your iOS project code

Firda Sahidi
iOSnesia
4 min readMar 3, 2020

--

Tidying dramatically changes one’s life. This is true for everyone 100% — Marie Kondo

I know that Marie Kondo it’s not an engineer, but she does say it right when it comes to tidying up. We coders always consider that is not necessary to tidy up our code, as long as it’s not caused a bug. Tidying up code has been underrated. Many advantages if we tidy up the code. Tidying up the code can reduce the size of your application, speeding up the performance of your applications, and reducing bug in your application.

Another great point if we tidy up your code, when new members come to your team, they can learn your code easier. In this article, I will conclude guidelines for tidying up your iOS Project.

Variable Order

Here’s the order of variable that should be implemented in every class.

1. IBOutlet

2. Public Let, then Var

3. Internal Let, then Var

4. Fileprivate Let, then Var

5. Private Let, then Var

I suggest IBOutlet in the top is because when there is conflict wether we want to merge, rebase, or anything, you can simply copy and paste all your code below the IBOutlet. Let after Var and Access-Control based order so you’ll know the most and least restricted variable of your project. I recommend making your variable private if it’s not really necessary to make it public, to make it less confusing when using which variable you want to use.

IBOutlet Name

For IBOutlet name, it is recommended to use camel case, with format [name][Type].

Example:

Spacing

You must use space between or after every variable, and after every function.

Example Wrong:

Example Right:

Variable Name

For variable name, it is recommended to use camel case and use US English spelling to match Apple’s API. Avoid mandatory (!) sign and use optional (?) instead. Don’t forget to set default value.

Example:

IBAction Name

For IBAction name, it is recommended to use camel case, with format [IBOutletName][ActionName].

Example Wrong:

Example Right:

Protocols

Instead of using extension beside main class, use extension below the main class. This will make it easier to clear the protocols if you no longer needed it.

Example Wrong:

Example Right:

Optionals

For avoiding nesting if let, it is recommended to use guard let instead of if let. Avoid if let if it’s not necessary.

Example wrong:

Example right:

Error Handling

Avoid using try! for error handling, because when it gets error, your iOS apps will be crashed. Use do catch is more recommended.

Example Wrong:

Example Right:

Unnecessary Code

There is a lot of example unnecessary code that not contain errors, but really affecting your apps size and performance. So, you need to delete it. These are the example of unnecessary code that you need to remove from your iOS project.

  1. Semicolons. Swift doesn’t need semicolons, so you need to get rid of this if you found one in your code.
  2. Parentheses when using if else statement or closure parameters.
  3. Emojis.
  4. Print methods. Use debug instead, or just delete it after you using it.
  5. Comments that not really necessary to be written.
  6. Unused Function/Variable. Delete it even if you feel you’ll need it someday. You can code it again, but you’ll likely to forget to delete that.
  7. Break statement in switch, except for the last condition.

So that’s all code style guidelines that I implemented in all my iOS projects. If you like to get more detailed code style guideline, check out these websites.

--

--

Firda Sahidi
iOSnesia

Indonesian iOS Developer with 6+ years of professional experiences. Recent MSc Fintech Graduate from University of Edinburgh. Check out my list below!