The ternary operator should not be used on the same line in Node.js. What for?

Consider the following code examples:

1.Sample

var IsAdminUser = (User.Privileges == AdminPrivileges)
  ? 'yes'
  : 'no';
console.log(IsAdminUser);

      

2.Sample

var IsAdminUser = (User.Privileges == AdminPrivileges)?'yes': 'no';
console.log(IsAdminUser);

      

2nd example which I really like and I code in this style but was told its the wrong way for no auxiliary reason.

Why is it not recommended to use a single ternary operator in Node.js?

Can anyone please provide a reason why this is so?

Thanks for the great help.

+3


source to share


5 answers


With all coding standards, they are generally designed for readability and maintainability. I guess the author finds it more readable on separate lines. A compiler / interpreter for your language will handle all of this. As long as you / your project has an established standard and stick to it, you will be fine. I recommend that the standards be reviewed, or at least reviewed by everyone involved in the project, before throwing them into the stone. I think if you decompose it into separate lines, you can also define an if / else conditional block and use it.

Be wary of rules in coding standards that have no excuse.



Personally, I don't like the ternary operator as it seems unnatural to me, and I always have to read this line several times to figure out what it does. I find the individual if / else blocks to be easier to read. Individual preference, of course.

+5


source


It is actually wrong to put ?

on a new line; although it doesn't hurt in practice.

The reason is a JS feature called Automatic Semicolon Insertion . "When a statement var

ends with a newline (no trailing comma, which indicates more declarations to follow), your JS interpreter should automatically insert a semicolon.

This semicolon would cause a IsAdminUser

boolean value to be assigned (namely the result User.Privileges == AdminPrivileges

). After that, the new (invalid) expression will start with a question mark for what you think is a ternary operator.

As mentioned, most JS interpreters are smart enough to admit that you have a newline that you don't have and to implicitly correct the 3-D operator. And while minifying your script, the newline is removed anyway.

So, in practice there is no problem, but you are relying on implicit fix of common JS engines. Better to write the ternary operator like this:



var foo = bar ? "yes" : "no";

      

Or, for large expressions:

var foo = bar ?
    "The operation was successful" : "The operation has failed.";

      

Or even:

var foo = bar ?
    "Congratulations, the operation was a total success!" :
    "Oh, no! The operation has horribly failed!";

      

+6


source


Because it's easier on the eye and easier to read. It's much easier to figure out what your first snippet is doing at a glance - I don't even need to read to the end of the line. I can just look at one place and immediately find out what values IsAdminUser

will have for what conditions. For the same reason why you shouldn't write entire if / else blocks on one line.

Remember that these are style conventions and are not necessarily supported by objective (or technical) reasoning.

+2


source


I totally disagree with the person who made this recommendation. The ternary operator is a standard feature of all "C" style languages ​​(C, C ++, Java, C #, Javascript, etc.), and most developers who code in these languages ​​are completely happy with the one-liner.

The first version looks strange to me. If I maintained the code and saw this, I would fix it down to one line.

If you need detailed information, use if-else. If you want neat and compact use the triple.

My guess is that the person who made this recommendation just wasn't very familiar with the operator, so found it confusing.

+1


source


Reason for which? and: on separate lines to make it easier to figure out what changed if your control source has a line-by-line comparison.

If you just changed stuff in between? and: and all on one line, the entire line can be marked as modified (depending on your comparison tool).

0


source







All Articles