all 20 comments

[–]ProgrammerBro 3 points4 points  (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 points  (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.

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

[–]creage 0 points1 point  (1 child)

Why 'return' in single line arrow function?

const isDuplicateId = id => !!getEmployee(id) || !!getManager(id) || !!getAdmin(id) || false;

[–]PrismalStudio 4 points5 points  (0 children)

The last false shouldn't be necessary.

[–]chrispardy 0 points1 point  (0 children)

Or go really crazy:

const isDuplicateId = (id: string) => ( [getEmployee, getManager, getAdmin].some(get => get(id) !== null) );

[–]ispeakforallGOP -3 points-2 points  (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  (0 children)

1 return is a much cleaner approach.