[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

super simple language enhancement

Alan Eden

12/30/2007 4:08:00 PM

I don't know if this has ever been done in any language.

Any vertical line character(s) ( | ) which precede anything else on any
line should be treated as whitespace. This makes possible the following:

# Say hi to everybody

def say_hi
| if @names.nil?
| | puts "..."
| elsif @names.respond_to?("each")
| | # @names is a list of some kind, iterate!
| | @names.each do |name|
| | | puts "Hello #{name}!"
| | end
| else
| | puts "Hello #{@names}!"
| end
end

I copied this example from Ruby in twenty minutes, page 4. Look at the
original and look at the above. Isn't it much clearer? Or is there a
good reason why this isn't done that I'm missing?
--
Posted via http://www.ruby-....

10 Answers

Mohit Sindhwani

12/30/2007 4:14:00 PM

0

Alan Eden wrote:
> I don't know if this has ever been done in any language.
>
> Any vertical line character(s) ( | ) which precede anything else on any
> line should be treated as whitespace. This makes possible the following:
>
> # Say hi to everybody
>
> def say_hi
> | if @names.nil?
> | | puts "..."
> | elsif @names.respond_to?("each")
> | | # @names is a list of some kind, iterate!
> | | @names.each do |name|
> | | | puts "Hello #{name}!"
> | | end
> | else
> | | puts "Hello #{@names}!"
> | end
> end
>
> I copied this example from Ruby in twenty minutes, page 4. Look at the
> original and look at the above. Isn't it much clearer? Or is there a
> good reason why this isn't done that I'm missing?
>

I know this isn't what you're asking, but many code editors do that
automatically.

Cheers,
Mohit.
12/31/2007 | 12:14 AM.



Alan Eden

12/30/2007 4:26:00 PM

0

Mohit Sindhwani wrote:
> Alan Eden wrote:
>> | elsif @names.respond_to?("each")
>> original and look at the above. Isn't it much clearer? Or is there a
>> good reason why this isn't done that I'm missing?
>>
>
> I know this isn't what you're asking, but many code editors do that
> automatically.
>
> Cheers,
> Mohit.
> 12/31/2007 | 12:14 AM.

Wow! Thanks for the quick reply!

Of course those lines don't show up on the printed page or computer
screen or simpler editors. Why not make it a part of every language?
--
Posted via http://www.ruby-....

Jari Williamsson

12/30/2007 6:09:00 PM

0

Alan Eden wrote:
> | | @names.each do |name|
> | | | puts "Hello #{name}!"
> | | end

Rewriting like this would then result in a syntax error, following your
suggestion:
@names.each do
|name| puts "Hello #{name}!"
end

And what if tabs and whitespaces are mixed in the code and different tab
settings are used on different machines? This produces hard-to-read code
even today, and your suggestion would make the code unreadable.

> I copied this example from Ruby in twenty minutes, page 4. Look at the
> original and look at the above. Isn't it much clearer?

No, I think it clutters the code, I want Ruby code to be clean.

> Or is there a
> good reason why this isn't done that I'm missing?

Apart from breaking existing code and editors, this doesn't add anything
to the Ruby language. It's in the same league as syntax highlighting. If
you need good formatting, use text a source code formatter or (more
fun!) build one yourself.


Best regards,

Jari Williamsson

Alan Eden

12/30/2007 7:09:00 PM

0

Thanks for the reply!

You're right that code rewritten like your example would lead to a
syntax error, but this unusual case is easily avoided.

My intention is to make the code more readable on the printed page,
simple word processor or anywhere other than a dedicated Ruby
development environment.

It's obviously a matter of taste, but I think the eye can use visual
clues to "parse" the code into it's nested parts. You can tell at a
glance which "class", "def", "if","elsif","else" , etc etc goes with
which "end", which block ( of contiguous code, not necessarily a "Ruby"
block )of code is nested in what other block, and where, that every
block is paired with one "end", and that there are no extra
"ends"s,"else"s etc etc. This leaves at least a little more brain power
to process the meaning of each easily picked out block, and how it
relates to its containing blocks.

Without these lines, the code on the page might be cleaner, but the code
in your brain is messier, because it's often ( actually almost always )
difficult to tell the above things.

As far as mixing tabs and spaces, this is of course already a problem.
But all that's needed is for the code writer to use the same combination
of spaces and tabs up and down between each "vertical line". To make
anything work, common sense must be used, and reasonable conventions
must be adopted.

The only case where existing code would be broken would be in the rare
case such as you gave above. It would be trivial to write a program
which would automatically alter any code slightly to append any line
beginning with a vertical line to the previous line, admittedly a pain,
but there are always pros and cons to every suggestion which need to be
weighed and it needs to be decided which outweighs which. I think the
insights into the code gained visually at a glance outweigh these
inconveniences.

That's my opinion anyway.

Thanks again for responding!


Jari Williamsson wrote:
> Alan Eden wrote:
>> | | @names.each do |name|
>> | | | puts "Hello #{name}!"
>> | | end
>
> Rewriting like this would then result in a syntax error, following your
> suggestion:
> @names.each do
> |name| puts "Hello #{name}!"
> end
>
> And what if tabs and whitespaces are mixed in the code and different tab
> settings are used on different machines? This produces hard-to-read code
> even today, and your suggestion would make the code unreadable.
>
>> I copied this example from Ruby in twenty minutes, page 4. Look at the
>> original and look at the above. Isn't it much clearer?
>
> No, I think it clutters the code, I want Ruby code to be clean.
>
>> Or is there a
>> good reason why this isn't done that I'm missing?
>
> Apart from breaking existing code and editors, this doesn't add anything
> to the Ruby language. It's in the same league as syntax highlighting. If
> you need good formatting, use text a source code formatter or (more
> fun!) build one yourself.
>
>
> Best regards,
>
> Jari Williamsson

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

Austin Ziegler

12/30/2007 8:33:00 PM

0

On Dec 30, 2007 11:07 AM, Alan Eden <aopcmmpjhody@spammotel.com> wrote:
> I don't know if this has ever been done in any language.

Because it's a bad idea whose time has never come.

It doesn't add to the readability except in extremely limited cases,
and it can actively cause problems with how certain expressions could
be formed.

-austin
--
Austin Ziegler * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca

Trans

12/30/2007 9:50:00 PM

0



On Dec 30, 11:07 am, Alan Eden <aopcmmpjh...@spammotel.com> wrote:
> I don't know if this has ever been done in any language.
>
> Any vertical line character(s) ( | ) which precede anything else on any
> line should be treated as whitespace. This makes possible the following:
>
> # Say hi to everybody
>
> def say_hi
> | if @names.nil?
> | | puts "..."
> | elsif @names.respond_to?("each")
> | | # @names is a list of some kind, iterate!
> | | @names.each do |name|
> | | | puts "Hello #{name}!"
> | | end
> | else
> | | puts "Hello #{@names}!"
> | end
> end

I'm not in any way, shape or form, recommending it, but if you are so
inclined, you can do this:

def say_hi
; if @names.nil?
; ; puts "..."
; elsif @names.respond_to?("each")
; ; # @names is a list of some kind, iterate!
; ; @names.each do |name|
; ; ; puts "Hello #{name}!"
; ; end
; else
; ; puts "Hello #{@names}!"
; end
end

T.

Alan Eden

12/30/2007 11:22:00 PM

0

Very clever. However, I already have a "pre-processor" that removes the
vertical lines before compilation.

There doesn't seem to be much enthusiasm for my idea. All's I can say is
that formatting code this way increases my comprehension 1000%.
Otherwise I spend too much time and mental energy trying to "see" the
structure of the code.

But I guess everybody is surprised when other people see things
differently from themselves. Oh well!

Trans wrote:
> On Dec 30, 11:07 am, Alan Eden <aopcmmpjh...@spammotel.com> wrote:
>> | elsif @names.respond_to?("each")
>> | | # @names is a list of some kind, iterate!
>> | | @names.each do |name|
>> | | | puts "Hello #{name}!"
>> | | end
>> | else
>> | | puts "Hello #{@names}!"
>> | end
>> end
>
> I'm not in any way, shape or form, recommending it, but if you are so
> inclined, you can do this:
>
> def say_hi
> ; if @names.nil?
> ; ; puts "..."
> ; elsif @names.respond_to?("each")
> ; ; # @names is a list of some kind, iterate!
> ; ; @names.each do |name|
> ; ; ; puts "Hello #{name}!"
> ; ; end
> ; else
> ; ; puts "Hello #{@names}!"
> ; end
> end
>
> T.

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

Trans

12/30/2007 11:39:00 PM

0



On Dec 30, 6:22 pm, Alan Eden <aopcmmpjh...@spammotel.com> wrote:
> Very clever. However, I already have a "pre-processor" that removes the
> vertical lines before compilation.
>
> There doesn't seem to be much enthusiasm for my idea. All's I can say is
> that formatting code this way increases my comprehension 1000%.
> Otherwise I spend too much time and mental energy trying to "see" the
> structure of the code.
>
> But I guess everybody is surprised when other people see things
> differently from themselves. Oh well!

I can understand. But sometime it just means you should practice the
eyes. I guarantee you will abandon your "pre-processor" in short
order.

T.

ara.t.howard

12/31/2007 3:23:00 AM

0


On Dec 30, 2007, at 9:07 AM, Alan Eden wrote:

> def say_hi
> | if @names.nil?
> | | puts "..."
> | elsif @names.respond_to?("each")
> | | # @names is a list of some kind, iterate!
> | | @names.each do |name|
> | | | puts "Hello #{name}!"
> | | end
> | else
> | | puts "Hello #{@names}!"
> | end
> end
>

> Or is there a
> good reason why this isn't done that I'm missing?


def say_hi
puts( @names ? [*@names].map{|nm| "Hello #{ nm }!"} : "..." )
end


that, and vim can do this for you already.

a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




Robert Dober

12/31/2007 5:24:00 PM

0

On Dec 31, 2007 4:23 AM, ara.t.howard <ara.t.howard@gmail.com> wrote:
>
> On Dec 30, 2007, at 9:07 AM, Alan Eden wrote:
>
> > def say_hi
> > | if @names.nil?
> > | | puts "..."
> > | elsif @names.respond_to?("each")
> > | | # @names is a list of some kind, iterate!
> > | | @names.each do |name|
> > | | | puts "Hello #{name}!"
> > | | end
> > | else
> > | | puts "Hello #{@names}!"
> > | end
> > end
> >
>
> > Or is there a
> > good reason why this isn't done that I'm missing?
>
>
> def say_hi
> puts( @names ? [*@names].map{|nm| "Hello #{ nm }!"} : "..." )
> end
>
>
> that, and vim can do this for you already.
>
> a @ http://codeforp...
> --
> we can deny everything, except that we have the possibility of being
> better. simply reflect on that.
> h.h. the 14th dalai lama
>
I woul like to add that if you need that kind of gizmo you might
better refactor the code for readability. When it come to code written
by others use the tools that have been mentioned.
But putting this "feature" into the languages would just encourage bad
programming style IMNSHO.
BTW Happy New Year to All of You.
Robert
>
>
>
>



--

http://ruby-smalltalk.blo...

---
All truth passes through three stages. First, it is ridiculed. Second,
it is violently opposed. Third, it is accepted as being self-evident.
Schopenhauer (attr.)