News for hackers. Stuff that matters.
Re:Emerging JavaScript pattern: multiple return values by franciscop on Thursday October 29, 2020 @06:56AM (#24929138)
Please some admin (or you, Loige) add the "[2018]" tag since this is well established in 2020. It was "emerging to already there" back in 2018.

I also missed generators from this list, which are the "official" (but a lot more complex than the ones showed here!) way of having multiple async returns:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

Re:Emerging JavaScript pattern: multiple return values by ubertaco on Thursday October 29, 2020 @06:27AM (#24928943)
Returning an object and returning an array aren't exactly new concepts in Javascript? The only relatively-new part is destructuring assignment, but that's been around since 2015, since it was added in ES6.
Re:Emerging JavaScript pattern: multiple return values by ilaksh on Thursday October 29, 2020 @06:43AM (#24929072)
Right and the article is from 2018.

I have been using it for awhile.

Re:Emerging JavaScript pattern: multiple return values by joshribakoff on Thursday October 29, 2020 @07:19AM (#24929279)
Sure, but I’ve been returning objects for 10+ years
Re:Emerging JavaScript pattern: multiple return values by Drakim on Thursday October 29, 2020 @07:33AM (#24929372)
But you likely haven't been using destructuring assignment for 10+ years.
Re:Emerging JavaScript pattern: multiple return values by smegcicle on Thursday October 29, 2020 @10:05PM (#24938366)
lisp has destructuring-bind (and multiple-value-bind)
Re:Emerging JavaScript pattern: multiple return values by tinkertamper on Thursday October 29, 2020 @09:52AM (#24930458)
I’ve found returning a tuple with my validation functions to be very helpful.

  let [isValid, err] = validate(data)
It’s nice for a few reasons:

1. I know anything returned `err` is safe to display back to the user

2. Using the array (as opposed to an object) can allow me to name the values when I destructure so it can be used multiple times in the same block with useful naming.

3. I can destructure just the first half `[isValid]` if that’s all I care about.

4. More generally it helps keep the code readable. Other control flows might rely on some casting, assumptions, or exceptions but this way presents a nice customizable api for users writing the code, and a readable api for those reviewing the code.

Re:Emerging JavaScript pattern: multiple return values by noman-land on Thursday October 29, 2020 @10:40AM (#24930937)
You probably already know this but you can destructive just the second half as well.

`cont [, err] = validate(data)`

Re:Emerging JavaScript pattern: multiple return values by paledot on Thursday October 29, 2020 @09:31AM (#24930248)
While there are isolated use cases for returning multiple values (quotient/remainder being a good example), all of the real-world cases I've seen where the author tried have been a serious code smell, a missed warning that the encapsulation was weak. Refactoring to use single return values or coherent value objects almost always produced cleaner, more readable, more testable code as a result.
Re:Emerging JavaScript pattern: multiple return values by Kiro on Thursday October 29, 2020 @06:25AM (#24928932)
I don't understand what kind of stuff people build in React where performance implications of this sort is relevant. I'm building games where I loop through potentially thousands of objects 60 times a second and optimizations on that level still don't make any difference. It's always rendering that is the bottleneck. Shouldn't it be the same with React?
Re:Emerging JavaScript pattern: multiple return values by joshribakoff on Thursday October 29, 2020 @07:17AM (#24929274)
No one said to do this for performance
Re:Emerging JavaScript pattern: multiple return values by Kiro on Thursday October 29, 2020 @09:06AM (#24930059)
I was referring to the Performance implications section.
Re:Emerging JavaScript pattern: multiple return values by rasz on Thursday October 29, 2020 @08:08AM (#24929614)
article mentions performance concerns only in the context of Array destructuring
Re:Emerging JavaScript pattern: multiple return values by carterklein13 on Thursday October 29, 2020 @09:57AM (#24930519)
This is one of the main reasons I love using Go for backend work, and the lack of this functionality is just one of the reasons I dislike JavaScript.

Anything to make JS feel less haphazard and more elegant (i.e. using TypeScript) is awesome in my books. Looking forward to hopefully using this pattern in my personal work going forward, and maybe trying to introduce it to my team ta work.

Re:Emerging JavaScript pattern: multiple return values by AstroJetson on Thursday October 29, 2020 @11:37AM (#24931643)
Same reason I'm a Lua fan, multiple return values is a super function. With Lua it's baked into the language.
Re:Emerging JavaScript pattern: multiple return values by cratering on Thursday October 29, 2020 @08:15AM (#24929653)
Re:Emerging JavaScript pattern: multiple return values by forest_dweller on Thursday October 29, 2020 @06:47AM (#24929090)
So instead of returning a object, we stuff two values into an array and this grants us the advantage of saving a few extra characters.

This is pretty hacky and it smells. it also won't work in languages like typescript (without losing the advantage of using it) which is frequently used.

Re:Emerging JavaScript pattern: multiple return values by Drakim on Thursday October 29, 2020 @07:31AM (#24929357)
Did we read the same article?

I do absolutely not see the author advocating returning values in an array instead of an object, in fact, they seems to be saying the exact opposite? (starting with the array example, and then going to the object example afterwards)

The primary point of the article is the destructuring which makes the syntax much nicer.

Re:Emerging JavaScript pattern: multiple return values by aurbano on Thursday October 29, 2020 @07:39AM (#24929413)
While the article does advocate for objects vs arrays for returning multiple values I wanted to address the second part of you comment.

It will work just fine in TypeScript, where you can define a "fixed size" array as a type:

    type Test = [number, string];
    const test: Test = [1, 'something'];
The second line will fail compilation if there's a different number of elements in that array, or if the types don't match.
Re:Emerging JavaScript pattern: multiple return values by siproprio on Thursday October 29, 2020 @08:59PM (#24938001)
Matlab has this!