The constant literal should be placed on the right-hand side of the comparison operatorSW-R1003
Comparing values in Swift follows a specific syntax for readability and maintainability. One of the best practices is to put the constant literal on the right-hand side of the comparison operator. This makes it easier to read and understand the comparison's intent, especially when comparing variables or expressions. Placing the constant literal on the left-hand side can result in unintended consequences, such as accidental assignment instead of comparison operator usage.
In Swift, it is crucial to follow the best practices and syntax conventions, as they help to prevent bugs and make the code more readable. Placing the constant literal on the left-hand side can lead to unintended behavior, as it can be interpreted as an assignment operator instead of a comparison operator. This can result in bugs that are challenging to identify and fix.
To fix this issue, make sure to place the constant literal on the right-hand side of the comparison operator.
Bad Practice
if 10 == num {
// some code
}
Recommended
if num == 10 {
// some code
}