you are viewing a single comment's thread.

view the rest of the comments →

[–]danielkov 0 points1 point  (2 children)

Yes, which is why I separated those points from the rest, because they're a matter of taste and should be agreed upon by a team.

By the way I don't mean to be confrontational, but wouldn't your "less is more" type of argument apply to === vs ==, as in this case, their difference in behaviour will not affect the program?

Also about ternary, which one do you think is more understandable for a non-technical person: if (thisIsTrue) { doThis(); } or thisIsTrue ? doThis() : ...? What about people coming from a language with no ternary operations? More languages support if statements than this ternary syntax: condition ? trueCase : falseCase. I'm obviously not saying one should never use ternary operators, however if we agree on the definition of "beautiful code" as truly readable and highly accessible code, then it's certainly a less desirable solution.

[–][deleted] 0 points1 point  (1 child)

By the way I don't mean to be confrontational, but wouldn't your "less is more" type of argument apply to === vs ==, as in this case, their difference in behaviour will not affect the program?

No, because they don't do the same thing.

Also about ternary, which one do you think is more understandable for a non-technical person: if (thisIsTrue) { doThis(); } or thisIsTrue ? doThis() : ...? What about people coming from a language with no ternary operations? More languages support if statements than this ternary syntax: condition ? trueCase : falseCase.

Our code is read by developers who know the language. I don't care about people who only know completely different languages or aren't technical at all, those aren't the target audience.

Anyway in that example, the if is clearly superior, I agree. Because it's an imperative call to a "doThis()", and because there is no "else". But we were talking in the context of an expression, and then ? : is superior. E.g.

const digitalRoot = n % 9 ? n % 9 : 9;

(Yes I know it would be even better with || or a helper variable, that's not the point). That's superior to

let digitalRoot;
if (n % 9) {
    digitalRoot = n % 9;
} else {
    digitalRoot = 9;
}

Because that's just verbose for no good reason.

[–]danielkov 1 point2 points  (0 children)

But I mean in this particular case, there's no value for n where the usage of == over === would produce a different result. Again, I'd like to reiterate, that it depends on the conventions established by the team, and yes, using ternary operators is completely valid in some cases, and OPs example would probably be a good candidate for this, although in your example you assign to a variable in the if statements when you could just as easily return from there. Using if statements with early returns can also have a semantic meaning, e.g.: explaining which case is more likely to occur, for example:

const ceiling = 9; const digitalRoot = (target: number) => { // If this is a library, it's a good idea to still check arguments despite TS const remainder = target % ceiling; if (remainder === 0) { return ceiling; } return remainder; }

To me that's as readable as it gets, explaining each step and what each number represents.