Trimming your code: for beginners

I’ve read that the only way to minimize bugs in your code, is to simply have less code to begin with! As a beginner, I’ve been more focused on my code working than efficiency. However, I’ve started to notice, the more efficient I can make my code, the greater the chances of it working!

Below are a few tips I’ve picked up by comparing my code (in Ruby) to more experienced coders.

Use a ruby class method

Map, reduce, find, sample, oh my! There are many many many ruby methods that can be used to make your life much easier. Using these methods can save you a lot of loops and if-else statements. However, just using them isn't enough. Keep an eye out for which of these methods would be the MOST EFFICIENT one to use.

The following is something I worked on (fairly simplified below). It shows how I went from a messy code to something neater.

Then used map and select

That looks much better! No need to create a new array and so on. I tried to go a little further and combines the select and map. The internet offered me the reduce method, which would look something like this:

However, I prefer the map and select. Maybe I was taking it too far.

Use the Boolean Data Type

Ruby will evaluate each expression and return a value. For Boolean data, this will return a true or false. Using this directly can help reduce some redundancies.

See if you can skip the if-else or ternary. This is one that I do all the time and when looking at the alternative it looks so much cleaner!

For example, if we need to return a true for a number greater than 10 and false or less we could use an if-else:

Or a ternary:

But we can take advantage of the statement’s return instead:

Use the || =

Another way to take advantage of the true or false return is by using the “||=” . This was introduced in my Flatiron Software Engineering course. Googling this operator shows there may be alternative uses with applications that may be beyond my reach at the moment. However, I can show you how I have used it, and how it saved me a bit of lengthy code.

The “||=” will assign a value (like only if what is left of the operator is falsey. Why would we ever need that?? Let’s look below at the adapted Flatiron example:

Here we can see that if the method is called without a name passed in, the name variable is falsey (remember nil is falsey!). This means the function will assign “stranger” to the name. Otherwise, the first line will not be executed and the name that was passed through will not be changed! Pretty neat stuff.

There’s more! Well lemme show you a handy trick I came across for creating hashes. Another example from Flatrion’s course work:

The above method, takes in a student name and their grade and updates a hash where the student grades are the keys and the name will be added to the value array. The ||= can be used to create keys and values in a new hash. The trick is that it helps the code check if a certain key already exists or not. If it does, then put the name in. If it doesn't, make the value array first, THEN put the name in. Saving you from writing some if-else states.

Don’t Get Attached

On a less technical note. I know you spent a long time figuring out exactly how to do something. But hey if there’s a one-word method that makes your hours of work into minutes… use it! Don’t get attached to your verbose and winded way of doing something only because of the time you’ve spent on it! Choose the easy way out.

Hopefully these tips are useful in writing more efficient code. But remember it’s not just the shortest code with the longest chains that is the winner. Be efficient but be effective. Readability is important! If other people can’t understand your code then it may not be very useful in the end after all. Sometimes creating another line to ensure nothing is lost in communication is the best way.

Written by

I love seafood, exploring new cities, board games and learning to love to code. https://github.com/sarakhandaker/portfolio

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store