Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does this function have too many parameters?

In the end, I got this function. I don't know whether it's normal or not.

function user_registration($user_name, $user_email, $user_pass, $address, 
                           $city, $postalcode, $country, $phone, $mobilephone)

How and why can I improve this?

like image 343
justjoe Avatar asked Apr 05 '10 13:04

justjoe


People also ask

What is too many parameters?

Too Many Parameters is a code smell that is indicated by a method or function that takes in a very large list of parameters. Imagine a function that takes in twenty parameters.

How many parameters should a function have?

The main function can be defined with no parameters or with two parameters (for passing command-line arguments to a program when it begins executing). The two parameters are referred to here as argc and argv, though any names can be used because they are local to the function in which they are declared.

What to do if a function has too many arguments?

There are two techniques that can be used to reduce a functions' arguments. One of them is to refactor the function, making it smaller, consequently, reducing the arguments' number.

How many parameters can of function requires?

Normally, You can pass 125 arguments/parameters in C and 255 or 256 in C++ but you should remember that functions are used to increase the execution speed of program and they provide better readability so the function should be passed only the necessary arguments.


2 Answers

You could either pass an array with all variables packed nicely together, or just make a "User" class and add all properties via setters and do the validation in the end with a dedicated method:

class User {

  public function setName($name) {
    $this->name = $name;
  }

  [...]

  public function register() {

    //Validate input
    if (empty($this->name))
      $this->errors[] = "ERROR, Username must not be emtpy";

    //Add the user to the database
    //Your SQL query
    return empty($this->errors);
  }

}

$user = new User();
$user->setName("Peter");
$success = $user->register();

if (!$success)
  echo "ERRORS OCCURED: ".print_r($user->errors, true);
like image 104
FlorianH Avatar answered Sep 18 '22 09:09

FlorianH


A solution would be to only have one parameter, that can contain several pieces of data -- like an array.

Your function could be defined this way :

function user_registration(array $data) {
    // work with $data['name']
    // and $data['email']
    // ...
}

And you'd call it like this :

user_registration(array(
    'name' => 'blah',
    'email' => '[email protected]', 
    'pass' => '123456',
    // and so on
));


Nice things are :

  • You can add / remove "parameters" easily
  • The "parameters" can be passed in any order you want

Not so bad things are :

  • No hint while typing in your IDE
  • No documentation (like phpDoc)
like image 34
Pascal MARTIN Avatar answered Sep 21 '22 09:09

Pascal MARTIN