October 07, 2014

Some Standards For Implementing Functions For Developers

If you have experience with regard to managing a software development team, you definitely know about maintaining and debugging a live application, while a few team members who were working on the project have left the company.

I highly recommend you to have some really clean and practical Standards Coding for your organization and ask team members to follow them. After a while, the result of this strategy will be amazing for you.

Following you can read about some standards of implementing functions:

1- Function should be single purpose

Your Functions should do just one job. If inside a Function you want to do another related job, try to split it in another separate Function.

2- Function should has meaningful name

Try to put understandable and meaningful name for functions.
Other developers should recognize the purpose of functions by their names.
Do not afraid to select long name, it just should be meaningful.
Define proper convention inside your team based on your technology and programming language.

3- Summary for function

Put a comprehensive and meaningful summary for function's purpose at the top.
Describe all Function's arguments and return value in detail.
Put summary about exceptions that a function can throw.

 Microsoft Sample:
        // Summary:
        //     Reads a 32-bit signed integer from unmanaged memory.
        // Parameters:
        //   ptr:
        //     The address in unmanaged memory from which to read.
        // Returns:
        //     The 32-bit signed integer read from unmanaged memory.
        // Exceptions:
        //   System.AccessViolationException:
        //     ptr is not a recognized format.-or-ptr is null.-or-ptr is invalid.
        public static int ReadInt32(IntPtr ptr);

4- Function’s body should focus on main purpose

The body of your function should not do any other things that not related to the main purpose.

Example: In retrieving Functions – which some data are returning back – do not save files or other data. Let’s do it by another Function and handle by main caller.

5- Keep function's body short

Long functions are hard and time consuming for tracking, debugging and maintaining. 
Keep it short as far as possible.

Regularly a whole function’s body should be visible in one page without scrolling.(normally 17inches monitor)

Your Function is really long? It means you may break it down to different Functions.

6- Arguments of function should be arranged

Put all the Boolean’s arguments at the end. 

All the Nullable arguments should be located at the end, after Booleans. 

Name_of_Function(object customer, object product, bool isGoldenUser, bool isValidProduct, object discount=null) 

7- Validations of function

At the beginning of Function’s body, check all arguments validation.
Do not check argument validation inside the body of Functions.
Do not forget to validate return value.

8- Variables inside function

The name of variables inside functions should be comprehensive and meaningful (like Functions’ name).

Wrong Sample:  var1, str, temp, flag;
Correct sample:  currentCustomer,   selectedProduct_byCustomer_to_Purchase,   predefined_Count_array = null;

Assign a default value for variables.
Avoid to define all needed variables at the beginning of functions.
Define variables in right place and stick it to the related code section.

Wrong Sample :
function name_of_function()
Var item1= new object();
Var item2 = new object();
  // function body

Correct Sample :
function name_of_function()
Var item1= new object();
//The block of code that working with item1
Var item2 = new object();
//The block of code that working with item1

9- Function's Dependencies

Do not change global variables inside Functions.
If you need another object inside the business logic, do not fetch it inside functions directly. 
Inject dependent object as an argument and use it.(Dependency Injection)