I user-define many functions and a few of them have six, ten, or even more arguments. Reading my code gets difficult when I forget what the arguments of a function are, or what order they go in. I've devised a way to deal with this, replacing all the arguments with a single array, and using array keys as labels for each argument. So, for example, instead of
function MyFunction(string $sSayThis, int $nRepeatTimes, bool $bLoud = $false) {...}
I now have
function MyFunction(array $args)
{$sSayThis = $args['sSayThis']); CheckType($sSayThis, 'string');
$nRepeatTimes = $args['nRepeatTimes']); CheckType($nRepeatTimes, 'int');
$bLoud = (IsSet($args['bLoud']) ? $args['bLoud'] : false); CheckType($bLoud, 'bool');
...
}
A call to this function, instead of
MyFunction('Hello', 3, true);
now looks like
MyFunction(array('sSayThis' => 'Hello', 'nRepeatTimes' => 3, 'bLoud' => true));
This is hardly necessary when there are only three arguments, as in this example, but it could be very helpful when reading code for a function with six or ten arguments! Also, if I only need to pass a value for the tenth argument and use the default values for all the optional arguments before that, I can just omit those other arguments from the call instead of passing a series of , ''
for them.
This is a hack, and it seems kind-of ugly. But it does help make my code self-documenting and easier to read.
I know there are IDEs that would give me argument hints, but I'm using Notepad++, which doesn't do that.
This idea is discussed in a similar question asked last year, PHP Function Arguments - Use an array or not?, but that question doesn't show what the function calls look like, which is the most important part of the question. Some people in answer to that question said that a function should never need ten arguments and having that many indicates poor design. I understand that concern, but sometimes an algorithm just needs a lot of information.
Is there anything wrong with this approach, or is there a better way to self-document these function calls?