1. 18
  1.  

  2. 5

    The List::Util module and friends are the best modules.

    1. 4

      This particular case seems to me rather like obfuscation than optimization. The original code was quite readable even to people who do not know Perl.

      To understand the „improved“ version, I have to look inside the map_x_if_y() function (i.e. go to some other place in the code and then back) read a bit cryptic (of course, it is normal in Perl, so regular Perl developers will understand it) code and know what pairmap does.

      Yes, DRY usually is good, copy/paste is bad… but sometimes the less sophisticated, more straightforward „low-tech“ code is better – especially for readability and maintenance.

      This approach might make sense for more complex code, more repetitions of the same pattern on more different places in the code base.

      Remember Master Foo and the Programming Prodigy

      1. 0

        but sometimes the less sophisticated, more straightforward „low-tech“ code is better – especially for readability and maintenance.

        Something I’ve been bitten by is assuming that “less sophisticated” and “things that I would do as a beginner” are the same. The trick is to realise that as you get better, the patterns you might reach for will sometimes (not always) actually be simpler.

        If you have map_x_if_y in your vocabulary, in this case this usage is less sophisticated. This would also likely more resemble how someone would describe a problem in prose (” this value ends up being one of these three values, depending on these conditions”).

        This case, I don’t think this is really about DRY or copy/paste, more about flow control in general. And I think having nice vocabulary for flow control can be super helpful for having straightforward stuff (that I would consider lower tech than trying to track a bunch of if statements)