- Use descriptive names for variables, functions, and types.
- Follow camel case for variable and function names (e.g.,
myVariable
,calculateResult()
). - Use uppercase camel case for type names (e.g.,
MyClass
,PersonModel
). - Prefix class properties with
self
to differentiate from method arguments and local variables.
- Indent using 4 spaces.
- Use spaces around operators and after commas.
- Place opening braces on the same line as the associated statement or declaration.
- Place
else
statements on the same line as the closing brace of the previousif
statement.
- Use
//
for single-line comments and/* ... */
for multi-line comments. - Write descriptive comments that explain the purpose of the code.
- Avoid excessive commenting for obvious or self-explanatory code.
- Use
if
andguard
statements for conditions and early exits. - Prefer the
for-in
loop for iterating over collections. - Use
switch
statements for multiple conditions with associated values. - Use
defer
to clean up resources or perform actions before exiting a scope.
- Use Swift's
try-catch
mechanism for handling errors. - Avoid using exceptions for flow control.
- Create custom error types for specific error cases.
- Use
throws
in function declarations to indicate that it can throw an error.