Saturday, April 20, 2019 04:18

Best practices when using methods and functions

There are two main reasons why methods and functions are even used. First one, which I already mentioned in a few of the previous posts, is code re-usability. The second is modularization of the code, splitting of complex tasks into smaller sub-tasks, which can offer us a better overview of the entire functionality.

This is a list of best practices when using methods and functions, as well as a more detailed explanation about why they are useful:

  • each method should perform a task, and a single task. If you have a method for writing a file, it should only write a file. If you have a function performing multiplication, it should only perform multiplications. Try to avoid combining multiple tasks in a single method or function, unless the certain tasks are grouped by same purpose (for instance, a method for saving a file could perform a few tasks that are related to the process of saving the file: check if the save location exists, if the file we are saving overrides an existing one, the actual process of saving the file, an optional message for the user in case of successful operation, perhaps some error handling, etc).
  • a method or function must have a good, logical, short and descriptive name. If a method performs the printing of a document, name it PrintDocument(), not MyMethod(). A function or a method should state its purpose from a simple glance at its name. Names such as Processing(), MyMethod() or DoCalculation() are not descriptive and will only confuse you later on. If you have problems naming your method or function, it is probably an indication of multiple tasks performed in a single method or function. Try to break them even further, into smaller tasks!
  • By convention, methods and functions names should contain a verb, and they should describe an action. Good methods or functions names are, for instance: SortNumbers(), DeleteFile(), SearchProducts(), etc.
  • Also by convention, methods and functions name should use PascalCasing, meaning that the name should start with capital letter for each word and continue with lower case letters, and the words should not be separated by any character. The preferred way of naming methods and functions: SendEmail(). Avoid send_email(), sendEmail(), sendemail(), etc.
  • Methods and functions should have the minimum dependency to the class where they were declared. This means, do not alter data that should not be altered by that method or function, and do not send more data to them than it is required for them to do their specific task.
  • Try to keep your methods and functions short. You will find that having a method or function that spans on more than a screen height is difficult to work with. There are cases when there is no other way, but if you have a choice, try to avoid having too long methods and functions.
  • Do not over-break your program. Some programmers, out of the fear that they are not splitting their code enough, over-break their methods and functions into too smaller units. It is not a good programming principle to have 3 functions for performing a simple addition of two numbers!
  • One last good recommendation when using functions and methods is to provide them with a good error handling mechanism. A method or a function should always throw a descriptive error or exception when it is unable to perform its designated task. Alerting the user that “Could not save file!” is not a very descriptive error and doesn’t offer any clue about the reason why the file cannot be saved.



Tags: , , ,

2 Responses to “Best practices when using methods and functions”

  1. Ikem says:

    Maybe you add a tag „best practice“, and tag the article about „Choosing significant variable names“ too.

Leave a Reply