DEV Community

Discussion on: JS Coding Patterns that give you away as a Junior Developer

Collapse
 
paullyfire profile image
Paul Patterson

Something that helped me change the way I viewed if statements is using early returns.

Big plus is reducing indentation and easing the cognitive load of the different forks your functions can take.

Collapse
 
cooljasonmelton profile image
Jason Melton

Yeah totally. Dealing with a lot of forks stinks. Also this might be obvious, but I remember being excited when I realized you could do if statements on one line, like

if (true) console.log('nice');
Enter fullscreen mode Exit fullscreen mode

For a long time, I always used a block--even for

if (true) { 
    return 
};
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
alainvanhout profile image
Alain Van Hout

Then again, a fair amount of seniors (and depending on the language) will go back to the multiline approach, so that every line does exactly one thing (where the check and the action count as separate things).

Thread Thread
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️ • Edited

This can be useful for finding errors and analizing test coverage reports as well as making the code easier to visually parse because it's closer to a list of instructions.