user10402330
user10402330

Reputation:

Kotlin: redundant if statement

I have this code:

val leftEnoughRoom = if(fx1 > eachSideBesidesFace){
    true
}else{
    false
}

And get the warning:

This inspection reports if statements which can be simplified to single statements. For example:  
if (foo()) {
   return true
} else {
   return false
}
can be simplified to return foo().

What does it want me to do? When I do:

if(fx1 > eachSideBesidesFace){
    val leftEnoughRoom = true
}else{
    val leftEnoughRoom = false
}

Then leftEnoughRoom is not reachable below any more

Upvotes: 1

Views: 1929

Answers (1)

TheWanderer
TheWanderer

Reputation: 17824

fx1 > eachSideBesidesFace

is a boolean statement. You don't need the if-else:

val leftEnoughRoom = fx1 > eachSideBesidesFace

As a sidenote, you can click the underlined expression, hit Alt+Enter and then have Android Studio automatically optimize the code.

Upvotes: 10

Related Questions