Punctuation
Semicolons
Don’t use semicolons. If you think you need to use semicolons, try to find a different way to do it.
Sole Exception
There is actually one case where semicolons are acceptable in the code: When a scope opens and you are logging both the entrance and exit of that scope. In this case, you may place both these log lines and the defer block on the same line of code.
Good | Acceptable |
---|---|
|
|
Braces
Along with Apple’s guidelines, curly braces ({
and }
) open on the same line and close on a new line. Curly braces that are the first non-whitespace character on a line must be indented to the same level as the code around its block, not inside it.
The main difference between our guidelines and Apple’s is the else
statement; we place them on the line following their if
’s closing brace, whereas Apple places them on the same line.
Braces are required, by all guidelines and by the compiler, for all if
, else
, guard
, switch
, while
, and for
statements.
Good | Bad |
---|---|
|
|
|
|
Parentheses
RULE-OF-THUMB When in doubt, more parentheses is better.
if
, else
, guard
, switch
, while
, and for
statements should not use parentheses unless necessary for compiling, or if their logic is so complex that parentheses are required for clear human andor compiler understanding. If this logic is very complex, consider moving it into its own function.
Always strive for clarity; when using multiple operators (mathematical, Boolean, or otherwise), use parentheses to explicitly group each pair. This ensures it does what you think it does, and future readers know what you intended it to do.
Good | Bad |
---|---|
|
|