Function arguments (2 or fewer ideally)

Limiting the amount of function arguments

It is very important and useful to limit the amount of function arguments, because it makes testing your function easier. Having more than three leads to a combinatorial explosion where you have to test tons of different cases with each separate argument. Using a object if you can’t avoid a situation requires more than 3 arguments.

Bad:

1
2
3
function createMenu(title, body, buttonText, cancellable) {
// ...
}

Good:

1
2
3
4
5
6
7
8
9
10
function createMenu({ title, body, buttonText, cancellable }) {
// ...
}
createMenu({
title: 'Foo',
body: 'Bar',
buttonText: 'Baz',
cancellable: true
});