Functional programming practice

I have a function that returns an integer , however I would like to expand it to add a new parameter to it. However, with this parameter, the function would have to return an array .

  • Does bad practice have a function that returns either an array or an integer based on a parameter?
  • If so, how can I solve this?

I think it would be bad practice to just copy the entire function for 4-5 extra lines as well.

+8
function php class methodology
source share
5 answers

If at all possible, I would call one function from the inside.

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; } 

It may not be possible for the function you mean, but it may be a good option.


As an additional note, since functions are related to each other, I will write them as class methods to “group” them together.

Take Users , for example; I may need a function to get one user and another for multiple users. It makes sense to collect these methods in a class:

 class Users { public function getUser($id){} public function getUsers(array $id = null){} } 
+6
source share

In my opinion, this is bad practice, as it can cause problems with it, because you will not always know if you get an int or an array from your function.

My suggestion

Always return an array (even if it is one element long) and have a more general function that is easy to process.

+2
source share

In PHP, I would say that "one function has one type of output." (except for the FALSE value, which is of particular importance in PHP). If PHP supports overloading, this may be different, but it is not. But the question is, why not just use one function that both of these 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); } 
+2
source share

I am opposed to returning things as an array if your function modifies more than one value, and then probably the function you wrote is not correct, and maybe you need to have two functions for the same. the reason for this clean and maintainable code, and tomorrow a new person should leave and ask what he will do, saying that there are times when you need to change more than one argument in this case, call, see what this method does and return the correct value, and another vale pass it a link and edit it.

Here is an example of going through the link.

 <?php function foo(&$var) {    $var++; } $a=5; foo($a); // $a is 6 here ?> 
0
source share

Here you name some principles:

  • Making functional parameters makes one sense (and ideally fulfills zero parameters, while avoiding all possible costs).
  • Make a function do only one thing.
  • Do not repeat yourself.

I think all three of these are valid. Your questions sound like trading between 3. and 2. or 1 .. I think you should not trade them against each other.

You didn’t talk very much about what you are trying to achieve with this function, therefore you are completely abstracted, I see the following ways:

  • You are faced with a design problem. Redesign and refactoring your code.

Hmm, that sounds a bit like a smart conversation, but to be honest, you can either not use iterators properly (foreach), or you start creating “one function does everything” - a function that you should not. This will lead to code duplication at the end.

0
source share

All Articles