TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Emerging JavaScript pattern: multiple return values

29 pointsby loigeover 4 years ago

9 comments

franciscopover 4 years ago
Please some admin (or you, Loige) add the &quot;[2018]&quot; tag since this is well established in 2020. It was &quot;emerging to already there&quot; back in 2018.<p>I also missed generators from this list, which are the &quot;official&quot; (but a lot more complex than the ones showed here!) way of having multiple async returns:<p><a href="https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Reference&#x2F;Operators&#x2F;yield" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Refe...</a>
ubertacoover 4 years ago
Returning an object and returning an array aren&#x27;t exactly new concepts in Javascript? The only relatively-new part is destructuring assignment, but that&#x27;s been around since 2015, since it was added in ES6.
评论 #24929072 未加载
tinkertamperover 4 years ago
I’ve found returning a tuple with my validation functions to be very helpful.<p><pre><code> let [isValid, err] = validate(data) </code></pre> It’s nice for a few reasons:<p>1. I know anything returned `err` is safe to display back to the user<p>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.<p>3. I can destructure just the first half `[isValid]` if that’s all I care about.<p>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.
评论 #24930789 未加载
评论 #24930937 未加载
paledotover 4 years ago
While there are isolated use cases for returning multiple values (quotient&#x2F;remainder being a good example), all of the real-world cases I&#x27;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.
Kiroover 4 years ago
I don&#x27;t understand what kind of stuff people build in React where performance implications of this sort is relevant. I&#x27;m building games where I loop through potentially thousands of objects 60 times a second and optimizations on that level still don&#x27;t make any difference. It&#x27;s always rendering that is the bottleneck. Shouldn&#x27;t it be the same with React?
评论 #24929274 未加载
评论 #24929614 未加载
carterklein13over 4 years ago
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.<p>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.
评论 #24931643 未加载
crateringover 4 years ago
C# also supports this with the Tuple type<p><a href="https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;dotnet&#x2F;csharp&#x2F;language-reference&#x2F;builtin-types&#x2F;value-tuples#tuple-assignment-and-deconstruction" rel="nofollow">https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;dotnet&#x2F;csharp&#x2F;language-refe...</a>
forest_dwellerover 4 years ago
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.<p>This is pretty hacky and it smells. it also won&#x27;t work in languages like typescript (without losing the advantage of using it) which is frequently used.
评论 #24929357 未加载
评论 #24929413 未加载
siproprioover 4 years ago
Matlab has this!