Clean Code: Function Arguments

Posted on

This article is a short summary about Function Arguments from Clean Code book (Robert C. Martin)

How good a function determined by number of arguments:

Strong reasons to have a function with few arguments:

  1. Easier to read
  2. Easier to write test cases. More than two arguments can be daunting.

Let's compare these functions

includeSetupPage()

is easier to read than

includeSetupPage(newPageContent)
includeSetupPage(newPageContent, pageAttributes)
includeSetupPage(newPageContent, pageAttributes, envVars)

Also, in general, should avoid output argument. Let's take a look at this function

copyArrayFromSourceToDestination(source, destination);

Function above use destination as output argument. It could be confusing because when we read a function, we expect the information is going in through arguments and out through the return value.

Here is a better way

const destination = copyArrayFromSource(source);