I Didn’t Want to Break My Code

Posted by on October 31, 2017

#Miriam Peskowitz

I was so excited to learn web-scraping. I’d taken a workshop a while back using Python and the descriptively-named Beautiful Soup library, and I’d worked on some APIs for a Code for Philly project. I couldn’t wait for this part of the Flatiron curriculum and for the first project: the vaunted CLI Data Gem project. I was geek-thrilled to watch the video about bundling, too, and requirements, a combination of mysteries I’d always wanted to solve: where all these files with fancy names come from and what they do, and how you tell files to interact with each other. I was a happy camper.

I was so psyched about my mini-gem. It worked. It scraped four kernels of daily information – a meditation, a word of the day, a writing tip of the day, and some ideas for conversation – and presented them to you, the user. It’s the perfect starter to a day, which is why I happily called it Day Starter. When my Terminal gave birth to the output, well, I was the proud parent of an awesome working gem. Had we not moved away from Terminal output decades ago, well, my app could be the first thing that millions would awake to, appreciatively, each day. I’ve built things with code before, but this was the best. I didn’t want to change it ever.

Round 1

#You Know the Downfall’s Coming

Except, as Tech Coach Enoch noticed about 28 minutes into one of our sessions, my gem worked without actually using Object Orientation.

“But it works,” I said.

“Yes,” he replied.

“There are classes,” I continued.

“Yes,” he agreed. “But there are no objects created. We can’t pass you along without an object. You forgot to use Object Orientation.”

I opened a file whose lines I’d commented out, to show him that I’d started to create an object, but then realized I could make the app work without it, and so I did just that. Don’t they say that working code is good code?

We were 28 minutes into our 30 minute session.

“Good,” he said, always upbeat. “You’re on your way.”

It’s not hard to code an object. I take that back. Until you “get” object orientation, it’s abstract and your brain has to constantly supply the 95% of stuff you’re not seeing in your code. You have to think through how the classes collaborate and what their relationships are, as if it’s a softball team or three kids working on the same Minecraft world. The biggest question: How’s everyone getting along and working together?

There is a flow and a rhythm to creating an object class, with repeatable questions to ask: What is this object? What does it do? What properties will it need to do its thing? What does every instance of it need when born? What responsibilities does it have?

Indeed, the example we watched in video after video was baby = Baby.new. Objects are creatures. They’re modeled after real life. We’re in control of the design.

You’d think I’d jump back in, rework my dear gem and move on. After months spent with Javascript, I loved the literary wordiness of Ruby. In truth, though, it was hard to reopen the Sublime files.

#Code-attachment To code my Day Starter gem to the next level, I had to break it, and I didn’t want to do that.

Like everything else, coding is emotional and human; it emerges from how we like to do our work, our quirky habits of creativity, and what we like to hold onto. At the same time as I was coding the gem, I was editing a 60 page chapter for my book, a chapter about Python projects for beginners. As much as was humanly possible, my goal was to remove the abstractions, to clear away anything that could break a beginning-to-code reader’s attention. My job was to highlight the feeling of magic when code works. I experimented with ways to rewrite code, over and over. I was no stranger to taking things apart to make them better.

Yet my gem went untouched for days, frozen in time.

I didn’t want to destroy what was working. The fact that no one in their right mind would care about or use an app that pushed information to the Terminal finally convinced me go in and make changes. We hold onto silly things. I made myself fetch the files from my github repo, and start the destructive part of the creative process. Refactor I must.

#Happy Ending? I started over. It happens. I chose a single site to scrape from: audiodharma.org, because I checked, and it’s HTML was well-organized, a must for web-scraping. Nothing was manipulating the DOM. I reread lessons, watched more videos, signed up for studyhalls and codealongs and lab, and in this, a new-and-much-simpler app emerged that scraped five of the most recent meditations on AudioDharma. When you want to listen to a meditative talk, you don’t really need to spend 20 minutes swiping through the site to pick the best and most perfect one. By the time you choose, you’re out of time to meditate.

You want five, or even one, so you can get right to it.

Code snippet

The new gem, Meditation, was simpler but went deeper. It initiatlized with an instance of the scraper class and used the scraped data to create instances of two other classes – Todays_choices and Meditation. The classes collaborated simply and in an organized fashion. I’d realized over the past few weeks that I tend to overcode. I add methods I don’t call and use more complicated patterns than I need. In the new gem, my goal was to create only the necessary methods, and nothing else.

Code snipper, working CLI DATA GEM

(Thanks to Corinna for pointing out that most windows machines block system/open, and suggesting I add the actual url instead.)

#The Grand Finale Go to Rubygems.org, type meditation in the search bar, and you’ll see my gem.

Meditation on Rubygems.org

Github Repo