class …
def …
raise “Directory #{dir} already exists.” if FileTest.directory?(dir)
end
end
Then I see it is too long to catch in just one glimpse, and one has to
read further to find the condition that actually triggers the
raising. So I rewrite it:
if FileTest.directory?(dir) then raise "Directory #{dir} already exists." end
The condition stands, but the line is even longer. So…
if FileTest.directory?(dir)
raise "Directory #{dir} already exists."
end
And I’ve taken three lines in a method.
It happens sometimes that I have the three styles play ping-pong among
each other, and I change from one to another over various sessions
depending on what I had for lunch and other very scientific
considerations.
I was wondering if a little syntactic sugar would help, something
that is readable, makes the condition stand, can be read in one
glimpse, and lies on a single line.
I was wondering if a little syntactic sugar would help, something
that is readable, makes the condition stand, can be read in one
glimpse, and lies on a single line.
Ruby isn’t trying to recreate English here. Try to think more in terms
of logical shortcutting so you naturally see that the raise doesn’t
get evaluated if the first expression evaluates to false.
This is far more transparent than making another method and passing
blocks to the evaluated value, not to mention being less to type and
faster
Subvocalisation is handy occasionally, but it’s not particularly a habit
you want to get into; it’s just one more layer your reading of the code
has to go through for you to understand it, and it doesn’t always map
that well to actual code
On Wed, Jul 31, 2002 at 01:53:37AM +0900, ts wrote:
FileTest.directory?(dir) and raise "Directory #{dir} already
exits"
My mind has trouble parsing it, though; it goes `well, this and that
what?’
Ruby isn’t trying to recreate English here. Try to think more in terms
of logical shortcutting so you naturally see that the raise doesn’t
get evaluated if the first expression evaluates to false.
We do have logical shortcutting somewhat of this kind in English:
One step closer and you’re dead.
So it’s sort of like:
I find out you’re a directory and there’ll be trouble.
I can think in logical operations, it’s when they blend too well with
a natural looking context that I have troubles. Paradoxically, `&&’
in shell scripts looks more natural to me, because there is no other
usual meaning associated to it and thus no source of confusion.
Massimiliano
···
On Fri, Aug 02, 2002 at 01:51:37AM +0900, Thomas Hurst wrote:
FileTest.directory?(dir) and raise "Directory #{dir} already
My mind has trouble parsing it, though; it goes `well, this and that
what?’
Ruby isn’t trying to recreate English here. Try to think more in terms
of logical shortcutting so you naturally see that the raise doesn’t
get evaluated if the first expression evaluates to false.