What Can You Do With Code?

The language of the 21st century is undeniably digital. We're on our computers and cell phones most of the day, whether responding to emails, checking our social networks or reading the latest news. Tech luminaries like Steve Jobs, Bill Gates and Mark Zuckerberg loom large in the public consciousness, and there has been an increasing focus on questions of technology in society with questions around net neutrality and NSA wiretapping. Given technology's increasingly prominent role in our lives, it's of little surprise that there has been a push to educate a wider swath of the populace in Computer Science and programming. Not only does the tech sector continue to be a reliable place to find employment, but even for those who don't plan to become programmers, understanding code is like understanding the English language - it's a significant basis of communication in our society. Therefore, nearly everyone agrees that spreading knowledge of Computer Science and coding is a good thing.

And yet, response has been achingly slow. While there is no shortage of schools, tutorials and other means to learn code (and even other resources extolling the benefits), its inherent value has not been communicated well. How exactly does the average user benefit from understanding code? Nowhere can this be more clearly seen than in the recent "I Can Be a Computer Engineer" Barbie book. Ostensibly meant to encourage young girls to pursue this path, the book paints Barbie as a poor user who doesn't understand computers well at all. She inadvertently infects several computers with a virus, and spends most of the story trying to restore the computers from backups. But worst of all, she self-identifies as a "game designer" who creates a game about a cute robot dog, and relies on her male friends to actually program the game and purge the virus. This prompted a severe backlash, a public apology from Mattel and, most amusingly, Feminist Hacker Barbie, a web app created by female engineer Kathleen Tuite. In this app, users can rewrite the words for various pages of the book, portraying Barbie as a true computer engineer (or not).

Feminist concerns aside, it's clear that those who don't yet program have no idea about what programming is actually like. As a software and web developer with a Bachelors in Computer Science, I will do my best to make this clear. Programming largely revolves around manipulating data, but I'm not referring to the incomprehensible (and totally fictitious) set of ones and zeros running across any movie computer screen (very few programmers work directly with binary). The types of data used by most programmers should actually look very familiar: the text of a tweet, the URL for an image or the number of unread emails. Basically, data is any type of digital value or information that is of interest to the programmer and users. And when we think of data in that sense, understanding the ability to manipulate it becomes very powerful indeed. Let's try an example to understand exactly how data is used by applications.

Data is usually arranged in tables, which relate different pieces of data to one another. For example, here's a table of pizza restaurants (a subject near and dear to my heart):
As you can see, there are several data attributes for each restaurant: the name, the address, the town, and the rating. When you have data arranged this way, you can reorder the different entries to sort by any of the column attributes, either ascending or descending. In fact, whenever you use Yelp (or some pizza-specific version that I hope is in the works), this is exactly what the app does behind the scenes - it searches its database for the data attributes that are most relevant to you, the user. In this case, this attribute is usually proximity to your current location, but if you specify the type of food or rating quality, the table is searched with more constrictions on its entries. This mechanism is the cornerstone of how data is actually stored and used, so it's very important to have your data arranged this way. But what if you don't? How can you turn unstructured data into structured data? Let's take a look at a list of webcomics, taken from the comics.txt file on TryRuby:

Achewood: http://achewood.com/
Dinosaur Comics: http://qwantz.com/
Perry Bible Fellowship: http://cheston.com/pbf/archive.html
Get Your War On: http://mnftiu.cc/

It may not be a table, but this list looks a lot like it: a series of entries, each with two attributes - the name and the URL. At first blush, this may appear to already be structured, since it looks enough like the previous table. But in fact, we just wrote out the text, and the computer has no way to know how all these texts relates to one another. It's no more structured than if you wrote this list with pen and paper. So now we have to put this text into a data structure. Let's see how we would do this in Ruby code, also taken from TryRuby:

def load_comics( path )
   comics = {}
   File.foreach(path) do |line|
     name, url = line.split(': ')
     comics[name] = url.strip

What's going on here? First, we defined the program with the def statement, and we called it load_comics. Path just refers to the directory path to the comics.txt file. Next, we created a hashtable with the two curly braces and called it comics. A hashtable is a data structure that will associate two values together (in other languages, this is called a dictionary because it resembles the association between a word and its definition). The File.foreach line indicates that the instructions coming after it (and indented further) will be repeated for every line of the text file. The next line of code splits each line of file text into the name (the text from before the colon) and the url (the text after the colon). The line after that associates that line's name and url together in the hashtable (the strip just removes any blank spaces). After that, the File.foreach instructions are closed with the end statement (you'll see it's been indented back), and then the comics hashtable is returned to us so we can see what it looks like. Finally, the whole program ends, and we get this:

{"Achewood"=>"http://achewood.com/", "Dinosaur Comics"=>"http://qwantz.com/", "Perry Bible Fellowship"=>"http://cheston.com/pbf/archive.html", "Get Your War On"=>"http://mnftiu.cc/"}

This may not look too different from before, but to the computer, it's almost like having one of those structured data tables. Now we're able to further manipulate this data and add to it as we see fit. We could even start building our own app to use this now-structured data.

My initial plan was to show more examples, but given how long this one turned out to be, I will save them for another time, if you all let me know you're interested. In the meantime, I hope this tutorial made sense for you, and that if you haven't yet coded, you are inspired to explore TryRuby or another coding resource. By exploring the world of coding even a little bit, you can start to gain a much clearer understanding of how it drives the technological and data-driven world we live in.