Home

Don't overdo positional arguments

Heads up: blog post for programmers

You should rarely use more than 1 positional function argument. I'm not the first to say this. However it seems not everyone agrees.

I've been playing with smart contracts this week and encountered this piece of code:

const gameContract = new ethers.Contract(
  CONTRACT_ADDRESS,
  myEpicGame.abi,
  signer
);

This piece of code bothers me because you need to pass in 3 positional arguments.

That means you need to remember the order in which you pass in the arguments when calling this method.

Why not something like:

const gameContract = new ethers.Contract({
	contract: CONTRACT_ADDRESS,
	abi: myEpicGame.abi,
	signer: signer
});

This way you don’t need to remember.

You could find this too verbose. Then do:

const gameContract = new ethers.Contract({
	c: CONTRACT_ADDRESS,
	a: myEpicGame.abi,
	s: signer
});

Here the only thing you need to remember is that:

  • c stands for contract address
  • a stands for Abi file
  • s stands for signer

For me positional arguments should rarely be used.

The main caveat is when the context makes it obvious. For example when the method name tells you the order you need to pass in the arguments:

apply_a_on_b_and_tell_c(a, b, c)

Is better than

apply_a_on_b_and_tell_c({a, b, c})