Interesting Research Paper on Constructors in OO Languages

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Jul 17 14:40:39 PDT 2013


On Wed, Jul 17, 2013 at 11:19:21PM +0200, w0rp wrote:
> I always just avoided confusion by limiting myself to a maximum
> of 5 arguments for any function or constructor, maybe with a soft
> limit of 3. Preferring composition over inheritance helps too.

My original motivation for trying to tackle this problem was when I was
experimenting with maze generation algorithms. I had a base class
representing all maze generators, and various derived classes
representing specific algorithms. Some of these algorithms have quite a
large number of configurable parameters, and the algorithms themselves
have different flavors, so some classes that already have many
parameters would have derived classes that introduce a few more.

Encapsulating all of these parameters inside structs was the only sane
way I could think of to manage the large sets of parameters involved.

Also, I agree that 3-5 parameters per function/ctor is about the max for
a clean interface -- any more than that and it's a sign that you aren't
organizing your code properly.  But in the case of ctors, it's not so
much the 3-5 parameters required for the class itself that's the
problem, but the fact that these parameters *accumulate* in all derived
classes. If you have a 4-level class hierarchy and each level adds 5
more parameters, that's 20 parameters in total, which is clearly
unmanageable.


T

-- 
Designer clothes: how to cover less by paying more.


More information about the Digitalmars-d mailing list