DEV Community

Discussion on: 5 ways to refactor if/else statements in JS functions

Collapse
 
aminnairi profile image
Amin • Edited

Hi there, great article, thank you!

I like to deal with all the error cases first, and then dealing with the heart of the function. Also I like to create small functions that deal with a single responsibility, this is easier to work with IMO.

I have sort of a kink with promises now and I like to put them everywhere. I like the simplicity of chaining functions.

"use strict";

const string = something => JSON.stringify(something);

const divide = (numerator, denominator) => new Promise((resolve, reject) => {
    if (typeof numerator !== "number") {
        return reject(new TypeError(`numerator should be a number in divide(${string(numerator)}, ${string(denominator)})`));
    }

    if (typeof denominator !== "number") {
        return reject(new TypeError(`denominator should be a number in divide(${string(numerator)}, ${string(denominator)})`));
    }

    if (0 === denominator) {
        return reject(new Error(`denominator should not be zero in divide(${string(numerator)}, ${string(denominator)})`));
    }

    return resolve(numerator / denominator);
});

divide(1, 2)
    .then(result => divide(result, 2))
    .then(result => divide(result, 2))
    .then(result => console.log(`After dividing by two three times: ${result}.`))
    // After dividing by two three times: 0.125.
    .catch(({message}) => console.error(`error while dividing: ${message}.`));

divide(1, 0)
    .then(result => divide(result, 2))
    .then(result => divide(result, 2))
    .then(result => console.log(`After dividing by two three times: ${result}.`))
    .catch(({message}) => console.error(`error while dividing: ${message}.`));
    // error while dividing: denominator should not be zero in divide(1, 0).
Enter fullscreen mode Exit fullscreen mode
Collapse
 
dhintz89 profile image
Daniel Hintz

This is an awesome demonstration! I need to start learning and living this pattern for larger functionality.

Collapse
 
rasmusvhansen profile image
rasmusvhansen

I am not sure turning inherently synchronous code into async is a good idea. Promises are OK (not great - observables are better in many cases) for async operations but I would definitely avoid introducing the complexity for inherently sync operations.

By using this pattern, you are forcing all your code to be async.

Collapse
 
sylwiavargas profile image
Sylwia Vargas

THANK YOU! YES, I was soooo tempted to touch on Promises but then majority of folks who read my blogs (maybe less so now?) are beginners so I'm planning to write a separate thing on promises in general. However, I'll link your comment in the article because I love your example! Thank you.

Collapse
 
eecolor profile image
EECOLOR

If you use async your code becomes a bit more natural:

async function divide(numerator, denominator) {
    if (typeof numerator !== "number") {
        throw new TypeError(`numerator should be a number in divide(${numerator}, ${denominator})`);
    }

    if (typeof denominator !== "number") {
        throw new TypeError(`denominator should be a number in divide(${numerator}, ${denominator})`);
    }

    if (0 === denominator) {
        throw new Error(`denominator should not be zero in divide(${numerator}, ${denominator})`);
    }

    return numerator / denominator;
});
Enter fullscreen mode Exit fullscreen mode

This however indicates that it would be probably be smart to remove the async and move that to a separate function. That would allow you to use the divide function in more scenario's.