In my current position, I work with both Perl and C++. Personally, I prefer working in Perl, but see a very important place for C++ in a number of applications. (Far less than it's used for, but I'm biased towards more intelligent and less insular languages ... like Perl!)
Recently, I found myself working with a C++ file. What it does is irrelevant. What's important is the following pseudo-code:
That switch has over 80 choices. Class_A and Class_B share an interface, but not a base class.for (int i=0; i < SomeValue; i++) { Class_A *a; Class_B *b; int SomeID; if (SomeFlag != SomeValue) { a = (Class_A *) some_param; SomeID = a->getID; } else { b = (Class_B *) some_param; SomeID = b->getID; } switch (SomeID) { case FIRST_CASE: if (SomeFlag != SomeValue) { SomeArray[i].value = a->FirstMethod; } else { SomeArray[i].value = b->FirstMethod; } break; case SECOND_CASE: if (SomeFlag != SomeValue) { SomeArray[i].value = a->SecondMethod; } else { SomeArray[i].value = b->SecondMethod; } break; } }
The be-grateful part here is that, because of weak typing and lazy evaluation, we could do the following in Perl:
A few of the many benefits that scaling 80 * 8 lines down to 80 * 1 + 8 (and being able to separate the two):my %CaseToMethod = ( FIRST_CASE => 'FirstMethod', SECOND_CASE => 'SecondMethod', ); for my $i (0 .. $SomeValue - 1) { my $method = $CaseToMethod($some_param->GetID); $SomeArray[$i]{value} = $some_param->$method; }
- Easier to read because I can parse the concept
- Easier to maintain because it's easier to understand
- Easier to improve because I can see everything I'm touching
- Less likely for bugs to creep in because I don't have to keep 80 things in my head
- Just darn nicer to deal with!
------
We are the carpenters and bricklayers of the Information Age.
Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Be grateful for Perl OO
by artist (Parson) on Jan 28, 2003 at 15:45 UTC | |
by Elian (Parson) on Jan 28, 2003 at 16:54 UTC | |
by dragonchild (Archbishop) on Jan 28, 2003 at 15:48 UTC | |
by gjb (Vicar) on Jan 28, 2003 at 22:24 UTC | |
Re: Be grateful for Perl OO
by pdcawley (Hermit) on Jan 28, 2003 at 19:13 UTC | |
Re: Be grateful for Perl OO
by John M. Dlugosz (Monsignor) on Jan 28, 2003 at 17:26 UTC | |
by tall_man (Parson) on Jan 28, 2003 at 18:09 UTC | |
Re: Be grateful for Perl OO
by Steve_p (Priest) on Jan 30, 2003 at 15:38 UTC | |
by dragonchild (Archbishop) on Jan 30, 2003 at 16:45 UTC | |
by RhetTbull (Curate) on Jan 31, 2003 at 15:31 UTC | |
by dragonchild (Archbishop) on Jan 31, 2003 at 15:45 UTC |
Back to
Meditations