Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

avoiding the tedium of optional parameters

If I have a constructor with say 2 required parameters and 4 optional parameters, how can I avoid writing 16 constructors or even the 10 or so constructors I'd have to write if I used default parameters (which I don't like because it's poor self-documentation)? Are there any idioms or methods using templates I can use to make it less tedious? (And easier to maintain?)

like image 760
Kyle Avatar asked Apr 23 '10 18:04

Kyle


3 Answers

You might be interested in the Named Parameter Idiom.

To summarize, create a class that holds the values you want to pass to your constructor(s). Add a method to set each of those values, and have each method do a return *this; at the end. Have a constructor in your class that takes a const reference to this new class. This can be used like so:

class Person;

class PersonOptions
{
  friend class Person;
  string name_;
  int age_;
  char gender_;

public:
   PersonOptions() :
     age_(0),
     gender_('U')
   {}

   PersonOptions& name(const string& n) { name_ = n; return *this; }
   PersonOptions& age(int a) { age_ = a; return *this; }
   PersonOptions& gender(char g) { gender_ = g; return *this; }
};

class Person
{
  string name_;
  int age_;
  char gender_;

public:
   Person(const PersonOptions& opts) :
     name_(opts.name_),
     age_(opts.age_),
     gender_(opts.gender_)
   {}
};
Person p = PersonOptions().name("George").age(57).gender('M');
like image 54
Fred Larson Avatar answered Oct 21 '22 08:10

Fred Larson


And now for the "Boost has something for it" answer:

The Boost Parameter Library seems to be a good fit to your use case.

like image 4
Éric Malenfant Avatar answered Oct 21 '22 07:10

Éric Malenfant


What if you made a parameter object that contained all the fields? Then you could just pass that, and just set whichever fields you need. There's probably a name for that pattern, not sure what it is though...

UPDATE:

Code might look like somewhat this:

paramObj.x=1;
paramObj.y=2;
paramObj.z=3;
paramObj.magic=true;
... //set many other "parameters here"

someObject myObject = new someObject(paramObj);

and inside the someObject constructor you can set defaults for things that were not already set (or raise an error if it was mandatory).

Honestly, I'm not a big fan of this solution, but I've used it once or twice when paramObj made sense by containing a set of data that usually all went together (so we could use it for more than just constructors), and it was better than multiple constructors. I found that it was ugly but it worked, YMMV.

like image 9
FrustratedWithFormsDesigner Avatar answered Oct 21 '22 07:10

FrustratedWithFormsDesigner