in reply to Code Maintainability
my $circle = Circle->new(4.5, 2.3, 2.2, 1)
Do you know which parameter does what? No, of course not, you'd have to memorize the order of parameters, which nobody can or want for larger libraries.
Consider this in contrast:
my $circle = Circle->new(); $circle->set_radius(4.5); $circle->set_origin(2.3, 2.2); $circle->set_visibility(1);
Although the second version is bloated, it's much more readable. So in terms of maintainability, it wins. IMHO. I don't like it, but it's easier to understand, and at least in terms of readability it also scales to more arguments.
But wait - can't we do better? The repeated part $circle->set_ is ugly, and repetition is the enemy of good programming. Consider this instead:
my $circle = Circle->new( radius => 4.5, origin => [2.3, 2.2], visibility => 1, );
That's not shorter than the second example, but much less bloated, because of less repetition. But it's still as readable, and it scales well (ie even if you have eight or ten arguments to new() you can see what it does).
(If you are a Perl 6 fan boy like me, you'll be pleased to learn that Perl 6 provides such constructors with named parameters by default. Of course they can be overwritten).
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Code Maintainability
by mpeever (Friar) on Dec 04, 2008 at 16:52 UTC | |
by autarch (Hermit) on Dec 06, 2008 at 16:32 UTC | |
Re^2: Code Maintainability
by jeffa (Bishop) on Dec 04, 2008 at 17:19 UTC | |
by Porculus (Hermit) on Dec 04, 2008 at 21:16 UTC | |
by holli (Abbot) on Dec 05, 2008 at 08:18 UTC | |
by mr_mischief (Monsignor) on Dec 05, 2008 at 23:21 UTC | |
Re^2: Code Maintainability
by James Board (Beadle) on Dec 06, 2008 at 03:09 UTC |