For example, take a look at the following code snippets :
Early Return
if(!user){
return;
}
//Do something..
Else condition
if(user){
//do something
}
Which one do you tend to use more often?
I think you are looking for clause guard, this refactoring method or way is if the function fails, or get the answer at any point, make the return earlier posible.
If you make a lot of if/else block you can have a lot of indentation an unefficient code to read.
I always go with the early return. Tons and tons of curly brackets due to nested IF'ing tends to lead to unreadable code or code that is fragile. If you know you've got nothing left to do, just leave the function and be done with it.
Joe Clark
Full-stack developer specializing in healthcare IT
I constantly tell myself when coding: "bug out early"
Sometimes I use an else if the situation calls for it, which is rare, but if I can exit a function/method early on, I'd rather do that. It's a readability thing, I think.