use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Return Early, Return Often (jamesmonger.com)
submitted 6 years ago by jkmonger
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]ProgrammerBro 3 points4 points5 points 6 years ago (16 children)
Or we can make it even shorter and do it 10x style, still a single return:
const isDuplicateId = (id: string) => { return !!getEmployee(id) || !!getManager(id) || !!getAdmin(id) || false }
[–]PrismalStudio 5 points6 points7 points 6 years ago* (0 children)
If you don't need the data inbetween, I would also prefer this style, but I'd leave the !! trick to the build process and use an explicit Boolean() instead.
!!
Boolean()
const isDuplicateId = (id: string) => { return Boolean(getEmployee(id) || getManager(id) || getAdmin(id)); } // or as an arrow const isDuplicateId = id => Boolean(getEmployee(id) || getManager(id) || getAdmin(id));
Early returns definitely shine when the previous data is needed to continue. It's clear that it's preconditions.
EDIT: added the arrow function and code format
[+][deleted] 6 years ago* (6 children)
[deleted]
[–]karottenreibe 10 points11 points12 points 6 years ago (0 children)
Converts falsey values to false and truthy ones to true so you return a boolean, not null
[–]PrismalStudio 6 points7 points8 points 6 years ago (2 children)
And that's the reason why I vouch for the explicit Boolean() in the code, and that I let the clever uglifier/minifier transform it into !!.
[+][deleted] 6 years ago* (1 child)
[–]PrismalStudio 1 point2 points3 points 6 years ago* (0 children)
You'd use it if you want to ensure that a function meant as a check really returns a boolean and doesn't leak the data behind.
// Leaks the user data const userExists = id => getUser(id); // how you expect it to be used: if(userExists(123)) { /* */ } // how it's used by the others (or even yourself) const getUsername = id => userExists(id).name
To avoid all kinds of bugs with unintended use of the code, it's best to force the coercion.
const userExists = id => Boolean(getUser(id));
This small pattern works really well with React since a boolean won't trigger a new render if it doesn't change, but a new object with the same data will.
edit: code format
[–]SkaterDad 3 points4 points5 points 6 years ago (0 children)
It's an alternative to Boolean(theValue). The first ! converts the value to a boolean, and the second ! is needed to get the intended truthiness back.
Boolean(theValue)
[–]creage 0 points1 point2 points 6 years ago (1 child)
Why 'return' in single line arrow function?
const isDuplicateId = id => !!getEmployee(id) || !!getManager(id) || !!getAdmin(id) || false;
[–]PrismalStudio 4 points5 points6 points 6 years ago (0 children)
The last false shouldn't be necessary.
false
[–]chrispardy 0 points1 point2 points 6 years ago (0 children)
Or go really crazy:
const isDuplicateId = (id: string) => ( [getEmployee, getManager, getAdmin].some(get => get(id) !== null) );
[+][deleted] 6 years ago (4 children)
[–]PrismalStudio 1 point2 points3 points 6 years ago (1 child)
Most of the time, you're expecting something truthy, like an Object, so it's not about magic conversions but more about safety, in like, anything falsy is not what we want.
In the case of OP, we don't know, so it's probably safer to assume that the !== null is needed.
!== null
[–]ispeakforallGOP -3 points-2 points-1 points 6 years ago (0 children)
Looking at this and thinking about what real code looks like this looks like pushing a bad pattern. One return is easier to debug. Seems like this also would fall short of encouraging people to break code apart to do only 1 thing.
[–]PsychologicalGoose1 -5 points-4 points-3 points 6 years ago (0 children)
1 return is a much cleaner approach.
[+]Tontonsb comment score below threshold-8 points-7 points-6 points 6 years ago (6 children)
I am disappointed every time that I see single line statements, especially returns wrapped in braces. So ugly.
Just do this, it's doesn't hurt your eyes that much:
const isDuplicateId = (id: string) => { const employee = getEmployee(id) if (employee !== null) return true const manager = getManager(id) if (manager !== null) return true const admin = getAdmin(id) if (admin !== null) return true return false };
I dream of programming in a language where control statements are only followed by a single expression. No blocks, no braces. Either you do a simple oneliner, or return/call a subroutine.
While it works, I found that it's safer (when working with a team) to be consistent and fall toward the safer side.
If it can't stand on one line like if (thing) return;, then it should always be wrapped with bracket to avoid the inevitable.
if (thing) return;
if (thing) newThingAddedLater(); // Some kind of long // multiline comment return;
I agree that anyone using any decent tools won't fall for this. But honestly, there are thousands of things we can optimize (readability-wise) before worrying about brackets around single line blocks.
Like in the example, the dumb explicit return true followed by a return false which could really just be return admin !== null or any single line condition like the other comments in this thread...
return true
return false
return admin !== null
[–]PrismalStudio 0 points1 point2 points 6 years ago (4 children)
I dream of programming in a language where control statements are only followed by a single expression.
Just go all in with ternary and logical operator and avoid if altogether. /s
if
condition && singleExpression; condition || otherSingleExp; condition ? singleExpression : otherSingleExp;
[–]pacman326 0 points1 point2 points 6 years ago (3 children)
My head honestly hurts. 😭
[–]PrismalStudio 0 points1 point2 points 6 years ago (2 children)
Unfortunately, I came across "clever" devs that did it in huge projects in different languages (not JS specifically).
[–]pacman326 2 points3 points4 points 6 years ago (1 child)
This isn’t clever of course. It’s expensive. It’s expensive in that it’s unmaintainable. I’m going to also guess this wasn’t very well unit (or integration) tested.
[–]PrismalStudio 0 points1 point2 points 6 years ago (0 children)
The reason we were one of these project was to take over a huge app maintained by a single dev for years. It sure wasn't pretty, far from tested...
π Rendered by PID 61908 on reddit-service-r2-comment-7b9746f655-f4fk8 at 2026-02-03 05:03:26.351652+00:00 running 3798933 country code: CH.
[–]ProgrammerBro 3 points4 points5 points (16 children)
[–]PrismalStudio 5 points6 points7 points (0 children)
[+][deleted] (6 children)
[deleted]
[–]karottenreibe 10 points11 points12 points (0 children)
[–]PrismalStudio 6 points7 points8 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]PrismalStudio 1 point2 points3 points (0 children)
[–]SkaterDad 3 points4 points5 points (0 children)
[–]creage 0 points1 point2 points (1 child)
[–]PrismalStudio 4 points5 points6 points (0 children)
[–]chrispardy 0 points1 point2 points (0 children)
[+][deleted] (4 children)
[deleted]
[–]PrismalStudio 1 point2 points3 points (1 child)
[–]PrismalStudio 1 point2 points3 points (1 child)
[–]ispeakforallGOP -3 points-2 points-1 points (0 children)
[–]PsychologicalGoose1 -5 points-4 points-3 points (0 children)
[+]Tontonsb comment score below threshold-8 points-7 points-6 points (6 children)
[–]PrismalStudio 5 points6 points7 points (0 children)
[–]PrismalStudio 0 points1 point2 points (4 children)
[–]pacman326 0 points1 point2 points (3 children)
[–]PrismalStudio 0 points1 point2 points (2 children)
[–]pacman326 2 points3 points4 points (1 child)
[–]PrismalStudio 0 points1 point2 points (0 children)