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

Ruby, RSpec, Linux and Relational Algebra (or "What I'm doing this Christmas")

Last week was challenging- I'm beginning to appreciate the need for a programming buddy.  So far, I haven't found a Ruby IDE that helps with missing brackets or illegal syntax. This means hours spent looking for errors which more experience (or an IDE) would have solved in an instant. I've spent a lot of time on Stack Exchange looking for solutions to problems that many have already had- so far I haven't had to post a single new question (though it's evident that many people post duplicate questions, not having looked for a solution first). I spent longer on the Ruby project than anticipated, BUT I really enjoyed learning about Behaviour Driven Development. Thinking of tests in RSpec and then watching my Ruby script pass (eventually) was like passing an exam (though  one I had written myself). This week I'll be starting Rails-after I complete the CodeSchool Rails courses I'll go on to their RSpec course. Partly for the geek cred, partly out of curiosit...

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...

Rails Project-Blogger Problem

Ran into my first challenge with the blogger today. I was creating a new model for a Comment feature and I typed: $ bin/rails generate model Comment author_name:string body:text article s :references That single red letter cost me an hour of struggle (I mean 'learning'). The extra s created a database schema containing something called 'articles_id'.  Since id  is a property of a single article, the comment controller  couldn't find an id  for articles,  as it didn't exist.  I looked at the Comment model file and saw the extra s  almost immediately after trying to enter a comment. But then I couldn't reverse the error. Rolling back the migration ( rake db:rollback), had no effect on the schema.  Destroying the model ( rails destroy model Comment)  had no effect on the schema. In the end I replaced the my local files with the Github repo contents (luckily I had just done a push). git fetch --all git reset --hard or...