[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Why not adopt "Python Style" indentation for Ruby?

Chris Dew

5/18/2007 7:24:00 AM

As far as I can see, the 'end' keyword is 'repeating yourself' when
used with properly indented code.

class Foo

def bar(value)
if value < 7
puts "hello"
else
puts "world"
end
end

def foobar
puts "foobar"
end

end

could be

class Foo

def bar(value)
if value < 7
puts "hello"
else
puts "world"

def foobar
puts "foobar"

with no reduction in meaning, yet 25% fewer lines of code.


What are the reasons why this isn't used/implemented/liked? It would
be a small change to the interpreter. Enabling meaningful indentation
would only make 'end' optional, not invalid; backwards compatibility
wouldn't be a problem.

(I use both Ruby and Python. I think indentation is one of the few
*language* features where Python leads Ruby.)

If this post generates a positive response, I'll make a patch for Ruby
1.9.

29 Answers

Julian Leviston

5/18/2007 9:07:00 AM

0

Actually,

Isn't end repeating yourself full stop?

Do we really need it?

Julian.

On Fri, 18 May 2007 16:25:03 +0900
Chris Dew <cmsdew@googlemail.com> wrote:
> As far as I can see, the 'end' keyword is 'repeating yourself' when
> used with properly indented code.
>
> class Foo
>
> def bar(value)
> if value < 7
> puts "hello"
> else
> puts "world"
> end
> end
>
> def foobar
> puts "foobar"
> end
>
> end
>
> could be
>
> class Foo
>
> def bar(value)
> if value < 7
> puts "hello"
> else
> puts "world"
>
> def foobar
> puts "foobar"
>
> with no reduction in meaning, yet 25% fewer lines of code.
>
>
> What are the reasons why this isn't used/implemented/liked? It would
> be a small change to the interpreter. Enabling meaningful indentation
> would only make 'end' optional, not invalid; backwards compatibility
> wouldn't be a problem.
>
> (I use both Ruby and Python. I think indentation is one of the few
> *language* features where Python leads Ruby.)
>
> If this post generates a positive response, I'll make a patch for Ruby
> 1.9.
>
>


Daniel Martin

5/18/2007 1:01:00 PM

0

Chris Dew <cmsdew@googlemail.com> writes:

> As far as I can see, the 'end' keyword is 'repeating yourself' when
> used with properly indented code.

Note that the Haskell folks have managed to evolve a language in which
python-like spacing can be used to mark the extent of discrete code
chunks, but so can traditional braces.

This is more then kind of thing I'd like to see in Ruby: having it
allow the compact python notation, but not require it. (So that,
e.g., code generators would still be easy to write)

I think anyone serious about adding indentation-as-syntax to Ruby
should take a very close look at how Haskell manages to allow it
without requiring it.

--
s=%q( Daniel Martin -- martin@snowplow.org
puts "s=%q(#{s})",s.to_a.last )
puts "s=%q(#{s})",s.to_a.last

Ryan Leavengood

5/18/2007 2:04:00 PM

0

On 5/18/07, Chris Dew <cmsdew@googlemail.com> wrote:
>
> What are the reasons why this isn't used/implemented/liked? It would
> be a small change to the interpreter. Enabling meaningful indentation
> would only make 'end' optional, not invalid; backwards compatibility
> wouldn't be a problem.
>
> (I use both Ruby and Python. I think indentation is one of the few
> *language* features where Python leads Ruby.)

This is a very subjective thing. A lot of people don't use Python
because they don't like this feature.

In my case I like the symmetry that Ruby's matching end provides. I
find it makes the code easier to read and maintain. I've written and
maintained some Python code that "trails off into nowhere" with a long
series of indentations. I find this "stairway" code rather painful to
work with.

But if it is possible to add this feature to Ruby without breaking
anything, it could be interesting. It would certainly draw a few
Pythonists to check out Ruby.

Ryan

WoNáDo

5/18/2007 6:13:00 PM

0

Chris Dew wrote:
> As far as I can see, the 'end' keyword is 'repeating yourself' when
> used with properly indented code.
>
> class Foo
>
> def bar(value)
> if value < 7
> puts "hello"
> else
> puts "world"
> end
> end
>
> def foobar
> puts "foobar"
> end
>
> end
>
> could be
>
> class Foo
>
> def bar(value)
> if value < 7
> puts "hello"
> else
> puts "world"
>
> def foobar
> puts "foobar"

The is one big disadvantage in Python's syntax. Things like...

def bar(value); (value<7) ? (puts "hello") : (puts "world"); end

...are not possible in Python - and they are useful for the typical
one-to-three-liners in support areas, where Ruby still has the chance to
replace Perl - a chance that Python never had.

I know that my example has nothing to do with documentation oriented
software development, but this is not necessary for a broad range of
small ad-hoc tools.

Wolfgang Nádasi-Donner

--
Posted via http://www.ruby-....

Enrique Comba Riepenhausen

5/18/2007 6:20:00 PM

0

>
> with no reduction in meaning, yet 25% fewer lines of code.
>
>
> What are the reasons why this isn't used/implemented/liked? It would
> be a small change to the interpreter. Enabling meaningful indentation
> would only make 'end' optional, not invalid; backwards compatibility
> wouldn't be a problem.
>
> (I use both Ruby and Python. I think indentation is one of the few
> *language* features where Python leads Ruby.)

Here my 5 cents...

Gentlemen, if you want to code Python style, go ahead, program in
Python.

I mean every language has it's pros an cons. I come from a Java world
(11 years of Java) and I am not asking that Ruby should be Java like.
That is absolute nonsense.

Ruby is like Ruby, so, if you like it (as I do) take it, if you
don't... there are lots of different programming languages out there
you can choose from, or like Mats, go ahead and develop your own (you
will notice it's actually not that easy to do it right...)

Cheers,

Enrique Comba Riepenhausen

Brendan

5/19/2007 3:02:00 PM

0

On May 18, 4:23 am, Chris Dew <cms...@googlemail.com> wrote:
> ...
> with no reduction in meaning, yet 25% fewer lines of code.
>
> What are the reasons why this isn't used/implemented/liked? It would
> be a small change to the interpreter. Enabling meaningful indentation
> would only make 'end' optional, not invalid; backwards compatibility
> wouldn't be a problem.
>
> (I use both Ruby and Python. I think indentation is one of the few
> *language* features where Python leads Ruby.)

I personally prefer that 'end' be in place, so that all ruby code is
readable. I personally believe there may be something wrong with a
syntax which sacrifices explicit readability (for everyone, not just
python programmers) for increased typing speed and the illusory gain
of 'fewer LOC'. This was one of the major reasons which convinced me
to use ruby instead of python.


Chad Perrin

5/19/2007 6:03:00 PM

0

On Sun, May 20, 2007 at 12:05:06AM +0900, Brendan wrote:
> On May 18, 4:23 am, Chris Dew <cms...@googlemail.com> wrote:
> > ...
> > with no reduction in meaning, yet 25% fewer lines of code.
> >
> > What are the reasons why this isn't used/implemented/liked? It would
> > be a small change to the interpreter. Enabling meaningful indentation
> > would only make 'end' optional, not invalid; backwards compatibility
> > wouldn't be a problem.
> >
> > (I use both Ruby and Python. I think indentation is one of the few
> > *language* features where Python leads Ruby.)
>
> I personally prefer that 'end' be in place, so that all ruby code is
> readable. I personally believe there may be something wrong with a
> syntax which sacrifices explicit readability (for everyone, not just
> python programmers) for increased typing speed and the illusory gain
> of 'fewer LOC'. This was one of the major reasons which convinced me
> to use ruby instead of python.

I, personally, don't have an argument on principle like that for my own
preferences in this matter. I just like the fact that an "end" makes it
look more complete to me. Reading Python code always makes me feel like
my eyes are just going to trail off the right-hand edge of the page
because the "shape" of the code never brings me back to the leftmost
edge, the way code "should".

When I look at Python code, and ponder the way it does things so
differently from Ruby regarding delimiters and indentation (the same
thing in Python), it seems to me that Python was created for people who
write code in a particular mindset, and it's not a mindset I share when
I'm writing code. I guess maybe some people, when writing code, think
in footnote hierarchies, while others (like me) think in nested scopes.

That's just an off-the-cuff hypothesis.

--
CCD CopyWrite Chad Perrin [ http://ccd.ap... ]
Larry Wall: "A script is what you give the actors. A program is what you
give the audience."

M. Edward (Ed) Borasky

5/19/2007 6:48:00 PM

0

Chad Perrin wrote:
> On Sun, May 20, 2007 at 12:05:06AM +0900, Brendan wrote:
>> On May 18, 4:23 am, Chris Dew <cms...@googlemail.com> wrote:
>>> ...
>>> with no reduction in meaning, yet 25% fewer lines of code.
>>>
>>> What are the reasons why this isn't used/implemented/liked? It would
>>> be a small change to the interpreter. Enabling meaningful indentation
>>> would only make 'end' optional, not invalid; backwards compatibility
>>> wouldn't be a problem.
>>>
>>> (I use both Ruby and Python. I think indentation is one of the few
>>> *language* features where Python leads Ruby.)
>> I personally prefer that 'end' be in place, so that all ruby code is
>> readable. I personally believe there may be something wrong with a
>> syntax which sacrifices explicit readability (for everyone, not just
>> python programmers) for increased typing speed and the illusory gain
>> of 'fewer LOC'. This was one of the major reasons which convinced me
>> to use ruby instead of python.
>
> I, personally, don't have an argument on principle like that for my own
> preferences in this matter. I just like the fact that an "end" makes it
> look more complete to me. Reading Python code always makes me feel like
> my eyes are just going to trail off the right-hand edge of the page
> because the "shape" of the code never brings me back to the leftmost
> edge, the way code "should".
>
> When I look at Python code, and ponder the way it does things so
> differently from Ruby regarding delimiters and indentation (the same
> thing in Python), it seems to me that Python was created for people who
> write code in a particular mindset, and it's not a mindset I share when
> I'm writing code. I guess maybe some people, when writing code, think
> in footnote hierarchies, while others (like me) think in nested scopes.
>
> That's just an off-the-cuff hypothesis.
>
I've been watching this debate go by for some time, and I'm not sure
this sort of thing can ever be solved, but here are my personal opinions:

1. When you come right down to it, there are only a few basic syntax
styles that have survived the test of time. *Most* languages, including
C, Ruby, Pascal and Perl, use some variant of the Algol 60 syntax. The
other "survivors" are the Lisp/Scheme variant of prefix notation,
Forth's postfix notation, and the regular expression syntax. I suppose
assembly language syntax has also survived, and FORTRAN and COBOL, but I
don't think anyone would design a new language with those.

2. I think Python's "variant" of Algol 60 notation is less than
satisfactory, but I also have problems with the liberality present in
the syntax of Perl and Ruby. I don't like to make the reader or parser
work any harder than necessary just for the convenience of the coder. So
I like explicit delimiters like curly braces, parentheses, mandatory
semicolons at the end of statements, begin/end pairs, etc. for the Algol
60 languages. Lisp/Scheme and Forth were deliberately designed to make
the parser's job trivial, and I like them too. :)

3. I've pretty much given up on the dream that I'll be able to program
in one language only, although some languages are close enough. At the
moment, I can get just about everything done in either Perl or R, with
the other languages simply being fun to learn and use but not essential.
I could probably eliminate Perl if there were more R programmers, but
there aren't and won't be, because R is designed for scientists and
mathematicians, not business people.



Robert Klemme

5/20/2007 7:43:00 AM

0

On 19.05.2007 20:48, M. Edward (Ed) Borasky wrote:

> 2. I think Python's "variant" of Algol 60 notation is less than
> satisfactory, but I also have problems with the liberality present in
> the syntax of Perl and Ruby. I don't like to make the reader or parser
> work any harder than necessary just for the convenience of the coder.

I would understand why you would want to make the reader's job easier -
but the parser? That's just a piece of software and I believe that we
should always strive to make software easier to use for humans not machines.

Besides of that, personally I don't find the current Ruby syntax hard to
read. Do you have some examples where you feel that reading Ruby is
made hard(er) because of the liberties you mention?

Kind regards

robert

SonOfLilit

5/20/2007 8:11:00 AM

0

IMHO, at least in complicated cases, the reader has to emulate a
parer, so both are the same.

Whenever there is syntax that's tricky to parse, it's also tricky to read.

See pythons space-vs-tab problem, for example.


Aur

On 5/20/07, Robert Klemme <shortcutter@googlemail.com> wrote:
> On 19.05.2007 20:48, M. Edward (Ed) Borasky wrote:
>
> > 2. I think Python's "variant" of Algol 60 notation is less than
> > satisfactory, but I also have problems with the liberality present in
> > the syntax of Perl and Ruby. I don't like to make the reader or parser
> > work any harder than necessary just for the convenience of the coder.
>
> I would understand why you would want to make the reader's job easier -
> but the parser? That's just a piece of software and I believe that we
> should always strive to make software easier to use for humans not machines.
>
> Besides of that, personally I don't find the current Ruby syntax hard to
> read. Do you have some examples where you feel that reading Ruby is
> made hard(er) because of the liberties you mention?
>
> Kind regards
>
> robert
>
>
>