1

In javascript you can't use statements in expressions. Because of this you are forced to use logic and ternary operators in an expression when you want to use if or switch etc.. Since I didn't want to use ternary operators everytime I do a condition check, I've written simple functions to do condition checks.

Using them I write code like this:

this.getEvent('aFrame').on(() => _if(this.isInTextEditor()).true(() => _if(this.getConfig('typeFrame')).true(() => _switch(this.state, {
    resume: () => this.bufferStack.check(() => this.animation.nextFrame(this.bufferStack.pop()))
})).false(() => _switch(this.state, {
    resume: () => this.animation.nextFrame()
}))));

Better formatted to understand:

this.getEvent('aFrame').on(() =>
    _if(this.isInclined()).true(() =>
        _if(this.getConfig('typeFrame')).true(() => _switch(this.state, {
            resume: () => {
                this.bufferStack.check(() => this.animation.nextFrame(this.bufferStack.pop()));
            }
        })).false(() => _switch(this.state, {
            resume: () => {
                this.animation.nextFrame();
            }
        }))
    )
);

The _if and _switch functions are:

const _switch = (a, obj) => {
    return obj[a] && obj[a]();
};

const _if = val => {
    return {
        true(fn) {
            val && fn();
            return this;
        },
        false(fn) {
            !val && fn();
            return this;
        }
    };
};

Everything is working fine with this code. But I just want to know if there are any downsides using this instead of ternary or logical operators:

this.getEvent('aFrame').on(() =>
    this.isInclined()
        && ((this.getConfig('typeFrame')
            ? (this.state === 'resume'
                && this.bufferStack.check(() => this.animation.nextFrame(this.bufferStack.pop())))
            : (this.state === 'resume'
                && this.animation.nextFrame()))
        )
);

Or simply using statements:

this.getEvent('aFrame').on(() => {
    if (this.isInclined()) {
        if (this.getConfig('typeFrame')) {
            switch (this.state) {
                case 'resume':
                    this.bufferStack.check(() => this.animation.nextFrame(this.bufferStack.pop()));
                    break;
                default:
                    break;
            }
        } else {
            switch (this.state) {
                case 'resume':
                    this.animation.nextFrame();
                    break;
                default:
                    break;
            }
        }
    }
});

Note: This question was moved from Stack Exchange Code Review.

2
  • You appear to have reinvented what a functional programmer would call monads and pattern matching. You might want to take a look at Haskell if you're not already familiar with it. – Ixrec Jun 11 '16 at 15:53
  • Looking into monads and pattern matching right now, thanks. – MindlessRanger Jun 11 '16 at 15:56
3

The benefit of an if expression is its result can be assigned to a variable, like:

var result = _if(...

Or perhaps its result is directly used in an expression. This isn't the case in your example. All the calculation is done by side effect. You are essentially ignoring the result of the if expressions, so they may as well be statements.

I'm all for the benefits of an if expression, but if you don't actually use the result, there's no need for the extra visual noise it requires.

Also, if this style of JavaScript appeals to you, you should know that there are several existing functional programming libraries like Ramda that can help you avoid reinventing the wheel, and give you better examples of idiomatic usage.

1
  • Although it cannot be seen in the snippets I provided, I have created _if to be able to access the result of it later when needed without having to type the condition again, so I am using the result. And thanks for the link, I'm trying to get used to functional programming and wanted to use a language I'm familiar with, that library will help me a lot. Thanks. – MindlessRanger Jun 11 '16 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.