levinzonr / table

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RED Don't
if(condtion) {
  doSomething()
} else {
  doSomethingElse()
}
when(condtion) {
  true -> doSomething()
  false -> doSomethingElse()
}

Avoid nested ifs

Following logic in a deeply nested if else mess can be hard and error prone for the next developer. There are several ways of avoiding that.

Consider inverting the expression and returning:

// Hard to follow
if (item != null) {
   if (item.getFollowers() != null && item.getFollowers().get(someId) != null) {
       follower = item.getFollowers().get(someId)
   }
}

// Better
if(item == null) {
    return
}

if(item.getFollowers() == null || !item.getFollowers().contains(someId)) {
    return
}

follower = item.getFollowers().get(someId)

About