From: Robert Klemme Date: 2012-01-26T00:05:49+09:00 Subject: Re: A sort of design question On Wed, Jan 25, 2012 at 3:53 PM, Panagiotis Atmatzidis wrote: > Dear Ladies (??) and Gentlemen (okay we have plenty of those), > > I wrote a script which needs a variable in order to proceed. This variable is defined by the user otherwise the default location is chosen. After a discussion I had on IRC (#ruby @ Freenode), I came up with a few questions. > > My script is structured like this: > > class [...] > optparse [...] > execution_flow_control [...] > > I do the 'control' of this variable inside the class like this: > > class Prog >  attr_accessor :database >  def initialize(database) >   @database = database >  end > > [...] >        def create_table(tableid) >                if @database and File.exists?(@database) <--- control > [...] > > end > > I was wondering if this is the right way actually do this control since, if @database is not set, the flow will never reach that point, because the class takes 1 argument (which is @database) and if it's null, it will come up with an error. I do not understand what you mean by "the flow will never reach that point": From what you have shown the constructor will happily accept nil for parameter "database". And if someone after that invokes #create_table the code will reach just this point. Also, I am not 100% positive what you mean by "this control". > The script works fine, as is. I optimized it a bit, by removing and/or optimizing some functions. But I'm curious, where is the right place for doing this control which is necessary for execution? Considering what I see here and on github I'd say constructor of Prog should check argument "database" for valid values and raise an exception if nil is passed. Then, later on, the other methods can rely on this value being valid. That's basically what OO is all about: you define the valid state of an instance and maintain it throughout all methods. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/