Require a small number of parameters.

Your method definitions should not be overly long.

A method with a long series of parameters can become a problem for other developers to use. Every time they use the method, they’ll need to research its full definition, determine which parameters are nullable, and what order they need to be in the call. Fortunately, better languages have named arguments, so the order becomes less of a concern. Still, a large quantity of parameters causes headaches.

You can group associated parameters into an object. For example, you can use a Coordinates object instead of (x, y, z). Passing Coordinates instead of three unknown temporary variables is much easier to understand.

Other parameters can be gathered into objects when they are related. For example, a PaymentAccount object could include the account number, subaccount number, group number, company, and account type; this would replace five parameters with one.

Having too many dependencies is a sign your method is doing too much. Try to reduce your method’s responsibilities, and the number of required parameters should also come down.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: