Friday Finking: Limiting parameters

Christman, Roger Graydon dvl at psu.edu
Sat Jul 11 17:46:24 EDT 2020


I'll preface this by saying I am a programming instructor
who still teaches from the ivory tower, so may not necessarily
reflect actual practice in industry.

But I have a very simple rule of thumb for limiting parameters:
One should be able to summarize what a function does in one or two
sentences,  e.g. computes the volume of a cone, or print address labels.
To fully understand the details of the task would certainly involve
listing the parameters, but I would generally expect that a practical
function would naturally put a cap on how much data is provided.
If you need to include more than 7 parameters to describe what
it is you want to do, you are probably doing more than one thing,
or doing something unnecessarily complicated.

I'll take your example with the address, and assume I want to
print a mailing label.    I could certainly see you needing to print:
first name, middle initial, last name, house number, street name,
apartment number, town, state, country, zip code, (and maybe more),
and listing all of those individually would produce a very long list.

But these values really are not all that independent of each other.
A person's name can be viewed as a single value that happens to
have multiple parts (collect that into a tuple or class).   Similarly,
a street address is a single object with multiple parts.   These can
easily be delivered as complete objects for a short parameter list.

That does not necessarily mean that the function needs to know
the particular representation or form of that data.   Let those be
objects with getter methods for the data you wish, and have the
function document what methods it will attempt to call.   Then
any class that provides the expected methods would be suitable.

That would also greatly simplify some other features for the problem.
For example, "first name" "last name" really does not mean the same
thing as "individual name" "family name", when some countries
list the family name first.   So it might be better to have a name
object include a "full name" method for the print-mailing-address
function to call instead of giving it access to component parts.

TL;DR:  I expect each parameter to a function to be independent
or orthogonal to the others.   Values that are closely related to
each other can probably be encapsulated into a collection or object.
If you still end up with a large number of independent parameters,
I would question the simplicity of your function's intent.

Roger Christman
Pennsylvania State University


More information about the Python-list mailing list