What I Learned From a Single Lesson using Exercism.io

JC Smiley Jr
codeconnector
Published in
3 min readMar 17, 2020
Photo by Ben White on Unsplash

A little background

I have been teaching myself to code with JavaScript for the last two years. I have a Management Information Systems Bachelor’s degree, but that was 13 years ago and I haven’t written code since then.

What is exercism.io? It’s a website that allows users to practice coding in a huge variety of programming languages for free. What makes it fantastic is mentors review your code and make suggestions for improvements. This is beneficial for aspiring beginners or self-taught developers, like myself, to learn how to write good code and use good coding practices.

Just like good house building practices will transform a lean-to into a quality home, good coding practices transform code that runs into human readable, efficient, and easily maintained code.

I recently finished the “Protein Translation” JavaScript challenge. The goal was to break down RNA sequences into a series of three letter codons and translate the codons into proteins. Edge cases included certain “stop” codons and error messages.

Lessons learned from submitting 3 solutions to the same problem

Let me state, each solution worked, passed all test cases, and I finished in good time. The following was the mentor’s suggestions and my “A ha” moments:

1. Don’t use Magic Numbers.

JavaScript function with a default value as a parameter.

That “0” was a magic number. It was not meaningful, just useful. JavaScript has “null” and “undefined” variable types that can be use to test if a parameter exists. Don’t reinvent the wheel.

2. Learn other iteration techniques for looping through an array/object other than a basic “for-loop”.

I had a 38 line for-loop with 18 if-statements testing each codon possibility.

A better solution was to use a “for-of” loop with a array. This was much cleaner and less code. Use the best tools for the job, not just a tool.

3. JavaScript arrays and objects have methods that can be used as lookup functions. Don’t reinvent the wheel.

I was using loops to find a value in an array and object.

Don’t waste time, do this:

if(object[key]){do something}

if(bigArray.includes(searchItem)){do something}

Instead of this:

for(let i=0;i<array.length;i++){
if(bigArray[i] === searchItem){do something}
}

4. Should this array or object be rebuilt every time you run a function?

I had place a huge object {} and a big array [] inside smaller support functions that was called in each iteration of a loop.

The solution was to place the non-changing object or array high up in the primary function and then pass it to the support function when needed. This made the code more efficient.

5. This huge block of data makes it hard to read your code. You can’t see the tree because of the forest. Practice good separation of concerns.

The solution was to break the primary function into smaller functions and use loops correctly. This made it immediately clear what is happening and where.

I learned the power of a code review

In the last two years I have written thousands of lines of code and numerous side projects. I have learned a lot. But in this short time period I was forced to accept that I don’t always write good code. The code starts off readable and slowly converts to non-readable code like “x = 0”. A code review is an opportunity to learn from others, reflect on what you don’t know, and observe your bad habits.

You can follow my journey to breaking into the tech industry on twitter at @JCSmiley4. I am doing the #100DaysOfCode (currently on round 3). Connect with me on LinkedIn at www.linkedin.com/in/jcsmileyjr. Thank you for reading and I hope you learn along with me.

--

--

JC Smiley Jr
codeconnector

Front End Developer, Tech Meetup Organizer, Gardener, and Outdoor Enthusiast