@jamey @meredith_matthews it's probably legal in js, I haven't tested it. Actually... It's not, I just tried. And browser js isn't real thrilled with if-statement version either. My context is that you see the if- version in JS lot and it looks incorrect to me, and I shortened for tweet-brevity, but node doesn't seem to throw a syntax error reliably
@meredith_matthews I guess I'd usually choose something like the if-statement version, come to think of it.
My old college advisor described something he called "programming by successive strengthening of invariants" (which is a super awkward mouthful, but he's a CS prof and not an English major, so...)
The idea is that as you go through a function in order, it establishes more guarantees you can rely on. An if-statement checking for invalid input should return or throw, and then for the rest of the function you know the input is valid. At the end of a loop you might have proved that the sum of the elements of an array is in a particular variable, and you can rely on that for the rest of the function. etc
So I'd usually write an if statement that returns when there's something simple to report, and then, without any else, I'd return the thing that needs a more complicated computation.
Here's a Python example I wrote the other day: https://github.com/outreachy/website/blob/40dae3e5c5ae5c50b464544daea686ca05a3160c/home/models.py#L4711-L4714
@meredith_matthews On the other hand, ternary conditional can be great to make it clear that a variable always has some value assigned to it, and it's just a question of which value. I especially like that when I can then declare a variable to be constant/immutable, which some languages don't allow if you assign to the variable from multiple branches of an if statement. But that isn't usually a consideration for a return statement.
@meredith_matthews haha, going the other direction, a friend pointed me at this essay the other day: http://conal.net/blog/posts/the-c-language-is-purely-functional
@grainloom @jamey @meredith_matthews speaking of, my personal experience is with playing with Racket. I've yet to get the hang of metaprogramming, so maybe I don't even lisp, but I'm willing to buy the js-as-lisp meme simply because it feeeeels the same. Only those two languages give me the same gleeful anarchic joy of diving in, scattering some values around, frolicking through some proceedures and generally writing ridiculous code I refuse to be ashamed of. It's *fun*.
@grainloom @meredith_matthews Yeah, I dunno if metaprogramming is key to "the lisp experience" or whatever... but I think most languages that don't try to statically help you avoid bugs have some degree of that "gleeful anarchic joy" (which is a wonderful turn of phrase for this btw). Even just a Jupyter Notebook can be that kind of fun. Having a compile step that repeatedly tells me "no, this won't work, try again" is something I really value, but it can definitely be a buzz-kill. 😅
return b ? x : y;
says "Here's where I return something, and here's what it is: a ternary expression.".
The other option is just a minified `if...else`. Sometimes you need to return inside a conditional, but I'd prefer not to. Having a single place where you return (or as few as possible) is nice.
If there are side-effects of building either of the values, or if there's other stuff happening that depends on the same conditional, then returning from an if/else would make sense.
Also, situations might come up where it's difficult or opaque to build one of the values in-line. If the values don't already exist as variables, and can't be built in-line, then one'll probably use the if/else format.
@meredith_matthews I'd be guided by how lexically compact bool, this and that are. If the whole ?: can be read easily in one (or, maybe, three) line(s) then that seems preferable as it emphasises that there's always a value returned. Otherwise the if form.
This Mastodon instance is for people interested in technology. Discussions aren't limited to technology, because tech folks shouldn't be limited to technology either!