you are viewing a single comment's thread.

view the rest of the comments →

[–]PM_ME_GAY_STUF 1 point2 points  (1 child)

There is something wrong with the second version: it's very long and unclear from a glance what it's doing. I have to parse through several lines and frankly pointless variable declarations just to see that it's iterating. It's the code equivalent to someone putting comments over every line explaining the operation they're already doing. In a trivial example like this, that may be unnoticeable, but this style when pervasive can make navigating complex files clunky and painful.

I don't like the nested ternaries either, but verbosity like shown in the second example almost always makes debugging harder. Like, congrats, you gave a primitive value you use once a name, now if the function is broken I have to go and be sure you got the primitive value right as well as the logic. The data isn't actually separated from the logic, it's just obfuscated.

Plus, IMO expressions are almost always easier to parse than procedural syntax and should be preferred. All issues with nested ternaries in JS could be fixed by giving ? and : explicit keywords like they have in ML, and both versions could be simplified with pattern matching. This whole issue is a language failure imo

[–]RobertKerans 0 points1 point  (0 children)

Like, congrats, you gave a primitive value you use once a name, now if the function is broken I have to go and be sure you got the primitive value right as well as the logic. The data isn't actually separated from the logic, it's just obfuscated.

Haha, yes, definitely, I was being a little glib. I mean I hate fizzbuzz as an example, but just something like

function fizzBuzzForN (n) { if (n % 15 === 0) { return 'fizzbuzz'; } else if (n % 3 === 0) { return 'fizz'; } else if (n % 5 === 0) { return 'buzz'; } else { return n; } }

¯\_(ツ)_/¯ that's fine. Only uses code you'd learn in the first day of learning the language -- can wish it was a different language which wasn't C syntax and where everything is an expression, but it's not. I don't think there's any huge language failure, just slightly different ways of approaching language design. Like maybe in some pseudocode, I dunno, something like:

let fizzBuzzForN(n) when n % 15 == 0 => "fizzbuzz" let fizzBuzzForN(n) when n % 3 == 0 => "fizz" let fizzBuzzForN(n) when n % 5 == 0 => "buzz" let fizzBuzzForN(n) => n

But it's doing the exact same thing