The art of Abstraction

Probably one of the coolest parts of coding is the art of abstraction, the ability to write one thing to say many things.

Part of my training in Flatiron involved creating a Ruby CLI Application. As much as I was ready for it, I was taken aback by how much needed to be done in order to achieve what I wanted.

I decided on a Music Writing App, that would take and create Chord Progressions and add them to a song, a sort of song scaffold. I thought the most difficult aspect would be the logic of music theory, how to add measures, or transpose keys. I was wrong

It was the sticking user interface.

I realized just how tricky it was to write code that would adapt to various user inputs. At first I created a switch statement that would make a decision based off a user’s input, but very quickly my code was littered with stagnate switch statements. Puts statements were more common than any other statement, and with each new ask for user input meant creating whole new methods and responses. It was really messy.

Then came my first big abstraction, a display message handler. I created a method that would take an array of strings and puts them in a consistent format. Gone all those stinking puts!

The second is when I discovered the method(:method). Here I could create an array of methods to be called on when a user makes a selection. Inputing 1 would output the first method, inputing 2 the second and so on. All of a sudden I had dynamic options that didn’t need any sort of Case / When. Thank heavens.

And then I got crafty by passing dynamic parameters to these callback functions. By creating one method to handle input and output, I was able to reduce redundancy, improve scale and readability and made my day oh so happy.

So needless to say, I’m looking forward to creating more applications with stating as little as possible.