super simple language enhancement

A

Alan Eden

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?
 
M

Mohit Sindhwani

Alan said:
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.
 
A

Alan Eden

Mohit said:
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?
 
J

Jari Williamsson

Alan said:
| | @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
 
A

Alan Eden

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!
 
A

Austin Ziegler

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
 
T

Trans

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.
 
A

Alan Eden

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!
 
T

Trans

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.
 
A

ara.t.howard

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://codeforpeople.com/
 
R

Robert Dober

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://codeforpeople.com/
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,274
Messages
2,571,370
Members
48,062
Latest member
leehaan

Latest Threads

Top