Penguin Logic
Misuse of conditional statements are not only hard to read, but also more complex for the processor.
Today, I want to underline some points about misused or over engineered conditional statements.

This image makes me smile everytime I see, and is a very good fit for our topic :)

So what do I mean about misused or over engineered conditional statement. I guess, some of these examples may bring up discussions, please comment with your rejections or approvals. I am always open for critics and learn from anyone.

Let me underline that the concepts I discuss is more applicaple if you are using ECMAScript5 and above.

Let’s take some samples from my code reviews at NodeJS;

Undefined or Null Checks

1
2
3
4
5
if(typeof variable == 'undefined') {
doSomething();
} else {
doSomethingElse();
}

This is a very common check and just to be on the safe side, we may use such a conditional statement. What do I mean by safe side is;

1
2
3
if(typeof variable == "undefined") //no errors even variable is not declared.
if(variable == null) //throws ReferenceError: variable is not defined

We are using and pushing for strict mode and undefined check is generally irrelivant. You can define strict mode as below;

1
"use strict";

This mode already gives and error if you assign a value to a variable which is not defined. So you need to define the variable as “var variable;”

On the other hand, let me tell you something.. Consider your purpose of your if statement, what will you do if the variable is undefined or null or false or NaN, most probably the same thing that you do for each of these. Here is a secret for you; below are all falsy statements at javascript. So any variable having any of these value would go to else block.

1
2
3
4
5
6
false
0
""
NaN
null
undefined

Generally you only need to write a simple check as below;

1
2
3
4
5
if(variable) {
doSomething();
} else {
doSomethingElse();
}

Inline Conditional Checks

Consider the below inline condition;

1
2
3
4
// Javascript
function myFunc(message) {
var myVariable = message ? message : 'Default value for my variable';
}

At first look this is ok for many programming languages. C# even has a special operator ?? just for this purpose. So that you can write in C#;

1
2
// C#
var myVariable = message ?? 'Default value for my variable';

This can be taken as a tip, since it is not ok to do in other programming languages like C/C++, C# or JAVA and many more. We can do a similar thing with an already existing operator in javascript;

1
2
// Javascript
var myVariable = message || 'Default value for my variable';

Good ha :)

Another tip for Conditional (ternary) Operator is, you can do more than one operation in a condition block like below. However, by means of readability of your code, do it if you realy need it ;)

1
2
3
4
5
6
7
8
9
var myVariable = condition ? (
console.log('Your condition is true'),
assignAnother = 123,
assignedValueForTrue // The last block will be assigned to myVariable.
) : (
console.log('Your condition is false'),
assignAnother = 321,
assignedValueForFalse // The last block will be assigned to myVariable.
);

Again another end for another post. Thanks for reading and if you found this post useful, please share it. I appreciate all types of feedback and encouragement.