OCD, end statements, and vertical space

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 :slight_smile:

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.

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 :wink:

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

looks about 100x worse than

end

:slight_smile:

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… :wink: 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 :smiley:
(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?

On Dec 10, 2007, at 9:19 AM, Jay L. 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:

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

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

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.

On Dec 10, 2007, at 5:34 PM, [email protected] wrote:

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

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. :slight_smile:

On Dec 10, 5:39 pm, thefed [email protected] 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

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?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs