Default parameters should always be positioned at the endSW-W1001
When defining functions in Swift, it is possible to provide default values for some of the parameters. When the function is called and the value of these parameters is not provided, the default value is used. However, it is important to note that default parameters should always be positioned at the end of the parameter list.
If the default parameters are not positioned at the end, it can lead to confusion for both the developer and the compiler. The problem arises when the developer tries to call the function and provide explicit values for some, but not all of the parameters. If the default parameters are not at the end, the compiler may interpret the values provided by the developer as values for the default parameters, leading to unexpected behavior.
In addition, placing default parameters at the end makes the function signature more readable and easier to understand. It is a common convention in Swift and adhering to it makes the code more consistent and easier to maintain.
Bad Practice
func someFunction(a: Int = 1, b: Int, c: Int = 2) {
// some code here
}
Recommended
func someFunction(b: Int, a: Int = 1, c: Int = 2) {
// some code here
}