Programming practice with a function
I 开发者_如何学Pythonhave a function that returns an integer, however I would like to expand it to add a new param to it. With this param, however, the function would have to return an array.
- Is it bad practice to have a function that returns either an array or an integer based on a param?
- If so, how can I solve this?
I think it would be bad practice also to just copy-paste the entire function for 4-5 extra lines.
If at all possible, I would call one function from within another.
function getSingle($arg)
{
// Do whatever it is your function should do...
return 1;
}
function getMultiple($args)
{
$out = array();
foreach ($args as $arg) {
$out[] = getSingle($arg);
}
return $out;
}
For the function you have in mind, it might not be possible to do this, but it may be a good option.
As an extra note, as the functions are related to one another, I would write them as class methods to "group" them together.
Take Users
for example; I might need a function to get a single user and another to get multiple users. It makes sense to collect these methods in a class:
class Users
{
public function getUser($id){}
public function getUsers(array $id = null){}
}
In PHP, I would say "one function one output type." (with the exception of the value FALSE
which holds special meaning in PHP). If PHP supported overloading that might be different, but it doesn't. But here's a question, why not simply have one function which both of those functions wrap?
function my_wrapped_function($param1, array $param2)
{
return $param1 * count($param2);
}
function get_array_from_wrapped( $param1, array $param2 )
{
return array( $param1, my_wrapped_function($param1, $param2));
}
function get_int_from_wrapped( $param1, array $param2 )
{
return my_wrapped_function($param1, $param2);
}
In my opinion it is bad practice, as it could cause problems working with it, because you won't always know if you are getting an int or an array from your function.
My Suggestion
Always return an array (even if it is one item long), and have a more general looking function, that is easily handle-able.
I am against returning things as an array if your function is modifying more than one value then probably the function that you have written isn't the right one and may be you need to have two functions for the same.. One main reason for this clean and maintainable code and tomorrow a new person need nt go and wonder what the he'll is happening having said that there are cases where you have to modify more than one argument in that case take a call see what the method does and return the right value and the other vale pass it by reference and edit it.
Here is an example of passing by reference..
<?php
function foo(&$var)
{
$var++;
}
$a=5;
foo($a);
// $a is 6 here
?>
You name some principles here:
- Make function parameters have one meaning (and functions ideally zero parameters, avoid more at all cost possible).
- Make a function do one thing only.
- Don't repeat yourself.
I think all those three are valid. Your questions sounds like a trade between 3. and 2. or 1.. I think you should not trade them against each other.
You have not shared much about what you try to achieve with that function, so totally abstracted, I can see the following paths out of it:
- You're facing a design issue. Redesign and refactor your code.
Hmm, sounds a bit like clever-talking, but honestly, you might either not use iterators properly (foreach) or you're starting to create a "one function does it all"-function which you shouldn't. That will duplicate code in the end.
精彩评论