DEV Community

Cover image for Clean Code Applied to JavaScript — Part III. Functions

Clean Code Applied to JavaScript — Part III. Functions

Carlos Caballero on December 05, 2019

Introduction In this post, we are going to present the fundamental tips and advice to generate clean code focusing on the element that a...
Collapse
 
jakesmithdeveloper profile image
Jake Smith

I really like this series

Collapse
 
carlillo profile image
Carlos Caballero

Thank you very much!

I have already thought up to 6 parts for it!

I do not want to extend too much in the series but I want to see the beauty of clean code.

A greeting!

Collapse
 
jannikwempe profile image
Jannik Wempe

Would really love to see more!

Love it how you keep it precise and illustrate with examples :-)

Collapse
 
paceaux profile image
Paceaux

the one problem with default parameters in a function is that it only works if the parameter is undefined.

If the parameter is null, then the default parameter isn't applied; Default parameters don't strictly behave as a truthy check.

e.g.

function plusSome(num, some = 1) {
 return num + some;
}

plusSome(1, null); // returns 1. Because null passed in, coerced to 0
plusSome('1', null); // returns '1null'

So, even with default parameters, you still have to be careful

Collapse
 
thiagomgd profile image
Thiago Margarida

I agree in part, because generally speaking you shouldn't pass null as param, and functions shouldn't return null

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.

Collapse
 
rolandcsibrei profile image
Roland Csibrei

Thanks for the article, it's really useful.

Collapse
 
carlillo profile image
Carlos Caballero

Thanks!!

This is a post series about my experience develop using JavaScript.

Collapse
 
remibruguier profile image
Rémi BRUGUIER

Wow ok. This is indeed a great series, I will also share it with my team. How the hell did I miss the ES6 default arguments thingy...

Collapse
 
thecodingalpaca profile image
Carlos Trapet

Pretty impressed with the quality of this post.
With your permission, I'm sharing it with my team, tocayo :)

Collapse
 
carlillo profile image
Carlos Caballero

It is a great honor for me to share my content :-).

Hopefully we can devirtualize in some event, some day!

Cheers!

Collapse
 
ialexreis profile image
Alexandre Reis

Congratulations Carlos!

Really good article that resumes the core points of the Clean Code book!

The tips you describe here are pretty big deal for newcomers and even for the senior developers!

Collapse
 
carlillo profile image
Carlos Caballero

Thanks! 💪💪

Collapse
 
monfernape profile image
Usman Khalil • Edited

A simple thank you can't express my gratitude. Bring more

Collapse
 
filipealc profile image
Filipe de Alcântara

How can I even begin to thank you, man, speechless on the quality of your post. My gratitude and I would like to share with my team the knowledge learned here.

Collapse
 
carlillo profile image
Carlos Caballero

Thanks! 🔥🔥

Collapse
 
havespacesuit profile image
Eric Sundquist • Edited

"Without wanting to enter into debate between programming paradigms"
Boldly enters into debate between programming paradigms...