DEV Community 👩‍💻👨‍💻

Michael D. Stemle, Jr.
Michael D. Stemle, Jr.

Posted on

Simple Code Tasks Should Be Simple

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.

Top comments (6)

Collapse
 
matthewpersico profile image
Matthew O. Persico

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.

Collapse
 
manchicken profile image
Michael D. Stemle, Jr.

What does this semantic difference mean for those writing code?

Collapse
 
matthewpersico profile image
Matthew O. Persico • Edited on

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. :-)

Thread Thread
 
matthewpersico profile image
Matthew O. Persico

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

Thread Thread
 
manchicken profile image
Michael D. Stemle, Jr.

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!

Collapse
 
antiloch profile image
Aleksashko

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!

The Complete Guide to Full Stack Web3 Development

>> Check out this classic DEV post <<