Simple Code Tasks Should Be Simple
Mike Stemle

Mike Stemle @manchicken

About: I have been a software professional since I was in high school in 1998. I'm enthusiastic about open source, and I really enjoy working in unusual software systems or within strange constraints.

Location:
Yardley, PA
Joined:
Sep 25, 2020

Simple Code Tasks Should Be Simple

Publish Date: Sep 29 '20
6 6

I frequently see people do simple things in the most complicated ways with dynamic languages. I suspect much of this is a carry-over from how we teach algorithms and programming in universities. If you want your code to be readable, and you want it to be maintainable long-term, then simple code tasks should be simple.

Merging Two Lists

This is simple. You've got a list of fruits, and a list of vegetables, and you want to merge them into a list called "produce."

In Perl:

my @fruits = qw/apple banana mango/;
my @veggies = qw/broccoli asparagus spinach/;
Enter fullscreen mode Exit fullscreen mode

In JavaScript:

const fruits = ['apple', 'banana', 'mango']
const veggies = ['broccoli', 'asparagus', 'spinach']
Enter fullscreen mode Exit fullscreen mode

Some folks will want to use iteration, or even a push() function of some sort here, but neither is necessary. A simple assignment statement will work just fine.

In Perl:

my @produce = (@fruits, @veggies);
Enter fullscreen mode Exit fullscreen mode

In JavaScript:

const produce = [...fruits, ...veggies];
Enter fullscreen mode Exit fullscreen mode

Not very impressive, I know, but watch what happens when I do the same thing with associative arrays (a.k.a. Objects, a.k.a. hashes). Now we're going to have produce items, with their colors.

In Perl:

my %fruits = (
  apple  => 'red',
  banana => 'yellow',
  mango  => 'light-orange');
my %veggies = (
  broccoli  => 'green',
  asparagus => 'green',
  spinach   => 'green');

my %produce = (%fruits, %veggies);
Enter fullscreen mode Exit fullscreen mode

In JavaScript:

const fruits = {
  apple:  'red',
  banana: 'yellow',
  mango:  'light-orange'}
const veggies = {
  broccoli:  'green',
  asparagus: 'green',
  spinach:   'green'}

const produce = {...fruits, ...veggies};
Enter fullscreen mode Exit fullscreen mode

It's super cool to have slick code that does neat things, but when it comes to squishing data together keeping things simple is always better.

One Exception: When you're using JavaScript, the spread operator (...) is limited to the maximum limit supported by Function.apply(), which (as of the time of this post) is 65,536 total values.

Anyway, I had fun writing this and I hope that your code brings you joy.

Comments 6 total

  • Matthew O. Persico
    Matthew O. PersicoSep 30, 2020

    This is why you will hear experienced Perlers (Perlizens?) insist that an array is not a list. If you understand that an array is a list interpreted in numeric indexed order and a hash is a list interpreted in pairwise key value fashion, then you are more likely to perform "list" operations when beneficial. I think the Perl literature needs to do more than it already does to make a "list" a real, live actual data structure, distinct from the array and hash "storage" of lists.

    • Mike Stemle
      Mike StemleSep 30, 2020

      What does this semantic difference mean for those writing code?

      • Matthew O. Persico
        Matthew O. PersicoSep 30, 2020

        It means they realize they can do list-y type things and not resort to loops, i.e. the answer to your followup question is your article. :-)

        • Matthew O. Persico
          Matthew O. PersicoSep 30, 2020

          Or, more precisely, if the semantic difference was more distinctly taught, you wouldn't have had to write your article.

          • Mike Stemle
            Mike StemleSep 30, 2020

            Yeah, most dynamic languages seem to have this same difference. I think that for beginners it is semantics, but for those who are starting to come into their own in their coding practice it's a super important concept to understand.

            Thanks for this contribution!

  • Aleksashko
    AleksashkoDec 2, 2021

    Insightful read. I could remember having a headache reading a senior developers code, it took me about a week to fully grasp what code does what. I will definitely keep this mind in my future projects. Nice write up!

Add comment