Learning Object Relationships Through Space and (in less) Time

In December 1995, the Hubble Space Telescope captured this image:

To describe it as breathtaking would be an understatement when we look beyond its perceivable beauty. When astronomers saw it for the first time, they knew they were looking at more than just a bunch of colorful spirals. They were also looking at undeniable proof that the universe is, in fact, much, much bigger than anyone had previously imagined. There are at least 1,500 galaxies floating around in this image alone, each made up of billions upon billions of stars with their own set of orbiting planets. And then let’s add this final little detail: Of the entire night sky, this image (along with everything in it) can be squeezed into the size of a single pixel of whatever screen you’re currently reading this on, held at arm’s length. Truly, a ‘keyhole’ into a world too far away for us to ever perceive with our naked eyes.

It’s stuff like this that has always fascinated me about space. It’s a topic I’ve spent entire weekends learning about for no particular reason other than I find it interesting. And yet, about a couple weeks into Flatiron School’s Software Engineering program, I found it all to be quite useful as well.

Grasping the fundamental concepts of Object Oriented Programming became a lot easier — and even enjoyable — once I began associating the new things I was learning to things I already knew a lot about.. like space stuff. In this first blog post, I’ll walk you through this time-saving trick in action as we explore simple object relationships in Ruby.

Earth (a planet)

Let’s start our journey right at home: ‘Earth’. This will be our first ‘object’. In Object Oriented Programming (or OOP), objects are simply things that we describe to a computer as having a set of attributes and behaviors. Further, there are two types of objects we want to be mindful of: classes and instances.

While a class serves as a sort of ‘blueprint’ for our objects, an instance (of a class) represents objects made from a particular blueprint. In our case, ‘earth’ (with a lowercase ‘e’) would be considered an instance of the class called “Planet”, as shown here written in the Ruby programming language:

If we wanted to, we could create seven additional instances of Planet to represent the other heavenly bodies that make up our solar system. For now, we’ll stick to just our one (earth) and continue to build on it. You’ll notice in the screenshot above that planets have an attribute (‘attr_accessor’ as its syntax) called ‘name’, which as you might have guessed, will prompt us to name each instance we create. Pretty convenient, right?

Next, let’s add a few more details to our blueprint to give a computer the ability to do more with a Planet. Below, you’ll see that I’ve added comments underneath certain pieces of the code, which can be identified by their green text, beginning with the conventional comment syntax of a #.

Since Earth doesn’t float about in space all by itself, let’s now introduce a computer to another class of objects, called ‘Star’.

Sun (a star)

Following the path charted above, it’s easy to make sense of what’s happening in this next snippet of code:

At this point along our journey, we now have a planet called ‘Earth’ and a star called ‘Sun’. Before we introduce our third and final class object to a computer, let’s add one small addition to our code to describe just how our planet and star are associated with one another — our first ‘object relationships’:

We need only go two lines deep into our code and make one small addition to give a computer a giant leap in its understanding of our two objects. Adding the ‘star’ attribute here is interpreted by a computer to mean: A planet belongs to a star, and a star can have many planets — thereby covering two (of the total six) possible relationships that can exist between objects in OOP.

Alas, we’ve made it all the way up to the great..

Milky Way (a galaxy)

By once more revisiting the steps outlined above that have led us to this final stage of our journey, we’ll notice just how far we’ve come. In a matter of minutes, we’ve traveled lightyears in equipping ourselves with the knowledge and skills needed to be able to teach a computer all about the class called ‘Galaxy’.

From here, the only thing left to do is teach a computer that (1) a star belongs to a galaxy, (2) a galaxy can have many stars, and (3) thanks to our earlier work involving object relationships, we can now also say that a galaxy can have many planets through stars. And how do we represent this final object relationship in our code? (The following should look familiar to us.. )

That’s right! By simply adding ‘galaxy’ as an attribute of Star. Doing so not only establishes a particular association between a galaxy and a star, but also between a galaxy and a planet (of a star).

And Back Home Again..

Returning now from our journey through the cosmos (so to speak), we’ve seen a glimpse of just how powerful a tool ‘learning through association’ can be. I hope you enjoyed it as much as I have. From here, I imagine the future concepts I’ll be learning at Flatiron School — even beyond OOP — can continue to build upon this simple ‘space stuff’ analogy.

I guess only time will tell.

’Til then..

Kaizen.