On May 27, 2009, at 9:35 AM, J Haas wrote:
worked fine.)
Ok, but now you’re beginning to see the edge cases that make this sort
of transformation difficult on a global level. Specifically, how would
you differentiate between these two cases using Pythonic indentation:
cat conditional_case.rb
result = case ARGV[0]
when /^\d*$/
“That’s an integer”
when /^[A-Za-z]*$/
“That looks like a word…”
else
“That’s not an integer or a word”
end if ARGV[0]
puts result
cat case_and_conditional.rb
result = case ARGV[0]
when /^\d*$/
“That’s an integer”
when /^[A-Za-z]*$/
“That looks like a word…”
else
“That’s not an integer or a word”
end
if ARGV[0]
puts result
end
Attaching a conditional to the end of a case statement (or any block,
for that matter) is possible in Ruby because the block is just an
expression, and the statement doesn’t end until the newline. With your
preprocessor, adding "end " when the indentation level decreases would
yield the first form, but then how do I get the second form? I suppose
I could both decrease the indentation level and leave a blank space,
but then it seems like we’re adding an awful lot of formatting rules
just to have the code look nice and still function. On top of that, I
think this makes things much less clear! Consider that in “Pythonic
form” the above cases would become:
cat pythonic_conditional_case.rb
result = case ARGV[0]:
when /^\d*$/
“That’s an integer”
when /^[A-Za-z]*$/
“That looks like a word…”
else
“That’s not an integer or a word”
if ARGV[0]
puts result
cat pythonic_case_and_conditional.rb
result = case ARGV[0]:
when /^\d*$/
“That’s an integer”
when /^[A-Za-z]*$/
“That looks like a word…”
else
“That’s not an integer or a word”
if ARGV[0]:
puts result
Looking at those two pieces of code, is it clear that they do two
different things? At the very least, I think that this sort of change
to the syntax would have to be optional. This would, of course, mean
that you’re not getting rid of all of the "end"s in code, so then
what’s the point (other than to confuse matters of syntax even more)?
Let me also provide a counter-point. I’m currently using MacVim with
the ruby-vim files and a coloring scheme which colors “def…end”
pairs in yellow and other “case/if/do/etc…end” pairs in purple. This
gives me a good way to quickly look at some code, and if I see that
the “end” immediately preceding a “def” is purple, then I know I’ve
forgotten and “end” somewhere in that function definition.
Finally, let me suggest a bit of a “wisdom of crowds” argument: if the
advantages to significant whitespace were so unambiguously clear as
you’d have us believe, then why do most languages not use it (even
those developed before many of the advantages of advanced text editors/
IDEs existed)?