Do we even need if/else?
Tags: javascript, react, typescript, codequality
Published: 2021-12-07 05:58:08

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.


![clean code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jcy0oh31fiabvyp1lo8g.jpg)

_Lets take a simple example to start with;_

### if/Ternary

``` javascript
const isWeekend = (day) => {
  let message;
  if (day === 'sunday') {
      message = "Its a weekend";
  }
  return message;
};
```
We have a function `isWeekend` which takes a `day `and 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:

```javascript
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:

```javascript
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.
```javascript
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.

```javascript
// 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.
```javascript
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`