https://kotlinlang.org logo
#detekt
Title
# detekt
j

Javier

02/24/2024, 2:43 PM
Which is the reason to have
detekt - ReturnCount: Function generateProperties has 3 return statements which exceeds the limit of 2.
? I would like to avoid nesting and doing multiple returns is easy, readable and avoid nesting, for example:
Copy code
override fun generateProperties(
        callableId: CallableId,
        context: MemberGenerationContext?
    ): List<FirPropertySymbol> {
        if (callableId.callableName != atomicName) return emptyList()
        val owner: FirClassSymbol<*> = context?.owner ?: return emptyList()

        ...
    }
h

Harold Martin

02/24/2024, 3:47 PM
Having many returns in a function decreases readability since they can be easy to miss when glancing through and thus make it hard to reason about functionality. In general there's something of a code smell if a function can be called with lots of invalid arguments and suggests there may need to be some refactoring of business logic to the callers.
j

Javier

02/24/2024, 3:49 PM
I am not sure if I agree in all use cases. I want to skip that
generateProperties
function from the visitor does anything based on those statements, how can I do that with less code and/or being more readable?
e

eygraber

02/25/2024, 2:43 AM
Guard clauses (like in your
generateProperties
function) shouldn't count towards the return count for that rule. Although now that I think about it, that might not be the default behavior and you have to configure it.
j

Javier

02/25/2024, 9:45 AM
That is nice, hope it was true by default
37 Views