Function Parameters

If you have a function that takes too many parameters, or parameters of the same type, then you might want to consider changing the function to take an object instead.

Consider the following function:

  1. function foo(flagA: boolean, flagB: boolean) {
  2. // your awesome function body
  3. }

With such a function definition it’s quite easy to invoke it incorrectly e.g. foo(flagB, flagA) and you would get no help from the compiler.

Instead, convert the function to take an object:

  1. function foo(config: {flagA: boolean, flagB: boolean}) {
  2. const {flagA, flagB} = config;
  3. // your awesome function body
  4. }

Now the function calls will look like foo({flagA, flagB}) which makes it much easier to spot mistakes and code review.

Note : If your function is simple enough, and you don’t expect much churn, then feel free to ignore this advice ?.