Skip to main content

Object Oriented Design- Wading into Deeper Waters

Objects, classes, inheritance, polymorphism- it sounds a little like Biology, and in some ways, object oriented programming (OOP) mirrors the natural world. 

I've finally reached the OOP section in the Odin Project's Ruby Programming course. Many of the concepts are straightforward-especially to anyone who has read a little philosophy or studied evolutionary biology. The difficult part is translating the concepts into actual functioning OOP applications. 

I've started reading what looks to be a promising book on OOP design in Ruby. I've learned something already that may improve my code. Here's an example from chapter 2:

 class Gear
   attr_reader :chainring, :cog, 
   def initialize(chainring, cog)
     @chainring = chainring
     @cog = cog

   end

   def ratio
     (chainring / cog.to_f).round(2)
   end
end

This is the 'good' way to create a class. 
Below is the 'bad' way- I've highlighted the offending variables in red.

 class Gear
   attr_reader :chainring, :cog, 
   def initialize(chainring, cog)
     @chainring = chainring
     @cog = cog

   end

   def ratio
     (@chainring / @cog.to_f).round(2)
   end
 end

The point here is that you should hide variables, even from the class where they are defined. That way, if you need to alter the variables, you can do so with a single method definition. 

I'm going to implement this suggestion in the game I'm currently working on.

Comments

Popular posts from this blog

Einstein's Logic Puzzle (SPOILER ALERT!)

On Monday I began working through a Discrete Math textbook in preparation for some courses I'll be taking in January. There was a beautiful logic problem in Chapter 1, apparently created by Einstein. This is one version of it: Five men with  different nationalities and with different jobs live in  con secutive houses on a street. These houses are painted  dif ferent colors. The men have different pets and have   dif ferent favorite drinks.  Determine who owns a zebra and  whose favorite drink is mineral water (which is one of the  favorite drinks) given these clues:  The Englishman lives  in the red house.  The Spaniard owns a dog.  The Japanese  man is a painter.  The Italian drinks tea.  The Norwegian  lives in the first house on the left.  The green house is  immediately to the right of the white one. The photogra pher  breeds snails.  The diplomat lives in the yellow house. ...

CodeSchool vs Codecademy(or 'How socket inherits event listening methods and implements asynchronicity')

In this review I'm going to focus on the pedagogy that I see evident in some CodeSchool courses and compare them to  Codecademy. By pedagogy, I mean: 'How does CodeSchool teach?' and ' Does it do a good job of teaching?'. I'm going to argue that despite high quality videos, colourful web pages, and often ssspppeeeeeakkkiiiing...rrrreeally...slowly..., CodeSchool's pedagogy is inferior to that of Codecademy. There are many fantastic resources for learning to code on the web, and CodeSchool is one of them. So far I have completed courses in Ruby, Rails, Javascript, HTML/CSS, Jquery and Git on CodeSchool. The courses have all included high quality videos and colourful, interactive exercises- as well as  massive  pdf files of the slides ( the files take more than a minute to load on my machine .) The question is: does the higher production value mean better educational quality? The 'Try' courses on CodeSchool(such as Try Ruby and Try jQuery) are f...

Job as Entry Level Developer

After 4 months of work, sometimes focused, sometimes not, I accepted a job as an Entry Level Ruby on Rails Developer yesterday. This is after starting with zero knowledge on November 1, last year. Beyond knowing a little about coding (but getting the definitions of  REST and AJAX wrong), what were the reasons for the job offer?  I think it was the meetup group I started in January that made me stand out from the rest. The motivation for the meetup group was to help me become a better coder and to indulge my teacher instincts. After some initial meetings at the library and my home, an IT hub in town offered to host us. This meant extra advertising and prestige for the group. After announcing the meetup group at an Agile meetup group for developers, I got some volunteers to give talks. The first volunteer offered a talk on Ruby. As I was comfortable with Ruby I prepared a coding tutorial .  After the tutorial, which was attended by some beginners and some a...