April 10, 2005

Code quality, part IV: Nameless abstractions are just complexity

I recently bumped into this:

foreach my $module ( grep( !/^Perl$/, $installed->modules() ) )
{
    if ($module =~ /DBD::(.*)$/) {
        $installed_DBD_modules{lc($1)} = 1;
    }
}

For the non-perl-literate ones: The code block runs through the list of Perl's external code modules and collects the names of installed database drivers into a hash table. We now focus on the grep( !/^Perl$/, $installed->modules() ) part, which is the enumeration we're looping through. The modules() call (from an external code library) returns not just the list of installed modules, but also "Perl" itself. The grep statement is there to remove this, thus allowing the code inside the loop to just concentrate on the external modules only.

I see the grep statement as an abstraction layer. It hides (or attempts to hide) the fact that the data source also returns perl itself, not just external code modules. However, as it is implemented currently, I view the grep part as being nothing but unnecessary complexity - removing it wouldn't affect the result at all as the in-loop regex wouldn't match "Perl" anyway!

This particular abstraction layer has one problem: It doesn't really abstract out anything, as people cannot understand its operation without parsing the line in their heads. Now, if the grep statement and its contents were pushed out into a method such as "GetInstalledModuleNames()", things would turn out entirely differently. Also, it would make the code more easily maintainable as changes to the filtering method wouldn't affect the call site (or perhaps several of them) anymore.

Therefore, naming is essential. Often, naming means extracting code into methods. Don't avoid it. It's not a sin to have a method that gets called only once. Neither is it bad practice to have a method with but a few - or even just one - lines of code. Code length is irrelevant, readability rules. Most complex expressions (regexes, heavy boolean conditions, non-trivial string catenations etc.) are detrimental to code readability if embedded in a block of other code. Not so when they're well secluded in their own little methods.

Rule of thumb: Never mix trivial and non-trivial code in a single method. "GetInstalledModuleNames()" is always easy to understand, but its implementation may range from very easy (like here) to quite difficult (do the same in C++ for example!). The name is what makes a potentially complex operation trivial.

Posted by Jouni Heikniemi at April 10, 2005 09:10 AM
Misc. programming
Comments

How do you like

grep {
/DBD::(.*)$/ && ($installed_DBD_modules{lc($1)} = 1)
} $installed->modules();


:-)

Though one wonders why it would go into a hash with a counter. Can a module be installed more than once?

I suppose there is a reason why this (which doesn't remove "DBD::" or convert to lowercase) wouldn't have been enough:

@DBD = grep /DBD::/ $installed->modules();

Posted by: Anonymous (coward) at May 8, 2005 07:25 PM

Clever people here. I will try it out. regards

Posted by: bill at December 13, 2005 08:25 AM

[url=http://groups.msn.com/PurchaseViagra-DISCOUNTatBestOnlinePharmacy/]purchase viagra[/url] fwlfbq [url=http://groups.msn.com/BuyViagraonline-SafeSecureOnlineShopping/]buy viagra online[/url] fwlfbq [url=http://groups.msn.com/ViagraPrescription-USLicensedPharmacies/]viagra prescription[/url] fwlfbq [url=http://groups.msn.com/OrderViagraOnline-BestUSPharmacyOnline/]order viagra online[/url]

Posted by: Suzan at March 12, 2008 09:40 AM

[url=http://groups.msn.com/CheapVIAGRA-DISCOUNTatBestOnlinePharmacy/]cheap viagra[/url] facmrdx [url=http://groups.msn.com/GenericViagra-SafeSecureOnlineShopping/]generic viagra[/url] facmrdx [url=http://groups.msn.com/OrderViagra-HighQuality/]order viagra[/url] facmrdx [url=http://groups.msn.com/BuyCialisOnline-BestUSPharmacy/]buy cialis online[/url] facmrdx [url=http://groups.msn.com/CialisGeneric-LowestPricesGuaranteed/]cialis generic[/url] facmrdx [url=http://groups.msn.com/OrderCialis-FDAApprovedPharmacy-/]order cialis[/url] facmrdx [url=http://groups.msn.com/CheapCialis-TrustedPharmacy/]cheap cialis[/url]

Posted by: Anonyme at March 19, 2008 10:27 AM

WItNs2 ndivksvajfzb, [url=http://fimtusqrhkuq.com/]fimtusqrhkuq[/url], [link=http://cvodlsapndbr.com/]cvodlsapndbr[/link], http://eazilccmjatb.com/

Posted by: awlwjehi at June 3, 2008 05:47 PM
Post a comment









Remember personal info?