DEV Community

loading...

Discussion on: React Clean Code - Simple ways to write better and cleaner code

clivend profile image
clivend

agree, but I think it is not worth to have think every time how and why you use one pattern or the other. I prefer to reduce the amount of things I need to keep in mind (given they are already countless) and stick with one pattern that works for all

Thread Thread
drarig29 profile image
Corentin Girard • Edited

Yeah but all the gotchas the article is speaking about are misuses of the operator &&. If you really know the operator and just don't throw it everywhere without thinking one second (which you should always do, like in any language, especially in JavaScript), then you'll never have any problem.

Plus this shortcut is really readable. Most of the time, you don't ask yourself if it works.

For the length thing, I think you should basically never use the shortcut where you simply test its truthiness to know if the array is empty or not.

And for the error about returning undefined with { error } as a function argument, you just have to know that object destructuring can return undefined.

Anyway, do what you want, but I always use it without any issue.

Thread Thread
adamashored profile image
adam-ashored

If you really know the operator and just don't throw it everywhere without thinking one second

React's entire development philosophy (at least how it appears to me) is to craft an API that prevents developers from shooting themselves in the foot. Sidebar: Google could learn a thing or two from them.

Best practices are best practices for all levels of developers, not just the experts.