Alex Rivera | Logout

Coding standards: Large amount of arguments

Asked 2009-05-28T23:46:54.493
14

Hey, I'm a fresh out of college graduate. I'm working on a project that I expect will be ultimately maintained by somebody else. I keep encountering an annoying situation on this project, and that is objects that require many private variables and as a result very long constructors.

Apart from variable naming, there isn't any coding standard enforced. I'm wondering how to deal with the likes of this. Sometimes I fear I will see some of my own code on dailywtf in the future!

I tought about trying to enclose some of these arguements in other classes, but in this situation it doesnt really make sense.

Is this a total non-issue or is it something that should and is easily correctable?

public function __construct($uCode = '', $uName = '', $uTime = '', $uArea = '', $uDomain = '', $uText = '', $uId = '', $uNum = '', $uVideo = 0, $uAudio = 0, $uImage = 0){
Edit
Report

1 Answer

16

Generally speaking, if you have more than about 4 arguments, you are better off using a temporary object or array instead. Often many of the parameters because optional and this can get pretty awkward and error prone pretty fast. So:

class MyClass {
  public function __construct($options) { ... }
...
}

$o = new MyClass(array(
  'uCode' => 'some value',
  'uText' => 'another value',
));

Compare that to:

$o = new MyClass('some value', '', '', '', '', 'another value');

Notice how the array version only includes what you want to pass.

answered 2009-05-28T23:49:05.160

Your Answer