Do we even need if/else?

Flow control is one of the first things we all learn as programmers.
We are going to learn about some of the alternatives we have, that from my point of view are generally more cleaner and safer.

Lets take a simple example to start with;

if/Ternary

const isWeekend = (day) => {
  let message;
  if (day === 'sunday') {
      message = "Its a weekend";
  }
  return message;
};

We have a function isWeekend which takes a dayand returns if its a weekend or not. Now this has an issue, but JavaScript itself doesn't give us any kind of error. We didn't return any message if its not sunday. So we can do something like this or add an else block:

const isWeekend = (day) => {
  let message = 'Its a working day :(';
  if (day === 'sunday') {
      message = "Its a weekend";
  }
  return message;
};

Now, as As the title says, do we even need if for this simple conditional block? No, we can use a ternary instead.
So we can update our function isWeekend like this:

const isWeekend = (day) =>
  day === "sunday" ? "Its a weekend" : "Its a working day :(";

// or

const isWeekend = (day) =>
  'Its a ${ day === "sunday" ? "weekend" : "working day :(" }';

Advantages of ternaries over ifs:

  • It forces to cover both if and else case.
  • Less code footprints.
  • More readable.
  • Another big advantage is that we can initialize a constant based on condition i.e.
const a = condition? 'value1' : 'value2';

We cannot achieve this using if else and will have to use let instead of const.

if/Switch

But what if we have to cover multiple conditions. Instead of using multiple ifs we should use the switch statement. Let take the same example, this time we need to have a condition for all the possible days i.e.

// multiple case switch program
  switch (day) {
    case "monday":
    case "tuesday":
    case "wednesday":
    case "thursday":
    case "friday":
      return "Its a working day :(";
      break;
    case "saturday":
    case "sunday":
      return "Its a weekend";
      break;
    default:
      return "thats not even a day";
      break;
  }

Plain Objects and the ??

We can even use plain objects and the nullish operator.

const daysMap = (day) =>
  ({
    "monday": "Its a working day :(",
    "tueday": "Its a working day :(",
    "wednesday": "Its a working day :(",
    "thursday": "Its a working day :(",
    "friday": "Its a working day :(",
    "saturday": "its a weekend",
    "sunday": "its a weekend",
  }[day] ?? "thats not even a day");

const isWeekend = ( day ) => daysMap(day);

Those who are not familiar with the ?? operator, it checks either it has a value or nullish (null or undefined). If day is nullish, then we use defaultValue, it not, use the value itself.

Conclusion:

There might be some cases where we have to use if/else. But, in my opinion, we can use the alternatives in most cases.

Any thought??

#coding #softwareengineering #productivity #cleancode #codingtips #javascript #webdev #devlife #programming #computerscience

28