New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Report the usage unneccessary function to invoke a lambda argument #5836
Comments
inline fun
This should be a rule for the I imagine that this rule should also check for things like: fun toDomain(i: Int) = i.toString()
// no compliant
listOf(1).map { it.toDomain() }
// compilant
listOf(1).map(::toDomain) // no compliant
listOf(1).map { it.toString() }
// compilant
listOf(1).map(Int::toString) This case is probably the same as the one before. // no compilant
val a: (Int) -> String = { it.toString() }
// compilant
val a: (Int) -> String = Int::toString |
Hi, @BraisGabin is |
The problem of this rule is that I'm not sure what's more easy to read: .map(String::toBooleanStrict)
//or
.map { it.toBooleanString() } But if we add it in the performance rule set this should be good.
What do you think about |
Sounds better as your suggestion is easy to understand. (I named mine after |
I personally prefer the first one. But w/o a rule I miss using |
No, I don't think this should be configurable. If we receive feedback about this we could reconsider it later. YAGNI. |
Hi @BraisGabin you can assign this to me |
From #6005 by @marschwar
What do you mean by "To me this feels like is own rule."? Custom rule?
I feel that, once you get used to it, the code is easier to read. But for sure this is a controversial rule so we should decide if we want to add it. I wouldn't talk about performance here. This change seems that something that any optmizer like Proguard or R8 should fix. So this is a rule about readability and code consistency. |
The initial idea for a rule did not mention function references. It only talked about passing a lambda as a parameter instead of calling the lambda as the only thing in a lambda. To suggest the use of function references wherever possible to me could be its own, probably controversial, style rule. |
Oh! So you say to handle this two cases as different ones, right? fun test1(lambda: () -> Unit) {
thread {
lambda()
}.start()
} fun lambda() {
println("Hello world!")
}
fun test1() {
thread {
lambda()
}.start()
} I was thinking of them as the same thing but it have sense to handle them as different things. Do you think that is better to have two different rules instead of one with a configuration? To me the first case should be nearly always prefered. In the second case I think is better to use function reference but I agree that it is way more controversial. |
Edited: see below |
I take that back. Even though the second one can be written with method reference, it is not a violation of the rule because it does not cause a double lambda. |
Hi, @BraisGabin / @marschwar let me first introduce the rule of double lambda as I think is an important and recurring issue I face in the code reviews. After that we can create new style rule of lambda reference |
To me this sounds like a great idea. |
This issue is stale because it has been open 90 days with no activity. Please comment or this will be closed in 7 days. |
not stale |
@atulgpt are you still working on this? Or should we unassign you so anyone can pick it? |
Hi @BraisGabin yes let me submit the updated code.. I thought we are not taking the new rule. Let me finish this. As discussed above I will first create the rule of |
Report usage of unnecessary creation of function literal to call a lambda argument.
Below are a few examples
Expected Behavior of the rule
// uncompliant code
// compliant code
// alternate compliant code
// uncompliant code
// compliant code
Context
I came up with this issue while following the discussion at the comment. There could have some cases which I might not think of yet. So creating this issue for discussion on this proposal
The text was updated successfully, but these errors were encountered: