DEV Community

Cover image for Coding Best Practices, Chapter One: Functions.

Coding Best Practices, Chapter One: Functions.

Levi Velázquez on May 22, 2019

My purpose IS NOT to say what is the best way to write code, some programmers have their ways and they are just fine. I just want to share with oth...
Collapse
 
harshilpatel007 profile image
Harshil Patel

SOLID principles is great for writing testing friendly code..!!
Great post.
following_for_more_post()

Collapse
 
levivm profile image
Levi Velázquez

Glad you liked it.

I will post the next chapter soon.

Collapse
 
jprochazk profile image
Jan Procházka

Personally, I would put throw statements at the start of a function to avoid large indentation. It really improves readability.

Instead of

func doThing(x: int) {
    if(x <= 10) { 
        print(x)
    } else {
        throw Exception("x is too large!")
    }
}

it would be

func doThing(x: int) {
    if(x > 10) { 
        throw Exception("x is too large!")
    }

    print(x)
}
Collapse
 
peterwitham profile image
Peter Witham

If there is one rule that gets broken more than most it has to be the 'do one thing', been there and guilty of that. Then when you suffer at the hands of you're own code, you suddenly realize why it's a thing.

Collapse
 
cescquintero profile image
Francisco Quintero 🇨🇴

Great post. Functions should always be so small they can be easily tested and read!

Testing code becomes really easy when functions(even classes) just do one thing.

Reading code becomes extremely easy to read when functions just do one thing.

Another way to answer the question

how we can know when our function is not doing just one thing?

Is by telling what the function does. As soon as we describe and use the word "AND" that function is doing more than it should.

"my function sends info to third-party and updates the record" red flag.

Collapse
 
douglasbarbosadelima profile image
Douglas Barbosa de Lima

Good job! For me, the code legibility is the principal skill to great developers.

Collapse
 
levivm profile image
Levi Velázquez

Glad you liked it. Yep, that is the most valuable skill for sure.

Collapse
 
earroyoron profile image
Ernesto λrroyo • Edited

Great post: the code is an abstraction for people; we write code for people not for computers..

Collapse
 
kevinhch profile image
Kevin

Tabs or spaces? :)

Collapse
 
levivm profile image
Levi Velázquez • Edited

Sometimes depends on the language being used, sometimes depends on the person. I can't say that there is a Real/Definitive answer but my preference is spaces.

I always map my tab key to spaces. Why? I use python mostly and tabs may vary depending on the system, this could lead to an enormous amount of issues, a space is just one column. Of course, you need to learn how to navigate through the code independently of the selected method to avoid using right/left arrows.

If you use Javascript, this guide made by Airbnb suggest two spaces instead of hard tabs. I consider this a good style guide for ES6 (javascript).

Python == Spaces and so on.

If you share code with other developers, you must agree on a standard no matter your choice.

If you want to use tabs and you are comfortable with it, it's just ok. But, I'm an astronaut(space person ;) )

Collapse
 
ibrahimisad8 profile image
Ibrahim Isa

Does it matter :)

Collapse
 
omarjalil profile image
Jalil

Tabs of course

Collapse
 
jijii03 profile image
lincey.J

I always forgot about the "one thing" in every projects thanks for those tips!

Collapse
 
jasterix profile image
Jasterix

What's a best practice for ensuring that your code is easy to follow? I've seen Gists with so many helper functions and callbacks that it's hard to follow the thread of execution

Collapse
 
levivm profile image
Levi Velázquez

I'll create a post explaining the best way to do that.

But, functions definitions should be written in top-down schema based on his level of abstraction and importance.

Collapse
 
jasterix profile image
Jasterix

Looking forward to it

Collapse
 
codenutt profile image
Jared

Excellent article! I read about functional programming not too long ago, and have dedicated my code to try this "do one thing" approach and its made my life so much easier! Good work.

Collapse
 
imbolc profile image
Imbolc • Edited

Do you separate scope of function definition somehow, e.g. defining sub-functions in the main function closure or moving them into separate module?

Pythons catch is except btw :)

Collapse
 
levivm profile image
Levi Velázquez

this depends on what framework, structure, etc, you are using. If I need to implement a concept with multiples features and it is quite complex, I create a module for him.

Collapse
 
anwar_nairi profile image
Anwar

Very good advices, I have seen implementing this guideline helped me solve like most of my mutations in my code, so really useful and worth the time spent!

Collapse
 
levivm profile image
Levi Velázquez

Glad to hear that.

Collapse
 
lonewolf9277 profile image
lonewolf9277

Great post but by readiness you meant readability?

Collapse
 
kylefilegriffin profile image
Kyle Griffin

Being verbose is hard when you know that doing so is not the fastest way to code. Knowing in advance that you'll take twice as long is a really good way to never get started at all.

Collapse
 
levivm profile image
Levi Velázquez

Hi, thx for your comment.

I disagree with you, this only should applies in programming contests where you have 3 hours to solve puzzles.

In my case and experience, beign verbose never going to take twice the time. In fact, you’ll need to review the code again, so, do you prefer spending time trying to understand your code because you used variables as: i, j, k, a, ? And if you are working with a team, imagine how painful could be. Of course this is not just easy, it requires practice but this is part of having good practices. Who doesn’t want to improve ?

Collapse
 
kimsean profile image
thedevkim

Thank you for this principles!