DEV Community

Discussion on: Question: Is this a terrible idea?

Collapse
 
harleybl profile image
harleybl • Edited

I have another method for the isEmpty case, but often I find it more readable to have if statement logic checks for a positive condition rather than a negative one. I read somewhere, I think it was Clean Code, that using positive logic requires less cognitive load when reading through the code. Especially when you are checking for more than one condition.

Which is more readable?

if (!isEmpty(arr) || !isEmpty(arr2) {
  doSomething()
} else {
  doNothing()
}

or

if (any(arr) || any(arr)) {
  doSomething()
} else {
  doNothing()
}

I prefer the second.

Note: isEmpty is an rxjs operator since that could be confusing I will call it something else like safeIsEmpty.

Collapse
 
pigozzifr profile image
Francesco Pigozzi

Just like you said: positive logic requires less cognitive load

For that reason, checking for a negative logic first and quit if true should be safer, more concise and readable than nesting two different logic inside an if/else statement