Writing High Quality Functions

Ever wonder how to write clean functions/methods? While searching for the answer you might have come across some metrics, stating your function should not be more than 25 lines of codes or it should not have more than 5 arguments passed to it etc. When it comes to quality, unfortunately such metric are bad indicator to measure the quality of the function. It will just trigger a flag that there is an issue, but it will not tell what the issue is.

When writing a high quality function, the first and primary rule is functions should be small, it should only do one thing. Sounds like Single Responsibility Principle? (SRP) yes it is, you can apply SRP not only to class, but to functions as well and this is the only measure you can use to check if the function you have written is of high quality. 
FUNCTIONS SHOULD DO ONE THING. THEY SHOULD DO IT WELL. THEY SHOULD DO IT ONLY.                                                                                                                               (Clean code Robert C. Martin)
But how would you know if the function is doing one thing? In order to make sure your function is doing one thing we need to make sure that the statements within our function are all at the same level of abstraction. Take a look at below code snippet.

You can see the “RegisterUser” method as well as the other methods are all doing only one thing, hiding away the details like, the database connections, queries, validation rules, navigation between pages etc. off course there is a chance that “CreateUser” method is performing some task before creating an object which is required to be accepted by database and then delegating the task to save that object into the database to other component altogether. 
Mixing levels of abstraction within a function is always confusing. Take a look at below code snippet

In the above version of the “RegisterUser” method, it is very difficult for the reader to read the method from top to bottom. The reader first has to understand by debugging the code, the steps involved in the “RegisterUser” method. The abstraction is leaked, just imaging a scenario where the consumer of the “RegisterUser” method gets an exceptions saying “The connection's current state is closed”, it’s just confusing.
Also if you see the high quality version of “RegisterUser” method, you will realize that the methods reads like a textbook paragraph. While the other version of the same method requires more efforts to just understand what the code is doing, the author of the code might have to add comments to explain what he is doing there.
Function Arguments:
You should avoid passing more than two or three argument at max to a Function. More than three requires a very special justification- and then shouldn’t be used anyway. Reason? If you are passing more arguments to a function, chances are that your function is doing more than one thing at a time. More than three arguments are a bad idea from testing point of view. Imagine the difficulty writing all the unit test to ensure that all the various combinations of arguments work properly.
As I stated above there are plenty of metrics available online ranging from seven line of codes to twenty five or your function should fit easily on to the monitor. In my personal opinion you can limit your function till 10 -15 lines of code. If it starts to go till 20 it’s a flag that it might be doing multiple things and you should consider refactoring your code by dividing the function in to multiple function. I would urge, you should read and explore this topic especially from the book “Clean Code” by Robert C. Martin. There are plenty of thing which will help you write the high quality functions.


Comments

Post a Comment

Popular posts from this blog

Leaky Abstractions: The initial sign of tightly coupled systems

Threading in .Net – Part I – The Basics.