Swift is a new programming language, first introduced by Apple in June 2014 for development on iOS, OSX, tVOS and WatchOS. We can call it the future of mobile development in Apple. Here are a few guidelines for programmers starting to learn this language.
Want to work in Objective-C, here is our [Objective-C guideline](https://github.com/Clustox/objective-c-style-guidelines)
- Name of a variable, parameter or type must be self-descriptive i.e., looking at name of the variable should inform reader about its nature.
let y = 5
let x = 10
let z = x + y
let firstNumber = 5
let secondNumber = 10
let sum = firstNumber + secondNumber
- Avoid spelling mistakes and abbreviations for variable names.
let tblView
let srchBar
var amnt
let tableView
let searchBar
var amount
- Parameter names must be there in function definition to clear ambiguity and provide clarity about nature and type of a certain parameter.
manager.remove(x)
manager.remove(notificationId: x)
- Function names must be short and precise. Avoid repetition of information.
manager.removeNotificationID(notificationId: x)
manager.remove(notificationId: x)
- Enum name must be Camel case.
- Values of Enum must be in Camel case.
public enum titles: String {
case settings = "Settings"
case feed = "Feed"
case friends = "Friends"
}
public enum Titles: String {
case Settings = "Settings"
case Feed = "Feed"
case Friends = "Friends"
}
- Avoid trailing white spaces.
- You can add as much of vertical spaces as you want.
- Using swiftlint will help you a lot in removing trailing spaces.
Swift differentiates constants from variables through keywords let and var respectively.
- Prefer using let unless you are sure that this identifer’s value is going to change in future.
- Easiest way to achieve is by declaring every parameter as let. Change only when compiler asks you to do so.
Swift has introduced a new idea of early exit by using keyword guard.
- guard works by executing else part first and then make an early exit if condition not met.
if self.childId != nil {
// do yur work
} else {
return
}
guard let childId = self.childId else {
return
}
// do your work
- Use access specifier on class level types, variables and functions to clear their scope.
- If no specifier is provided, by default it would be public.
- When defining variables, colon will be attached to the identifier instead of the type.
@IBOutlet weak var tableView :UITableView
@IBOutlet weak var tableView: UITableView
- Avoid force unwrapping of Optional values. Instead Use guard to unwrap the values.
let index = self.index
students.remove(at: index) //Crash if index is null
guard let index = self.index else {
return
}
students.remove(at: index)
In Swift, structs are way more powerful than their counterparts. Like class, struct can also have properties, methods, protocols, extensions and initialisers. So except inheritance, structs are very much classes in Swift and are really light-weight and faster when it comes to processing. Inheritance itself is not a good enough reason to use classes. It can be achieved by using protocols and Composition, which is highly supported in Swift.
- Preferred way to handle an error is to throw. The object thrown must conform to the ErrorType protocol. NSError conforms to this protocol. Enums are used for classifying errors.
enum SomeError: ErrorType {
case LowLevel
case MiddleLevel
case HighLevel
}
func doSomeStuff(argument: String) throws {
guard !argument.notRequired else {
throw SomeError.LowLevel
}
guard !argument.notRecommended else {
throw SomeError.MiddleLevel
}
guard !argument.restricted else {
throw SomeError.HighLevel
}
// do your stuff
}
- Use try catch finally to handle exceptions.
do {
let response = try argument.doSomeStuff
}
catch SomeError.LowLevel {
// handle Exception
}
catch SomeError.MiddleLevel {
// handle Exception
}
catch SomeError.HighLevel {
// handle Exception
}
// do your stuff
- Try documenting the code as much as you can. Every public variable, type, method must be documented.
- If changing an existing code or doing something in a way which is not recommended, make sure to document the reason properly.
- Follow a standard documentation style through out the app. Using three slash documentation will help you adding a default template for documentation.
- Documentation must add enough information about method name, variables types, function they perform and expected output of any method.
- Install VVDocumenter for standard documentation style
- Class, types and protocol names must be in camel case.
Notificationviewcontroller
onAPIcallcompleted
Errortype
NotificationViewController
onAPICallCompleted
ErrorType
- Variables, parameters and method names must start with a small letter. Rest of the name will be in camel case.
let Count, tableview
manager.Fetch(NotificationID: x)
manager.FetchAllNotifications()
let count, tableView
manager.fetch(notificationId: x)
manager.fetchAllNotifications()
- Remove commented and unused code from project.
- Switch, while, for, if, else and methods must have open brackets on same line but closing brackets on next line.
- A class must not exceed 200 lines of code. If it does, it's time to refactor.
- If you want to add any functionality to the existing code, add a new extension of that class instead of changing the current one.
- Separate business logic completely from controller code.
This guide is established through a collaborative effort of Team Clustox. Following are some of the sources we have taken inspiration from: