This is a general answer - it applies to pretty much every language.
There is ONE exception (that springs to mind) - and that is any function that is part of an API should always take an array as this saves on work if you want to call a function from within your application or via a $_POST[]
- you can use the same method with a couple of quick checks to see if it is a post via http
or a variable passed in by the application itself (however very few people seem to be at the stage of consuming their own APIs yet :-P)
Specific to Models and CI
Always have named variables if you can - it will reduce the likelihood of errors in your application.
Unless your application is massive / really data intensive (or has the potential to get that way in a matter of days) it is better to do multiple inserts one at a time through the model for debugging and shouldn't have any significant effect on speed.
Additionally this generally makes the application more secure as you can define the variable types more easily (only numeric for example) this way and handle the error at controller
level rather than model
level
General Answer
It is very much dependant on the function - but naming variables should ALWAYS be the preference.
I will try and explain when to use each:-
1. Loads of optional parameters = array();
If it is a function with loads of optional parameters then it is best to pass in an array - but I would say less than 20% of functions require this much flexibility. -> this saves on having to write
private function myFunction($a, $b = "", $c = "", $d = "", $e = "", $f = "", $g = "", $h = "");
$a = $this->myFunction("aa","","","","","","","bb");
if you want to use just 2 parameters instead of
private function myFunction($arr = array());
$options = array("a"=>"aa","h"=>"bb");
$a = $this->myFunction($options);
Additionally if a function has that many parameters - it probably needs a re-write!
2. Few optional parameters = named variables
However - if it is a function that always requires 3 variables for example, you should hard code them in as that makes catching errors a whole lot easier (also with an IDE you should get code-hinting on your function if you comment it correctly.)
The above example with an array means that when you have a function that MUST have at least 3 variables passed your IDE will have no idea that
$options = array("a"=>"aa","h"=>"bb");
$a = myFunction($options);
Should throw an error.
Additionally you have to write a lot of unnecessary checks to see that required variables are present and set.
3. The Golden Rule and Middle Ground
The BEST way to write functions - in nearly any language is:
- use named variables for REQUIRED fields.
- If you have optional values that always carry on from each other (so your first optional variable is necessary to need your second optional variable) then use
$a = "", $b = ""
- For any other variables - or for functions that may take only one of 4/5 variables pass in an array.
So your ideal scenario for a complex function would be
private function _complex_function($required1,
$required2,
$optionThatWillAlwaysBeRequiredBeforeOtherOptions = "",
$mixAndMatchOptions = array())
{
}
Hope that is clear.