Journal entry

Limit arguments

No one likes a lot of arguments and I don’t just mean the verbal kind. When you write a function like the one below it’s clear there are too many arguments being passed.

function fight( kicks, punches, meanWords, sharpSticks, angryFaces, danceMoves ) {
// insert actions for the battle
}

Too many arguments like this make the function difficult to read and it’s a sign of one of two things; the function is doing too much and needs to be broken into two or more functions. Alternatively, you may need to create a new object and use it to pass the data like this:

function fight(movesAndWeapons) {
  // create user here
}

As a general rule of thumb, attempt to keep the number of arguments to 3 or less and if you go over that re-think your approach.