DEV Community

Discussion on: Clean Code Applied to JavaScript — Part III. Functions

Collapse
 
cscarlson profile image
cScarlson

Of course, I'm only pointing out the negatives and none of the many positives...

You made a big no-no with some of your functions by NOT naming them as VERBS. "newBurger" is NOT a good function name. What? Are you "newing" it??? How about, "createBurger"? NOW that IS a VERB -- much better!

Also, it is a good thing to implement METHOD CHAINING in your CLASSES, but I see chains used TOO MUCH. My rule, if it's a void return then it should instead return this. However, your example above for total chains .map & .reduce. Instead, you should have 2 variables: prices (mapped) & total (reduced). That way, when more functionality around pricing is added, another [Jr] dev doesn't come along and write var prices = items.map( ({ price }) => price ) -- adding another full iteration to the operation. It's also more informative when we're "reducing prices" rather than "reducing [structure]".

Moreover, JavaScript is both Functional AND OOP, and if you're not using both you're just not going to write good code. Why encourage only Functional? Jr devs automatically, naturally gravitate toward writing Functional code, why not encourage more OOP? That would probably benefit a dev much more.