perlmeditation
dragonchild
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!)
<p>Recently, I found myself working with a C++ file. What it does is irrelevant. What's important is the following pseudo-code:
<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;
}
}
</code>
That switch has over 80 choices. Class_A and Class_B share an interface, but not a base class.
<p>The be-grateful part here is that, because of weak typing and lazy evaluation, we could do the following in Perl:
<code>
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;
}
</code>
A few of the many benefits that scaling 80 * 8 lines down to 80 * 1 + 8 (and being able to separate the two):
<ol>
<li>Easier to read because I can parse the concept
<li>Easier to maintain because it's easier to understand
<li>Easier to improve because I can see everything I'm touching
<li>Less likely for bugs to creep in because I don't have to keep 80 things in my head
<li>Just darn nicer to deal with!
</ol>
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.
<p>------<br><i>We are the carpenters and bricklayers of the Information Age.</i><p>Don't go borrowing trouble. For programmers, this means <i>Worry only about what you need to implement.</i>