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.


In reply to Be grateful for Perl OO by dragonchild

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.