Hi Adam,
  The point of the syntax is that an objects accessors (the superset of
variables and methods) are interchangeable.  The implementation internally
can change without effecting the code that calls those accessors.  An good
example would be a TimeEntry class which keeps track of the start_time,
end_time and hours worked on a task.  Say in the original code that
start_time end_time and hours are all database entries originally (so they
are variables for the TimeEntry objects) but it becomes clear that keeping
both end_time and hours is redundant and that they can get out of sync which
causes bugs.  You could easily change end_time to be start_time + hours and
be much better off.  End_time is now a method instead of a variable, but the
code using it doesn't need to know that.

  When it comes to local variables and local methods the same concept
applies.  If you find yourself confused about this is which then you can add
self. in front of the method calls to differentiate them.  I don't add the
() because I may find that I don't really need a method and a variable will
do.  I usually don't call self. either, unless there is real potential for
confusion.  If you are calling variables the same names as methods, then
your doomed to lack clarity.  One alternative is to do cached method calls.
They work like this:

def stuff
  @stuff ||= code that figures out stuff's value
end

--OR--

def stuff(clear=false)
  if clear || [EMAIL PROTECTED]
    @stuff = code that figures out stuff's value
  end
  return @stuff
end

  This provents stuff from being recalculated every time it is called, but
you can change it when you want to.  The _ prefix doesn't seem like a winner
to me.  _ is valid at the start of the method names just as much as variable
names, so your relying solely on someone being 'in the know'.  I really
appreciate your post.  It is great to talk over these kinds of semantic and
syntactic 'nitty-gritty' here.

Happy Friday,
Rob

On Thu, Apr 17, 2008 at 2:03 PM, Adam Grant <[EMAIL PROTECTED]> wrote:

> Hey Folks,
>
> I've been noticing more and more while looking through other people's code
> that differentiating between local variables and method calls can be quite a
> laborious undertaking in Ruby. Plus it hurts my head after a while of
> reading through gems or plugins, especially on long and complicated methods
> (which you shouldn't have anyway).
>
> The special thing about Ruby is that you don't HAVE to do all the syntax
> stuff of other languages, like declare variables and type cast.
>
> I think it is still a good practice, for readability's sake, and I wanted
> to ask you all:
> *
> What do you use to differentiate local vars from methods? *
>
> Do you use the method_name() syntax for functions that don't take any
> arguments, or do you throw in an _ at the beginning of
> _local_variable_names?
>
> I tried the _ for a bit, but realized REAL quick that it made my code
> completely unsightly! I threw the _ on the end, but thats just confusing.
> Example =>  your_special_item_.save!
>
> Or do you just roll with the undifferentiated forms?
>
> I'm curious...
>
> - Adam
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby
-~----------~----~----~----~------~----~------~--~---

Reply via email to