DEV Community

Discussion on: Writing Clean Code

Collapse
 
kiritchoukc profile image
KiritchoukC

I tried to get my functions the least indented possible using early exit.
It also helps readability in my opinion.

So instead of

function doStuff(myInput){
  if(myInput === "OK"){
    var foo = getFoo()
    if(foo){
      return "SUCCESS"
    }
  }
}

I do

function doStuff(myInput){
  if(myInput !== "OK"){
    return
  }

  var foo = getFoo()
  if(!foo){
    return
  }

  return "SUCCESS"
}
Collapse
 
ganderzz profile image
Dylan Paulus

Agreed!
I think it also helps in figuring out what a function does. It's easy to see what a function returns--especially in a dynamic language like JS.