A Note-Taking Analogy about ‘Helper Methods’ and a Few Examples

Cartoon of someone hearing a speech bubble that read’s ‘tHERE’s 4 KEY CoNCEPts… 1, 2, 3, 4'. There is a dotted line connecting the speech bubble to to the person’s brain but their brain only has a ‘mental cache’ that is 3 lines long. The dotted continues through their head and ends pointing to their hand which is taking notes.

With regards to notes and note-taking, notes are like ‘helper methods’ for your brain. They form a bridge between a lecture or a lesson and its implementation. They help us break down complicated ideas into smaller concepts. And because they’re written down you can always go back to them which is nice.

Once you understand a concept you can build off of it. You can try it out in various contexts etc, etc. Helper methods in Ruby work similarly. They define a small task in as succinct a way as possible. Once defined a helper method can be implemented to perform their task in different contexts in your code. The methods that employ a helper method become more readable and more succinct.

One more thing on note-taking. In terms of how I take notes, I like to use Notion. It’s free and I can write Ruby code in it. Evidently you can also write many other languages in it as well. In Ruby mode it maintains the same color scheme and format as Ruby and it’s easy to share, save, edit, etc. I’ve already shared them in the past, and people seemed to like them. Here I’m going to share some Object Oriented Programming code snippets, mainly things related to helper methods. These will be snippets from my code that I wrote in Notion. At the bottom of this post I’ll drop a link to a file called ‘artist-<painting>-gallery’. This has all the code for this particular domain model in its entirety, for context.

Helper Methods Hidden in Plain Sight

In this example the set up is like this:

artist has many paintings and gallery has many paintings. They are connected through the paintings.

We’re interested in the artist’s relationship to their paintings for this upcoming example. Each painting holds the unique attribute of the artist’s connection to it. Their unique artist ID. But here’s the catch, unfortunately in this situation the artist herself cannot tell if a painting is hers or not because she has no way to see that information, the artist ID.

Pablo Picasso ‘Girl Before a Mirror’ 1932, oil on canvas, 64 x 51 inches. A colorful, semi-abstract painting, with a representation of a woman, made up of a combination of soft shapes and horizontal lines. She looks in a mirror expressionlessly. Through abstraction there are multiple perspectives being presented at once.

Now the artist needs to know, not only where but what are all her paintings. Since this is not real life and only an example the artist is going to use Ruby to solve this problem. She knows she needs to literally iterate over every painting ever made. And if she can do this it doesn’t matter if she can tell if she made a painting or not because the paintings will tell her. They hold the single source of truth. And the artist knows how to do this magic trick because she knows in Ruby its just a class issue. Using the class of Painting, which in a way will be her helper method, she dips into one of the most powerful class methods available to her .all. And with a ‘Painting.all’ followed by a ‘.select’ she is able to go through ALL the paintings and select the ones that have her matching signature. She groups those all together into an array. Now through the power of Ruby she has all the paintings she made and this big little problem has been solved. See the ‘def paintings’ method below to see how she did it.

class Artistattr_reader :name, :years_experience
@@all = []

def initialize(name, years_experience)
@name = name
@years_experience = years_experience
@@all << self
end

def self.all
@@all
end

def paintings
Painting.all.select{|painting| painting.artist == self}
end
end

Then she wanted to know what galleries she was represented by (aka what galleries or gallery ‘has-many’ of her paintings) or if she even was represented by a gallery at all. This time she uses the .paintings method as a helper method. Iterating over herself with the .paintings method she just made, she is able to bring up the array of all her paintings, and from it return what galleries have paintings of hers. To do this she uses .map (as opposed to say .select) to manipulate an existing array of paintings, the very same paintings that, through Ruby, told her she made them. The .map method will return that array of paintings she made but reduce it according to the the block parameters set up. Inside the pipes, aka the block parameter, she enters ‘painting’ which will grab all the paintings she has made. Outside the pipes and after repeating the entry from she made for the block parameter she uses the .gallery (an attribute selector) which will show her what galleries her paintings are in. She placed a .uniq at the end so that none of the galleries will be repeated. See the ‘def galleries’ method below to see how she did it.

class Artistattr_reader :name, :years_experience
@@all = []

def initialize(name, years_experience)
@name = name
@years_experience = years_experience
@@all << self
end

def self.all
@@all
end
def paintings
Painting.all.select{|painting| painting.artist == self}
end
def galleries
self.paintings.map{|painting| painting.gallery}.uniq
end
end

We are glad Ruby could help figure it out for our artist in this example, sorting out which paintings are hers and what galleries her paintings are in, but what about the art collectors? What they want to know is who the most prolific artist is. That is, who is the artist who makes the most paintings in the shortest amount of time? This time we are going to make a helper method, not required by a specific problem or deliverable, to determine an artists proficiency. In turn we will use this .proficiency method to figure out who the most proficient artist is. By counting an artists paintings and dividing that number by their years of experience we get a number representing their proficiency. See the ‘def proficiency method below to see how we did it. Beneath that method, still inside the code, see how we employed this helper method in the method ‘most_proficient_artist’.

class Artist

attr_reader :name, :years_experience
@@all = []

def initialize(name, years_experience)
@name = name
@years_experience = years_experience
@@all << self
end

def self.all
@@all
end
def paintings
Painting.all.select{|painting| painting.artist == self}
end
def galleries
self.paintings.map{|painting| painting.gallery}.uniq
end
def proficiency
self.paintings.count.to_f / self.years_experience
end
def self.most_prolific
Artist.all.max_by{|artist| artist.proficiency}
end
end

Thanks for reading my blog, this is been my first post as a blogger. To let me know what you think of anything I covered here email me at benmorgancleveland@gmail.com

artist — <painting> — gallery