On Wed, Jan 25, 2012 at 3:53 PM, Panagiotis Atmatzidis
<ml / convalesco.org> wrote:
> Dear Ladies (??) and Gentlemen (okay we have plenty of those),
>
> I wrote a script which needs a variable in order to proceed. This variabl=
e 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
> =A0attr_accessor :database
> =A0def initialize(database)
> =A0 @database =3D database
> =A0end
>
> [...]
> =A0 =A0 =A0 =A0def create_table(tableid)
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0if @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 op=
timizing some functions. But I'm curious, where is the right place for doin=
g 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

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/