8

I have 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.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
luqita
  • 4,008
  • 13
  • 60
  • 93

5 Answers5

6

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){}
}
adlawson
  • 6,303
  • 1
  • 35
  • 46
2

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.

Community
  • 1
  • 1
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • Those functions are doing it for error-handling, not for the typical case. And it's burdensome, so can't follow the argument that it's adviseable. – hakre Aug 22 '11 at 18:18
  • @hakre true, but I'm 100% sure I saw a function that returns either an int or an array at least once, just can't put my finger on it. – Madara's Ghost Aug 22 '11 at 18:21
  • Only those exist does not mean it's recommended. I know such function do exist, but the OP was looking for some guide and I just want to noted that it might be not be adviseable. – hakre Aug 22 '11 at 18:26
  • @hakre noted, I'll change my answer (now that I think of it, unmaintainable function is kinda bad practice). – Madara's Ghost Aug 22 '11 at 18:29
2

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);
 }
cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
0

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
?>
Baz1nga
  • 15,485
  • 3
  • 35
  • 61
0

You name some principles here:

  1. Make function parameters have one meaning (and functions ideally zero parameters, avoid more at all cost possible).
  2. Make a function do one thing only.
  3. 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:

  1. 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.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • What do you mean by "and function ideally zero parameters"? – Marvo Aug 24 '11 at 18:57
  • @Marvo: Just that functions should have as less parameters as possible, ideally none - which is not always possible, sure. So "ideally". – hakre Aug 25 '11 at 07:36
  • While I've heard the rule to keep the number of parameters low or under a certain limit to produce maintainable code, I've never heard that the ideal number is zero parameters. Functions, almost by definition, take some number of parameters. – Marvo Aug 25 '11 at 18:44
  • Well that's exactly the point. Ideally a function has zero parameters, if you look into classes, this is more often the case. In procedural programming, this can be the case as well but it's much harder to achieve to have zero parameters and really, it's only the ideal case to have zero parameters, practically we need to have as many parameters as we need to have. Just saying, keep the numbers low for clean code. – hakre Aug 25 '11 at 19:11