Better conditions in your code¶
Conditions are essential to programming. if loops, for loops, even the smallest pieces of code generally include some kind of condition. Today we will see how we can write better and simpler conditions using tricks specific to javascript.
Conditions without comparison (Boolean conversion)¶
Typically when we want to have a condition we write a comparison between several variables or between variables and numbers. This is just a way to get a Boolean (either true or false) value. In javascript, variables are converted (coerced) to the required type as needed, so we can use that to our advantage.
Numbers, strings and other types can be used as Booleans to help us simplify conditions. When converted to booleans, numbers evaluate to true, except for 0 (zero) which evaluates to false. If we plan ahead we can use this to our advantage. Here is an example of a value select that uses numbers to simplify conditions:
var showChart_VS = omni.createValueSelect ({
y: {name: 'Yes', value: 1},
n: {name: 'No', value: 0}
});
[...]
omni.onResult ([], function (ctx) {
if (ctx.getNumberValue('showChart')) {
// Show chart
} else {
// don't show chart
}
});
This is a very basic example but we can already see the advantage between using if (variable) and if (variable != 0). The only downside is that if the variable (showChart in the example) is an empty string, undefined or any other value that evaluates to false, we will be triggering the same behaviour as if it was 0. This behaviour might or might not be desired depending on the situation, so be mindful of it.
All the values that evaluate as false are called “falsy” in javascript jargon; those who evaluate to true are called “truthy”. You can learn more about them on the Falsy article and Truthy article , respectively.
Tip
Falsy and Truthy values can also be combined with the “NOT” (!) operator. (!number) is the same as if (number == 0).
Ternary operator (ifs without ifs)¶
One of the most frequent uses of if clauses is when we have two possible behaviours; the basic if ... else. Sometimes it even feels wasteful an inefficient to write 5-10 lines of code in what is a simple “A or B” decision. It is in those cases that the ternary operator becomes extremely useful.
The ternary operator is the perfect choice for simple “A or B” decisions that can fit in one line. This expression returns one value if the condition is true and another if the condition is false. Technically it can do much more than that, but when you increase complexity… you should probably use an if.
Let’s take a look at an example. We want to know if we are dealing with a car (4 wheels) or a bike (2 wheels), the if version of that situation would be:
var wheels;
if (isCar) { // isCar is a boolean or a falsy/truthy value.
wheels = 4;
} else {
wheels = 2;
}
In contrast, if we use the ternary operator we can do it in a single line:
var wheels = (isCar) ? 4 : 2;
As you can see it is more simple and faster to understand. Ternary operators work also with function calls or any other expression, not just with numbers and assignments.
The general syntax of the ternary (also called conditional) operator is: condition ? exprIfTrue : exprIfFalse. You can also have the expressions be ternary operators, but at that point you should probably use if instead.
Note
You can learn more about the Ternary operator on Mozilla’s JS Documentation.
Stealthy conditions (Implicit conditions to avoid errors)¶
But what if the ternary operator is still too long or too redundant? Then we are in luck thanks to javascript’s implicit conditions. Let’s take a look at the common situation in which we want to know the value of a variable inside of onResult and make sure it is not undefined but we don’t want it as a trigger.
Maybe the calculations can be carried out if the variable is undefined by simply assuming it has a value (for example 0). Typically you could use if statements like this:
omni.onResult ([], function (ctx) {
var dummy;
if (ctx.getNumberValue('dummy') == false) { // 'dummy' is zero, undefined, empty string...
dummy = 0;
} else { // optional, just for completeness
dummy = ctx.getNumberValue('dummy');
}
});
If we leverage the power of the ternary operator, we can reduce it to a single line:
omni.onResult ([], function (ctx) {
var dummy = (ctx.getNumberValue('dummy') ? ctx.getNumberValue('dummy') : 0;
});
But it gets long very easily 1 and it seems wasteful to call getNumberValue twice. There is a much shorter version of this:
omni.onResult ([], function (ctx) {
var dummy = ctx.getNumberValue('dummy') || 0;
});
We are using here the “OR” operator (||) to replicate the behaviour of the code samples above. We can also use the “AND” operator (&&) in the fashion. This operation is not limited to binary options, we could even concatenate several expressions for multiple options.
Here is a short explanation of the behaviour of Logical Operators :
Syntax |
Returns |
|---|---|
|
First Truthy expression or the last expression |
|
First Falsy expression or the last expression |
|
First non-nullish expression or the last expression |
Warning
A nullish is an expression equivalent to null or undefined. The ?? operator Nullish coalescing operator is a fairly new addition to javascript as the best way to set default values. However, our BB doesn’t like it and complains a lot about it (even though it still executes it correctly) so only use it if you totally know what you are doing.
This expressions can be combined but, as we said before, at that point you’re probably better off using plain old if statements.
Tip
The expr# in the table can be a value or a function call. You can dummy && ctx.addHtml('text') to display an HTML message only if dummy is not Falsy, but BB will complain (showing a warning triangle), so I cannot recommend doing this as the default behaviour.
After all this talk about fancy conditions, it is important to remember that the only reason we should be using them plain old if statements is for simplicity. So resist the temptation to turn all your if into ternary operators; I know it’s hard 2 but everyone will thank you for that.
As a rule of thumb, both ternary operators and implicit conditions should not be used if the whole statement doesn’t fit in 1 (2 at most) lines.
Footnotes