'Using side effects in control flow [closed]

Is it considered bad practice to have side effects in an if statement? For example, something like this:

'use strict';
let a = 2;
if ( a++ > 2) {
    // ...
}

Or:

'use strict';
let a = 2;
if ( a++ > 2, a) {
    // ...
}

Or:

'use strict';
let a = 2;
if ( ++a > 2 && a++) {
    // ...
}

If so, why would that be frowned upon, and what might be a better approach?



Solution 1:[1]

Yes, it is considered as a bad practice by some (me included). There are linter rules to avoid this convention.

Why?

Because it's less readable. You want a single instruction to perform a single action. a++ vs ++a not only does something in expression (which can already be considered clumsy), but also doesn't seem to be easiest to read. I always need to think for a moment whether the value returned is a or a + 1.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 E_net4 - Mr Downvoter