Using Helper Methods/Functions

Dayne T. Dewar
5 min readMar 8, 2021

When I first started on my coding journey, I would have one delieverable to complete and my thought process was always, “Do what needs to be done to get to the answer”. In return, I would have 1 method, with 8–12 lines of code, filled with if/else statements. Of course in the beginning I saw this less as a problem and more as me becoming the next Steve Wozniak. After showing my instructors, I could only imagine how they felt about seeing the next Bill Gates come to fruition. I turns out they didn’t take to kindly to my 11 line code for finding if a Student instance had a grade of above 65 over not. “It works, but it’s too messy. You could slim this down by using helper methods.”…huh?!?

What Is A Helper Method?

I’m 95% sure Bill Gates actually said this.

As you can see, Bill and I felt the same way, “If it ain’t broke, don’t fix it.” I’m not sure if that thinking is because of my fear of making a mistake and messing up my already working code, just natural laziness. Whichever it may have been, I looked passed it and took a shot at giving these “helper methods” a chance. Would have thought they would actually be pretty…HELPFUL…

Before I explain, think of it like this. You work at a ramen shop, and someone comes in to order a bowl of Tonkotsu Ramen. You take the order, run to the back, grab a knife to slice a few pieces pork, then, go and makes the noodles from scratch, now you have to get the ingredients for the broth, put them together and bring it to a boil. I’m no chef but If that were me making it, I just hope the person ordering wasn’t expecting to eat for a few hours. Leaving all the tasks up to one person isn’t impossible, but wouldn’t it make more sense and be a little easier to have other people to handle and HELP you with certain tasks.

In this situation, helper methods, are the employees you hire to do tasks for you. A helper does just that, you create a method for yourself, which may not be executing a lot but is there for you to call on later as you write your code. It may seem like a waste of time to get someone just to make noodles all day, but in the grand scheme of things it applies less pressure on the head chef, the cashier and anyone else who. In coding, it trims down your code and can save you a lot of time if you have multiple repeating lines. Thinking you’ll get confused with having a bunch of helpers and calling and remembering what does what? Just remember my favorite accornym, K-I-S-S.

Hopefully it doesn't hurt your feelings as much.

A helper can be as simple as one line of code that gives you just a piece of the puzzle.

The Pro’s

For one, your code gets a lot cleaner, and looks way nicer. If you’re someone who thrives on chaos, then clean code might not be your thing, however if you were in the business of having other developers give your code a look, it’s easier to explain what a method does if it only has one job as opposed to 3–4 different jobs.

Another pro, for me at least, is being able to name your methods whatever you want. If a situation came up where I needed the average of a set of numbers, I could write a helper method for it, and give it a simple name like “average”. Now whenever I call “average”, not only do I know what is going to be return, but it’s easier on my brain and more human to read it like that.

The Con’s

The only downside I see are less on the helper’s end and more on the Developers end. So long as you make your helper’s specific, by only give them 1 task to complete, and giving it a simple enough name, your confusion on what is being returned and where your errors lie is limited.

Why Did You Put Functions In The Title?

Recently, I’ve been learning JavaScript and if there’s one place I believe where helpers are a necessity it’s with JavaScript functions. Functions in JavaScript are pretty much the exact same thing as methods in Ruby/Rails. The thing is that JavaScript as a language is more explicit than something like Ruby. Ruby does a lot of the grunt work for you behind the scenes, so your code is already smaller by default because of this, where as in JavaScript, functions need to be very specific about what you want to accomplish and can get congested and bulky very quickly.

JavaScript sometimes sort of pushes you towards making helper functions for yourself. Callbacks are functions that you pass as an argument into another function…sound familiar? Now of course you could instead create a function on the spot, in place of a callback, but what happens when you have a new event listener down the road that needs access to the same data? Helper functions are super important here because things can get out of hand and cluttered very quickly. For Instance, if we are fetching data from a database that creates a div element for each object on the database and appends it to our DOM we could write it like so..

Everything is being handled in the fetch

However, what if we need to later make a POST request and make a new ramen? If we instead had a separate function for creating our ramen when given a set of data, it could help us later and look a lot nicer as well.

notice how I call on makeRamen in the .forEach

One of the things I love about coding is that there is almost never only 1 way of doing things, which gives everyone creativity and freedom to write as they please. What works for one may not work for another, so for me, helpers are something that I believe is for me and should be used as often as possible!

--

--