iOS Code Style Guideline Best Practice
Let’s tidy up your iOS project code
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:
@IBOutlet weak var nameLabel: UILabel!
Spacing
You must use space between or after every variable, and after every function.
Example Wrong:
if a = 0 {TCDebugPrint(“hello”)} else {TCDebugPrint(“bye”)}
Example Right:
if a = 0 {TCDebugPrint(“hello”)} else {TCDebugPrint(“bye”)}
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:
let number: Int? = 0
IBAction Name
For IBAction name, it is recommended to use camel case, with format [IBOutletName][ActionName].
Example Wrong:
@IBAction func didTapLoginButton(_ sender: Any)
Example Right:
@IBAction func loginButtonTapped(_ sender: Any)
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:
class MyViewcontroller: UIViewController, UITableViewDataSource, UIScrollViewDelegate {
}
Example Right:
class MyViewcontroller: UIViewController {
}
extension MyViewcontroller: UITableViewDataSource {
}
extension MyViewcontroller: UIScrollViewDelegate {
}
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:
if let number1 = number1 {
if let number2 = number2 {
if let number3 = number3 {
// do something with numbers
} else {
fatalError("impossible")
}
} else {
fatalError("impossible")
}
} else {
fatalError("impossible")
}
Example right:
guard
let number1 = number1,
let number2 = number2,
let number3 = number3
else {
fatalError("impossible")
}
// do something with numbers
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:
let json = try! JSONSerialization.jsonObject(with: data, options: .allowFragments)
print(json)
Example Right:
do {
let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
print(json)
} catch {
print(error)
}
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.
- Semicolons. Swift doesn’t need semicolons, so you need to get rid of this if you found one in your code.
- Parentheses when using if else statement or closure parameters.
- Emojis.
- Print methods. Use debug instead, or just delete it after you using it.
- Comments that not really necessary to be written.
- 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.
- 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.