Technical best practices: Ternary operator

Every ServiceNow developer should read the technical best practices every now and then.

I agree with most of the things that are written in the guide.

However, there is one thing that I will never follow. I even consider it a really bad practice: Writing if/else statements for simple conditions that need to return an expression to avoid using the ternary operator.

This is not a fancy trick, just writing code that is actually easier to read.

Just one sentence that is easy to understand at a glance:

var result = (x == y) ? a : b;
Code language: JavaScript (javascript)

Instead of this:

if (x == y) {
    result = a;
}
else {
    result = b;
}
Code language: JavaScript (javascript)

But what about less experienced developers that might be updating your code in the future and might find it confusing?

They can learn and improve their coding skills, right?

If you hire a developer who can’t google “javascript question mark” and figure it out, then you have bigger problems to worry about.

More info: Conditional (ternary) operator