Okay, So my first take on this blog "how to write good functions". If I tell you about my experience nobody taught me how to write good functions and classes. All I assume in the beginning of my career was I should write less amount of functions and do as much code as in one function which is totally wrong. At that time, I assume that it's a really good practice to write everything in one function which I assume that my function is very powerful doing all the stuff at once. As soon as I move to build advance project I always stuck in the mid of big functions and writing same amount of code again and again. Those functions made me realise that I am doing bad code. I was so frustrated with this practice.
One day my friend suggested me to read Design Patterns to get rid of this problem. I started reading SOLID principles. All those heavy words went over my head and again it the feeling was like I can never learn this. But SOLID really helps. I will write a separate blog on SOLID principles.
Uncle Bob To Rescue:
So many of you might know Uncle Bob but if not "Robert Cecil Martin" aka Uncle Bob. I was just surfing on YouTube and found a great video of Uncle Bob explaining the difference between good and bad functions. Following few point I took from his talk. So without wasting your time let's discuss how to write good functions. If you keep remember these 5 points when writing functions then you will rock.
- Boring Function
- Make It Like A Story
- Make Them Reusable
- 3 Minute Strategy
- No More Than 2 Arguments
Boring Functions:
If you are reading a function and whatever you read is compiling in your mind and you feel like.
Thats so boring and simple to understand whats going on.
Then the function is well written and that's exactly you have to write your functions. Good functions are always easy to understand. Let me explain in more detail.
On the other side if you are reading a function and you have a confusing eye on the screen, Moving your eyeball from top to bottom of that function thinking that, what the hell is going on here. So it's a sign of bad function.
A function should always do a one task. It should not be doing multiple tasks in its implementation. If you have multiple logics in your functions just separate them into other functions and reuse them. Doing that you will be making your functions easy to read and understand. Make sure one function is only performing one task.
Make it like a story:
Ever remember when you were a kid and reading a story book and after reading each line you smile or you become curious that what will gonna happen next. While you were reading you already know what going on and sometimes you also predict what will gonna happen next.
In the same manner we have to write our class like a story with the help of functions. Name your functions like if anybody reads your function, he shouldn't have to read the implementation. Just by reading the name he understands what is going on inside the function. To make your class beautiful like a story you have to write small functions so the reader (you) can understand just by reading the name of functions with a smiling face.
Make them Reuseable:
Always try to make your functions reusable, If you have written a function of 30 lines. I am 100% sure that there would be some chunks in your function which you can move it in different functions and reuse them in others. Even if you are repeating a single line again and again just put that line in another function, return it and reuse it.
Try not to use global variable in your functions as it results tight coupling in your functions and you can never use them again. Use parameters instead of global variables to make them more dynamic.
3 minutes strategy:
If you are taking more than 3 minutes to understand a function than there is something bad in that function. 3 minutes is a lot of time to understand. The average time to understand a function is 90 seconds (No valid stats). If you are taking more than that. Then its time to separate some chunk from your function and write them in other.
Not more than 2 arguments :
According to uncle bob, Good function are always without arguments. Now in some cases we need parameters to process the output. The fine limit is 2 arguments in a function. But you can add more where it should definitely make sense. Adding 5 6 or 7 arguments in a function is a bad practice.
Tip : Good functions are no longer than 20 lines.
So if you like this article do share it with your friends and let me know your feedback on this. I will be so happy if these 5 points help you to write your good functions. Cheers
Top comments (1)
Nice Article keep it Up