DEV Community

Esto Triramdani N
Esto Triramdani N

Posted on • Edited on

Avoid Else Statement In Your Function To Write a Clean Code

When I took programming class in college, one of fundamental topic is control flow, including if - else if - else statement.

if (a === 'one') {
  // do something here
} else if (a === 'two'){
  // do something here
} else {
  // I said, do something!!
}
Enter fullscreen mode Exit fullscreen mode

My lecturer taught me in a good way how to write and implement if statement. But I just remembered, I learn if statement subject before I learn about function.

It is okay if we use else outside function or in top level code, moreover we are still need else.

const sayHi = true;
if (sayHi) {
  console.log("Hi!")
} else {
  console.log("....")
}
Enter fullscreen mode Exit fullscreen mode

In a function that returned something, in most cases we don't even need the else statment. Here is the example.

function func(isRich: boolean){
  if (isRich) {
    return "I am rich!";
  } else {
    return "Can you give me a money, please?";
  }
}
Enter fullscreen mode Exit fullscreen mode

Since function will stop executing lines of code after return is written, we can just remove else statement.

function func(isRich: boolean){
  if (isRich) {
    return "I am rich!";
  }
  // intepreter, compiler, or anything won't execute codes bellow
  return "Can you give me a money, please?";
}
Enter fullscreen mode Exit fullscreen mode

When we need to check another condition that has been specified we might use else if statement and write the logic inside the block.

function grade(score: number) {
  if (score >= 90) {
    return 'A';
  } else if (score >= 80) {
    return 'B';
  } else if (score >= 70) {
    return 'C';
  } else if (score >= 60) {
    return 'D';
  } else {
    return 'F';
  }
};
Enter fullscreen mode Exit fullscreen mode

Or we can write it with switch-case.

function grade(score: number){
  switch (true) {
    case score >= 90:
      return 'A';
    case score >= 80:
      return 'B';
    case score >= 70:
      return 'C';
    case score >= 60:
      return 'D';
    default:
      return 'F';
  }
};
Enter fullscreen mode Exit fullscreen mode

Two examples above work fine. But I prefer this style.

function grade(score: number){
  if (score >= 90) return 'A';
  if (score >= 80) return 'B';
  if (score >= 70) return 'C';
  if (score >= 60) return 'D';
  return 'F';
};
Enter fullscreen mode Exit fullscreen mode

Thank you for reading!

Top comments (5)

Collapse
 
peerreynders profile image
peerreynders • Edited
import { match, when, otherwise, greaterThanEquals } from 'https://unpkg.com/patcom?module';

function grade(score) {
  return match(score)(
    when(greaterThanEquals(90), () => 'A'),
    when(greaterThanEquals(80), () => 'B'),
    when(greaterThanEquals(70), () => 'C'),
    when(greaterThanEquals(60), () => 'D'),
    otherwise((_) => 'F')
  );
}

console.log([91, 80, 79, 60, 59].map(grade)); // ["A", "B", "C", "D", "F"]
Enter fullscreen mode Exit fullscreen mode

Greetings from ECMAScript Pattern Matching

Collapse
 
estotriramdani profile image
Esto Triramdani N

Thank you for new insight!
That's new for me.

Collapse
 
peerreynders profile image
peerreynders • Edited

This week they'll decide whether it gets to stage 2 (as an ES feature). It's been lingering in stage 1 for the past 4 years.


As I'm expression oriented anyway, I'm perfectly happy with

function grade(score) {
  return score >= 90
    ? 'A'
    : score >= 80
    ? 'B'
    : score >= 70
    ? 'C'
    : score >= 60
    ? 'D'
    : 'F';
}
Enter fullscreen mode Exit fullscreen mode

but that rubs many people the wrong way 🤷

Collapse
 
shshank profile image
Shshank • Edited

Thanks for sharing article, I prefer the last style without else. That's my personal opinion. But as @peerreynders mentioned about ECMA Script Pattern Matching, it's a wow for me. Will give it a try for sure.

Collapse
 
mjcoder profile image
Mohammad Javed • Edited

Personally for me, I like the switch statement. Multiple else if statements just doesn't sit comfortably with me.