http://qs321.pair.com?node_id=230610

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:

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; } }
That switch has over 80 choices. Class_A and Class_B share an interface, but not a base class.

The be-grateful part here is that, because of weak typing and lazy evaluation, we could do the following in Perl:

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; }
A few of the many benefits that scaling 80 * 8 lines down to 80 * 1 + 8 (and being able to separate the two):
  1. Easier to read because I can parse the concept
  2. Easier to maintain because it's easier to understand
  3. Easier to improve because I can see everything I'm touching
  4. Less likely for bugs to creep in because I don't have to keep 80 things in my head
  5. Just darn nicer to deal with!
I'm not attempting to slam C++. There are tons of reasons to work in C++ over Perl, the main one being that it's a technology choice that was made 4 years before you heard of the project. But, that doesn't mean I can't have a soft spot in my heart yearning for the greener pastures of Perl.

------
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.