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.
Back to
Meditations