DEV Community

Discussion on: Six Things You Thought Senior Devs Did (But We Don't)

Collapse
 
antonfrattaroli profile image
Anton Frattaroli

However it's worded, it works. This article gives a good example: axelerant.com/resources/team-blog/...

//Cyclomatic Complexity = 3. Npath Complexity = 4.
public int function (a, b) {
  If (a>b) {
    $return_value = a;
  } else {
    $return_value =b;
  }
  return $return_value;
}

// Above code can be rewritten as below.

//Cyclomatic Complexity = 2. Npath Complexity = 2.
public in function (a,b) {
  if(a >b) {
    return a;
  }
  return b;
}
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
codemouse92 profile image
Jason C. McDonald

Precisely.

The only time you might need an explicit else in my experience is when one or more conditions do not contain logic to leave the context (e.g. a return, break, or continue), and you have a block of code which should only be executed if none of the conditions were met.