A very important thing you should have in mind when writing functions, is how many arguments each functions should have. Just like shorter functions are easier to understand, functions with few arguments are easier to understand and use.
How many arguments should a function have?
The short answer for this question is that a function should have as few arguments as possible. You should aim for functions with no arguments, when possible. If not, then pass in one or two arguments. According to the Clean Code book, three arguments is getting to the limit, and you should “never” use more than that. While this might seem like a rule without any justification, it can help your code in the following ways:
It makes your functions easier to read
If a function has only a few arguments (or none at all), then it will be much easier to understand how those arguments are used by the function. If your functions are also small, then it will be that much easier to quickly read the code.
It helps make testing easier
When writing your unit tests, the less arguments a function has, the less argument combinations you have to write tests for.
What does it mean if my functions have more than three arguments?
In my personal experience, if your functions seem to have too many arguments, it can be because:
- your functions are doing more than one thing, you could probably split the function into smaller ones with fewer arguments
- Your function should really be a separate class.
- You are passing values separately when you should be using a data structure
Some examples
Below we can see some examples of valid cases where you can use one, two, or even three arguments.
One Argument
Usually a single argument is used when you want the function to check something about the argument, or you want the function to do something to the argument.
bool FileExists(string fileName); StreamReader OpenFile(string fileName);
Boolean (flag) arguments
One thing you should try to avoid is passing flag arguments. This is a very clear indicator that you have a function that does two things. Instead of a function like
void DoThisOrThat(bool flag);
You should try to write
void DoThis(); void DoThat();
Two Arguments
When writing functions with two arguments you should try to make sure that the relationship between the arguments is clear and that the argument follow a logical order
void MakeReservation(string customerName, datetime reservationDate); void UpdateUserPassword(int userId,string newPassword);
Three Arguments
While functions with three arguments should try to be avoided, in some cases they are justified. Just be careful with the argument naming and ordering.
void TransferFunds(int originAccount, int destinationAccount, decimal amount);
In the previous example, switching the origin account and the destination account by mistake could get you in a lot of trouble!
Argument Objects
If your functions require more arguments, then it’s very likely that what you really need is an argument object. If we examine the TransferFunds function from the previous section, the arguments clearly are related and could be abstracted into an object. The function could be rewritten to be cleaner.
void TransferFunds(Transfer data);
We used a new Class that has the account info and transfer amount as fields.
Argument Lists
Another way to pass multiple similar arguments is to store them in a list or array. This is necessary when you don’t know how many arguments there are.