Making great code review. Functions

I will describe set of simple rules how to organise your functions and how to improve quality of your code and code review. Please keep reading!

Code should be consistent

Approach for grouping code into functions should be consistent, code style and naming should be identical across all the code. If this rule is not followed, then something is wrong with the code.

Functions should meet naming recommendations

Name of function should reflect what does the function do, with what arguments and, if possible which result does it return. More about naming you may read here.

Functions should perform one action

This is an interpretation or single responsibility principle. The function should perform only one logical actions. If the function does more than one — it should be decomposed into several functions.

Function should actually do what is written in declaration

It may seem obvious, but I met many situation when function performs different actions, then it's declared.

Parameters grouping recommendations

Parameters that are passed into the function should be also consistent: they should logically to the same group of items. The amount of function as a rule should not be more than 5–6, but exceptions are often met.
If there are many parameters from 1 logical grouped, most probably, they should be groped into one single structure or class and function should take this class as a parameter.

Size of functions

There are a lot of recommendations how big functions should be. I don't think that there is a silver bullet for that. But on the other hand, functions should not be large and/or overcomplicated. Heuristic rule is that function should be visible in one screen or less. Often static code analysers have warnings on functions length, just try to follow these rules.

Visibility level

Functions should have minimal visibility level: if private is enough, then no need to make this function public or protected. Be careful with default level visibility four your language, declare level explicitly if it's needed.

Conclusion

Functions are extremely important units of programming, so we need to pay attention to that. In the story I have described quite simple set of rules and if you start to apply them, quality of your own code and your code reviews will enhance very fast.

iOS developer, TeamLead. Love engineering, guitars, martial arts and learning