Function Parameters

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

Consider the following function

function foo(flagA: boolean, flagB: boolean) {
  // your awesome function body 
}

With such a funciton definition its quite easy to invoke it wrongly e.g. foo(flagB, flagA) and you would get no help from the compiler.

Instead of convert the function to take an object:

function foo(config: {flagA: boolean, flagB: boolean}) {
  const {flagA, flagB} = config;
  // your awesome function body 
}

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

Note : If your function is simple enough and you don't expect much churn feel free to ignore this advice 🌹.

results matching ""

    No results matching ""