Solve long, then solve fast.
Tags: programmingparadigm, programminglanguage, programmingtips, ruby
Published: 2018-10-26 19:56:05

---
title: Solve long, then solve fast.
published: true
tags: programming-paradigm,programming-language,programming-tips,ruby
canonical_url: https://medium.com/@ruthbrandsoprano/solve-long-then-solve-fast-5113a56f1fd2
---

When I first started programming, I fell in love with the ability to solve the same problem in many different ways. I also learned the hard way that long-handed tedium ALWAYS beats shorthanded shortcuts when you are first learning a concept.

To that end, I would like to show you a couple different ways to get through writing literally ONLY TWO METHODS in a Ruby exercise called “Unicorn”, part of a series called “Mythical Creatures”, using a magical Ruby tool called an "attr\_reader".

![](https://cdn-images-1.medium.com/max/186/1*-MxvO-39B_GBemYD87ljTg.png)<figcaption>This unicorn symbolizes your excellence</figcaption>

I’m using Ruby 2.4.1, the Minitest gem, the Atom text editor, and an incredibly dope font called “Monospace”.

After setting up the basics, I’m ready to make my first test pass.

Let's take a look:

![](https://cdn-images-1.medium.com/max/777/1*qXpd6uNQIhtLbwHwxBpT-w.png)

Based on the guiding principles of TDD, let's run the test and see what errors it gives us.

![](https://cdn-images-1.medium.com/max/1024/1*3t7yxB-wugb4Ykf6R96hcA.png)

Alright, dig it. Let’s make a method called \<name\> and reference my instance variable, @name, that I made in the initialize method inside of it!

![](https://cdn-images-1.medium.com/max/670/1*mkPKSnXCQPLbKvdcj0C7HQ.png)<figcaption>Gorgeous. Beautiful. I’m obsessed.</figcaption>

Let's run my test again and see if it passes.

![](https://cdn-images-1.medium.com/max/953/1*IzJ-pCn8KFPCy00YZMvJXg.png)

That’s cool! It works, my test pass and I can move on- and that is ALL that matters at this point.

Upon further inspection, however, we discover though that this method is three lines of code to do…sort of nothing. It's a getter method, sure, which means it shows me the data that it's referencing, but its lot of writing over time if I need to write three lines of code just to read the data the instance variable @name points to. What if I wanted to do this with other attributes of this class? What if it had a name, and a color, and a city of origin, and a breed, and a…I don’t know, other unicorn-y attributes? Is there, then, a way to do the same thing and not take up three lines of code for each attribute? I’m inherently lazy, so I would like to find out.

![](https://cdn-images-1.medium.com/max/794/1*3nHz08Nf1UDnqxUZHidINA.png)<figcaption>We’re going to die here.</figcaption>

Here is where the attr\_reader comes in. An attr\_reader is a shorthanded way to write getter methods as you just did above. It also makes your data public, the same way that writing out a full method does. It looks like this:

![](https://cdn-images-1.medium.com/max/737/1*xBXZ9Ggjdwx06hS5cXH_1g.png)<figcaption>using an attr_reader to replace writing out a &lt;name&gt; method</figcaption>

With this tool, you can shorthand the \<name\> method that we wrote earlier, and add others too, which we will see shortly. So, it saves you having to write out all that code over and over again. This becomes especially wonderful when we have lots of instance variables in our initialize methods.

**_A WORD OF CAUTION_**

When I was first starting to code, people were telling me “Just use an attr\_reader! Just use an attr\_reader!!”, so I used them, not understanding fully what it actually does. It took a very wise mentor to help me see that I didn’t know what the hell I was doing, so she made me write everything out by hand FIRST and then let me use the shortcut. I \*highly\* advise you do the same. Going full in and using a “shortcut” cost me more time in the long run.

We run our test again, and it passes.

Let's look at our next test:

![](https://cdn-images-1.medium.com/max/1280/1*9faudx-pm-0MAfNNS4c-wQ.png)

Upon running this test, we see the method \<color\> doesn’t exist:

![](https://cdn-images-1.medium.com/max/1024/1*bGgLMdj-5CPJFmy0j6bFnA.png)

Let's write it! First the “long” way, and then using an attr\_reader.

![](https://cdn-images-1.medium.com/max/406/1*cxzuj4ohK8KvkZn0gObPlw.png)

And now, using an attr\_reader:

![](https://cdn-images-1.medium.com/max/774/1*NghCfd-G-m-VfCLE3s6S2w.png)

Either one fulfills the requirement of writing a method \<color\> which returns the data associated with the instance variable @color. Both will pass our test, and move on to the next issue, which is another post for another time!

_BUT WAIT, ANOTHER WORD OF CAUTION_

If you want to add more than one thing to an attr\_reader, you MUST put a comma after each one except the last one. My pain, your gain.

Remember, write it out the long way a few times first before you start taking shortcuts-you don’t want to get lost in the woods! Unless the woods are full of unicorns. In which case, get as lost as you want.

![](https://cdn-images-1.medium.com/max/215/1*ylc2E2vYh4TsFtbcJdkF-Q.png)<figcaption>Margret the Unicorn is proud of you</figcaption>