Where is the Single Source of Truth?

Dukhyun Ko
4 min readJul 14, 2020

--

It’s been two weeks since I started boot camp at Flatiron. I’m learning a lot and feel pretty confident up to this point, but at the same time, I feel uneasy about the remaining weeks I have left.

Things were going pretty smooth until I encountered Object Relationships. As my instructor gave a lecture about “belongs-to”, “has-many” and “has-many-through” relationships, I began questioning my knowledge in Object Oriented Class.

Define Object Relationships

After doing many labs and going through a code challenge. I realized that I complicated this topic for myself.

It’s actually very simple. Object Relationships is exactly what it’s saying it is. It’s a relationship between two objects.

“Belongs-to”, “Has-Many” and “Has-Many-Through”

First when we want to establish a relationship between classes, we want to classify their relationship into these categories to establish a “Single Source of Truth”.

Example
We will start by creating classes of Listing and Guest

1 - Relationship between a Listing(“Comfy Home”) and a Guest(“Sean”)!
Listing only has one Guest named Sean.
Guest only has one Listing titled Comfy Home.
THEREFORE: Listing belongs-to Guest, Guest belongs-to Listing.

2 - Our Listing got a second Guest (“Jane”)!
Listing now has two Guest(s) named Sean and Jane.
Sean only went to one Listing titled Comfy Home.
Jane only went to one Listing titled Comfy Home.
THEREFORE: Listing has-many Guest(s)(Sean and Jane), Guest(s) belongs- to Listing.

3 - Sean and Jane went to a different Listing(“Sunset Tree House”)!
Comfy Home has two Guest(s).
Sunset Tree House has two Guest(s).
Sean has been to two Listing(s)
Jane has been to two Listing(s)
THEREFORE: Listing(s) has-many Guest(s). Guest(s) has-many Listing(s)

Define the Single Source of Truth!

Using the relationship we defined above, we can write out the code.

  1. When two classes belong to each other, we can simply assign the instance object to each other and keep the single source of truth.
1. belongs-to and belongs-to relationship

2. Now things are a little different with has-many and belongs-to relationship. We can go ahead and do the same and assign the Listing instance to the Guest instances and still keep the single source of truth. The problem is, the initialize method only has one guest and we now have two guests. If we put another guest in the initialize, it would ruin our single source of truth! If we still want the Listing to have a method to call up on its guests, we have to build some more methods to get around it.

2. has-many to belongs-to relationship

Now, the Listing is not collecting Guest(s) object instances directly during the initialize, but it is able to call up on the guests that stayed at its specific listing.

3. Now, we really have to pay attention to the behavior of the classes. Since now we have Guests with multiple Listings and Listings with multiple Guests, we can’t assign either of their object instances into the initialize. Then how are we going to call on each other?

The answer is “Join Class”!

We are going to need to create a “Join Class” to connect each other without having more than one single source of truth!

3. has-many-through relationship

Trip class was created to be a join class that can contain Guest object and Listing object. Listing and Guest class can use Trip class to access each other’s information. Trip class belongs-to both classes.

Oh and the single source of truth?
Each class has its own set of information and only contains what they need to know. This information that is only within one class will eventually allow us to code other methods without triggering the single source of truth to break.

--

--

Dukhyun Ko
Dukhyun Ko

Written by Dukhyun Ko

Software Engineer 👨🏻‍💻 | Personal Trainer 🏋🏻‍♂️ | Commissioner 🏀

No responses yet