[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

OCD, end statements, and vertical space

Jay Levitt

12/10/2007 4:16:00 PM

I spent too many years in PL/I, where nested functions and subroutines look
like this:

outer: procedure (param1);
declare param1 char(4);

declare local char(1);

put skip;
put skip list ('hello outer');
put skip;

inner: procedure (param1, param2);
declare param1 char(4);
declare param2 char(4);

declare local char(4);

put skip;
put skip list ('hello inner');
put skip;

end inner;

end outer;

Indenting preferences aside, that made *vertical* spacing a no-brainer.
Put a blank line before the procedure declarations, another after the
parameters, another after the locals, and one each before and after the end
statements, and everything looks nice, at least to me. (This will actually
look really funny to Ed, who's probably used to much more segmented-style
declarations.)

But I haven't found a style in Ruby that makes me happy yet. It always
looks so unbalanced:

module Mod
class Klass
def one_method(param1, param2)

puts
puts "hello world"
puts

end

def two_method

puts "hello again"

end

end

class NotherKlass
def three_method
puts "hi already"
end
end

end


No matter how I space it, either the "end" statements look detached from
their class, so nesting is hard to follow, or the def looks detached, or
everything runs together, depending on how many lines are in the routine,
how long the method name is, how many methods/classes are in a row, etc.

I can't find a spacing rule that I like more than 50% of the time. Not 50%
of the *cases*, mind you - all the cases, just different solutions in
different moods. I'm pretty sure I spend 50% of my time deleting and
re-adding newlines on end statements :)

Has anyone (as obsessive as I am) found a simple, aesthetically pleasing
pattern to follow? I like programming to be art, but not Colorforms art.

--
Jay Levitt |
Boston, MA | My character doesn't like it when they
Faster: jay at jay dot fm | cry or shout or hit.
http://... | - Kristoffer
10 Answers

ara.t.howard

12/10/2007 8:06:00 PM

0


On Dec 10, 2007, at 9:19 AM, Jay Levitt wrote:

>
> No matter how I space it, either the "end" statements look detached
> from
> their class, so nesting is hard to follow, or the def looks
> detached, or
> everything runs together, depending on how many lines are in the
> routine,
> how long the method name is, how many methods/classes are in a row,
> etc.

couple comments:

- in vim, with ruby.vim and matchit, i can simply bounce on % to
match do/end, module/end, class/end, if/else/end, etc. this works on
keywords and, of course, "{}" braces. the result is that i hardly
care about balancing ends visually since it's so easy to let my
editor do it

- folding compresses vertical space and reduces the problem by at
least 80%. are you using a folding editor? here is what your code
looks like to me (with some formatting of course)

http://s3.amazonaws.com/drawohara.com.screenshots/vim....
49.png
http://s3.amazonaws.com/drawohara.com.screenshots/vim....
50.png
http://s3.amazonaws.com/drawohara.com.screenshots/vim....
51.png

as you can see, the problem kind of goes away quickly when you
simply eliminate the vertical space

>
> I can't find a spacing rule that I like more than 50% of the time.
> Not 50%
> of the *cases*, mind you - all the cases, just different solutions in
> different moods. I'm pretty sure I spend 50% of my time deleting and
> re-adding newlines on end statements :)
>
> Has anyone (as obsessive as I am) found a simple, aesthetically
> pleasing
> pattern to follow? I like programming to be art, but not
> Colorforms art.


why not 'space after the "end" except that last one:

module Mod
class Klass
def one_method(param1, param2)
puts
puts "hello world"
puts
end

def two_method
puts "hello again"
end
end

class NotherKlass
def three_method
puts "hi already"
end
end
end

??

a @ http://codeforp...
--
it is not enough to be compassionate. you must act.
h.h. the 14th dalai lama




furtive.clown

12/10/2007 10:31:00 PM

0

Remember you can reduce nesting if you wish,

Mod = Module.new # equivalent to module Mod ; end

class Mod::Klass
def one_method(param1, param2)
puts
puts "hello world"
puts
end

def two_method
puts "hello again"
end
end

class Mod::NotherKlass
def three_method
puts "hi already"
end
end

Speaking of OCD, I use 3-space indent because I like the non-
overlapping staircase look of the 'end's. :)

fedzor

12/10/2007 10:40:00 PM

0


On Dec 10, 2007, at 5:34 PM, furtive.clown@gmail.com wrote:

> Remember you can reduce nesting if you wish,
>
> Mod = Module.new # equivalent to module Mod ; end
>
> class Mod::Klass
> def one_method(param1, param2)
> puts
> puts "hello world"
> puts
> end
>
> def two_method
> puts "hello again"
> end
> end
>
> class Mod::NotherKlass
> def three_method
> puts "hi already"
> end
> end

Wow. I really really like that. Do you actually use that or are you
just proposing it?

-ari

Marc Heiler

12/11/2007 1:27:00 AM

0

> Has anyone (as obsessive as I am) found a simple, aesthetically pleasing
> pattern to follow?

I can understand you somewhat. I am one of the few here (obviously) who
does not find the "end"'s that visually appealing, at least compared to
omit them ;)

Although, I must actually say,
end inner;
end outer;

looks about 100x worse than

end

:)

> It always looks so unbalanced:

I think the comparison is not fair... you put all your classes inside
your module namespace, but ... what are your classes actually doing?!
Your first code example seems to do something, but your ruby code
seems to hardly have any content.

My classes normally have a LOT more methods, with several lines each
inside... ;) In this regard they do no longer feel disturbing at
all (plus they get the job done quickly and nicely too)
Don't know ... looks really strange.

Typically one writes a few classes in ruby that are adapted to your
task at hand. The best rule of thumb is to make them short, concise
and readable... if they become too big, it is probably time to think
of refactoring/rewriting the annoying parts.

Aside from this I only use 2 spaces indent. However in
certain occasions I do love to use {} as in

def run_all_tasks
loop {

}
end


I like that a lot, especially inside a method :D
(Being very concise is IMO not that easy... I often see
code of other people who really have few methods which
do all the code with one-liners like constructs. But i
think that is actually an asset, not a liability)

Just write a bit more ruby code?
--
Posted via http://www.ruby-....

furtive.clown

12/11/2007 3:22:00 AM

0

On Dec 10, 5:39 pm, thefed <fed...@gmail.com> wrote:
>
> Wow. I really really like that. Do you actually use that or are you
> just proposing it?

Yes, I use that idiom fairly often. This variant also has its uses:

Mod = toplevel = Module.new

class toplevel::Klass
def one_method(param1, param2)
puts
puts "hello world"
puts
end

def two_method
puts "hello again"
end
end

class toplevel::NotherKlass
def three_method
puts "hi already"
end
end

John W Kennedy

12/11/2007 6:20:00 PM

0

Jay Levitt wrote:
> I spent too many years in PL/I, where nested functions and subroutines look
> like this:
>
> outer: procedure (param1);
> declare param1 char(4);
>
> declare local char(1);
>
> put skip;
> put skip list ('hello outer');
> put skip;
>
> inner: procedure (param1, param2);
> declare param1 char(4);
> declare param2 char(4);
>
> declare local char(4);
>
> put skip;
> put skip list ('hello inner');
> put skip;
>
> end inner;
>
> end outer;

Or, if you were really cool:

0outer: procedure (param1);
declare param1 char(4);
0 declare local char(1);
0 put skip;
put skip list ('hello outer');
put skip;
0inner: procedure (param1, param2);
declare param1 char(4);
declare param2 char(4);
0 declare local char(4);
0 put skip;
put skip list ('hello inner');
put skip;
0end inner;
0end outer;

(...which would somewhat speed up your compiler listing on impact printers).
--
John W. Kennedy
"There are those who argue that everything breaks even in this old dump
of a world of ours. I suppose these ginks who argue that way hold that
because the rich man gets ice in the summer and the poor man gets it in
the winter things are breaking even for both. Maybe so, but I'll swear I
can't see it that way."
-- The last words of Bat Masterson

Jay Levitt

12/11/2007 7:07:00 PM

0

On Tue, 11 Dec 2007 13:20:10 -0500, John W. Kennedy wrote:

> (...which would somewhat speed up your compiler listing on impact printers).

OK, I'm intrigued - why do the zeros speed up printing?

The only thing I can remember about speed and impact printers is that the
bidirectional ones would usually optimize out the end of the line movements
but not always the start-of-line ones.. is that related?

--
Jay Levitt |
Boston, MA | My character doesn't like it when they
Faster: jay at jay dot fm | cry or shout or hit.
http://... | - Kristoffer

Jay Levitt

12/11/2007 7:11:00 PM

0

On Mon, 10 Dec 2007 15:05:36 -0500, ara.t.howard wrote:

> - in vim, with ruby.vim and matchit, i can simply bounce on % to
> match do/end, module/end, class/end, if/else/end, etc. this works on
> keywords and, of course, "{}" braces. the result is that i hardly
> care about balancing ends visually since it's so easy to let my
> editor do it
>
> - folding compresses vertical space and reduces the problem by at
> least 80%. are you using a folding editor? here is what your code
> looks like to me (with some formatting of course)

These are really interesting points. In PL/I I'm using a rudimentary
version of emacs, with no folding or syntax recognition/highlighting, so
I've gotten used to making whitespace do all the work. In fact, one of the
first things I tend to do with any modern editor is turn the folding off.
And I use whatever the default highlighting is.

Perhaps I need to learn to use my tools better.

--
Jay Levitt |
Boston, MA | My character doesn't like it when they
Faster: jay at jay dot fm | cry or shout or hit.
http://... | - Kristoffer

Paul Duca

1/3/2014 7:03:00 PM

0

On Friday, January 3, 2014 10:50:11 AM UTC-5, duke wrote:
> On Thu, 02 Jan 2014 06:43:03 -0800, linuxgal <linuxgal@cleanposts.com> wrote:
>
>
>
> >duke wrote:
>
> >> On Wed, 01 Jan 2014 20:50:39 -0600, Free Lunch<lunch@nofreelunch.us> wrote:
>
> >>
>
> >>> >On Wed, 01 Jan 2014 17:03:41 -0800, linuxgal<linuxgal@cleanposts.com>
>
> >>> >wrote in alt.atheism:
>
> >>> >
>
> >>>> >>Free Lunch wrote:
>
> >>>>> >>>When did England separate church and state?
>
> >>>> >>
>
> >>>> >>They separated the Church of England from the Papal States.
>
> >>> >
>
> >>> >Touch�.
>
> >> Haahaahaa. Dumber than I first thought.
>
>
>
> >Marjiuana is legal in Washington State now.
>
>
>
> >God is punishing us by moving the 777X widebody line to a Right to Work state
>
>
>
> Union labor is screwing itself. I took crumbs for the right to work--
>
> and LIKED it!
>
>
>
> duke, American-American
>

Paul Duca

1/5/2014 1:00:00 PM

0

On Saturday, January 4, 2014 8:40:57 AM UTC-5, duke wrote:
> On Fri, 03 Jan 2014 10:50:14 -0600, Free Lunch <lunch@nofreelunch.us> wrote:
>
>
>
> >On Fri, 03 Jan 2014 09:50:11 -0600, duke <duckgumbo32@cox.net> wrote in
>
> >alt.atheism:
>
> >
>
> >>On Thu, 02 Jan 2014 06:43:03 -0800, linuxgal <linuxgal@cleanposts.com> wrote:
>
> >>
>
> >>>duke wrote:
>
> >>>> On Wed, 01 Jan 2014 20:50:39 -0600, Free Lunch<lunch@nofreelunch.us> wrote:
>
> >>>>
>
> >>>>> >On Wed, 01 Jan 2014 17:03:41 -0800, linuxgal<linuxgal@cleanposts.com>
>
> >>>>> >wrote in alt.atheism:
>
> >>>>> >
>
> >>>>>> >>Free Lunch wrote:
>
> >>>>>>> >>>When did England separate church and state?
>
> >>>>>> >>
>
> >>>>>> >>They separated the Church of England from the Papal States.
>
> >>>>> >
>
> >>>>> >Touch�.
>
> >>>> Haahaahaa. Dumber than I first thought.
>
> >>
>
> >>>Marjiuana is legal in Washington State now.
>
> >>
>
> >>>God is punishing us by moving the 777X widebody line to a Right to Work state
>
> >>
>
> >>Union labor is screwing itself. Let's hear a cheer for Boeing.
>
> >
>
> >The rich, who have been working together to screw working people,
>
>
>
> What if that's what the working people want - to be screwed? After all, they
>
> get reimbursed in accordance with the level of their individual value to the
>
> owners. I knew I had very little value to those who owned MY soul from 9
>
> to 5, so I accepted the meager offering they gave me in return.
>
>
>
>
> duke, American-American
>