Skip to content

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... [Read Full]
markdown guide

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


Glad you liked it.

I will post the next chapter soon.


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) { 
    } else {
        throw Exception("x is too large!")

it would be

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


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.


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


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


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.


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 ;) )


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


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.


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 :)


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.


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!


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


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


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.


Great post but by readiness you meant readability?


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.


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 ?

code of conduct - report abuse