How Not to Refactor a Function That Has Too Many Parameters

Most programmers know that having functions with too many parameters can be confusing. However, fixing such problems requires some intelligence. A programmer once saw some code like this:

SetObjectParams(obj, foo, bar, baz, quux, xyzzy, abra, cadabra, hocus, pocus, presto, shazam);

Finding a stylistic rule somewhere that said a function should have no more than five parameters, the programmer “refactored” it to this:

SetObjectParams1(obj, foo, bar, baz, quux);
SetObjectParams2(obj, xyzzy, abra, cadabra, hocus);
SetObjectParams3(obj, pocus, presto, shazam);

No, that's not how one resolves this problem. You fix this problem by defining functions that each do something simple, give each function a name that describes what it does, and let them take however many parameters make sense.

Comments

Missed Something

Someone smarter than I once said "If your function has 5 arguments... you forgot some."

uh

you made this all up

Did not make it up

The names of the functions and parameters in my article are fictional, but it is otherwise true.