Wilbur Suero - Software Craftsman

Function arguments, how many are too many?

February 18, 2023

Function arguments are the values passed to a function when it is called. In Ruby, functions can take any number of arguments, including none. However, passing too many arguments to a function can make the code difficult to read and maintain. In this blog post, we will explore how many arguments are too many, and show some alternatives to using a large number of arguments.

How many arguments are too many?

The number of arguments a function should take depends on the complexity of the function and the problem it is trying to solve. Generally, if a function takes more than 3-4 arguments, it might be a sign that the function is doing too much and should be refactored. In such cases, it might be better to group related arguments into a single object or to split the function into smaller, more focused functions.

It’s important to note that having a large number of arguments doesn’t necessarily mean that the function is bad. There may be cases where a function needs to take a lot of arguments to perform a complex task. However, it’s important to be mindful of the readability and maintainability of the code.

Alternatives to using many arguments

1. Use a Hash

One alternative to using a large number of arguments is to pass a hash as a single argument to the function. A hash is a collection of key-value pairs, which can be used to group related arguments. This approach can make the function call more readable, as well as allow for more flexibility in the arguments that are passed.

Here’s an example:

def send_email(to:, subject:, body:, attachments: [])
  # Send email
end

send_email(to: 'example@example.com', subject: 'Hello', body: 'World')

In this example, the send_email function takes a hash as its only argument, which contains the to, subject, body, and attachments keys. This approach allows for more flexibility in the arguments that are passed, as the attachments argument is optional.

2. Use a Struct or Class

Another alternative to using a large number of arguments is to create a Struct or Class to encapsulate the arguments. This approach can make the code more readable, as well as provide a clear separation of concerns.

Here’s an example using a Struct:

Email = Struct.new(:to, :subject, :body, :attachments)

def send_email(email)
  # Send email
end

send_email(Email.new('example@example.com', 'Hello', 'World', []))

In this example, we define a new Email struct that contains the to, subject, body, and attachments fields. We then pass an instance of this struct as the argument to the send_email function. This approach provides a clear separation of concerns and allows for more flexibility in the arguments that are passed.

3. Use Defaults

Finally, another approach to reducing the number of arguments is to use defaults. This approach can be useful when certain arguments are optional or have a default value.

Here’s an example:

def send_email(to, subject, body, attachments: [])
  # Send email
end

send_email('example@example.com', 'Hello', 'World')

In this example, the attachments argument has a default value of an empty array. This approach allows the function to be called with fewer arguments and provides a default value for the optional attachments argument.

Using too many arguments in a function can make the code difficult to read and maintain. It can also make the function call hard to understand. However, by using alternatives like a hash, a struct or class, or default values, we can make the code more readable and maintainable.

As Ruby developers, we should strive to write clean and maintainable code that is easy to understand and modify. By being mindful of the number of arguments we use in our functions, we can make our code more readable and easier to maintain in the long run.


Crafted by Wilbur Suero, a Software Engineer, who is passionate about building innovative and impactful solutions that drive business growth and operational excellence.