DEV Community

Discussion on: Optimize the use of if-else, you need a better selective structure!

Collapse
 
get_infinite profile image
Infinite Table

if the conditions are exclusive, I find early returns to be a very useful way of organising code:

if (condition1){ 
   // do stuff when condition1 is true
   return
}
if (condition2){ 
   // do stuff when condition2 is true
   return
}

// do stuff for condition 3, which is the last condition

return ...
Enter fullscreen mode Exit fullscreen mode