Main Content

Thin application, thick objects - keep you main code simple. Example in Ruby

Archive - Originally posted on "The Horse's Mouth" - 2015-11-21 13:05:54 - Graham Ellis

A suggestion. Write thick objects and thin applications. That's what I did in Ruby towards the end of this week's Ruby Course.

Here's the complete application ...

  stations = Station.factory("rstats2014.xyz")
  stations.sort!
  for k in (0...20) do
    puts stations[k]
  end


Even if you don't know Ruby, you could probably tell me that my application sets up an Array of Station objects from data in a file called rstats2014.xyz, sorts those objects into a natural sorting order, and outputs the first 20. And, yes, you would be right!

The detailed internal stuff can be designed, implemeted, tested and debugged by someone who knows all about stations and the data associated with them, and can then be reused by lots of other people who - like the programmer of the code above - perhaps don't know about what's going on inside the class and indeed don't need to know.

For anyone interested ... ... the full application code we wrote is [here]. It turns out if we look inside that we have a lot of data available to us, and indeed who different classes of stations - those for which we have complete data sets and those which (for various reasons) are incomplete. We define how to sort stations, how to print stations out, how to make a station (or either type) from a raw data line read from our standard data file.