Scribe-Android
Scribe-Android copied to clipboard
Fix linting issues caused due to methods with too many return statements
Terms
- [X] I have searched open and closed feature requests
- [X] I agree to follow Scribe-Android's Code of Conduct
Description
Methods with too many return statements can be hard to read. Aim to have a single point of exit from a method, which makes the code more understandable and easier to debug.
Before fixing:
fun getDiscount(category: String): Int {
if (category == "A") return 10
if (category == "B") return 20
if (category == "C") return 30
return 0
}
After fixing:
fun getDiscount(category: String): Int {
return when (category) {
"A" -> 10
"B" -> 20
"C" -> 30
else -> 0
}
}
More about this can be read from the docs
These can be identified across the codebase by the following steps:
- Enable the ReturnCount rule in detekt.yml.
- Run ./gradlew detekt to identify methods with too many return statements.
- Refactor methods to reduce the number of return statements.
Contribution
I would love to work on this and am more than willing to help anyone solve this issue. 😄