# OCD, end statements, and vertical space

**URL:** <https://www.ruby-forum.com/t/ocd-end-statements-and-vertical-space/122684>\
**Category:** Ruby\
**Created:** [December 10, 2007, 5:22pm UTC](https://www.ruby-forum.com/t/ocd-end-statements-and-vertical-space/122684 "2007-12-10T17:22:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [December 10, 2007, 5:22pm UTC](https://www.ruby-forum.com/t/ocd-end-statements-and-vertical-space/122684/1 "2007-12-10T17:22:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [December 11, 2007, 2:26am UTC](https://www.ruby-forum.com/t/ocd-end-statements-and-vertical-space/122684/2 "2007-12-11T02:26:48Z")

</div>

> 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 😃  
(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?

---

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [December 12, 2007, 6:36pm UTC](https://www.ruby-forum.com/t/ocd-end-statements-and-vertical-space/122684/3 "2007-12-12T18:36:04Z")

</div>

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:

- 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)

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/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [December 12, 2007, 7:09pm UTC](https://www.ruby-forum.com/t/ocd-end-statements-and-vertical-space/122684/4 "2007-12-12T19:09:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [December 12, 2007, 9:15pm UTC](https://www.ruby-forum.com/t/ocd-end-statements-and-vertical-space/122684/5 "2007-12-12T21:15:33Z")

</div>

On Dec 10, 2007, at 5:34 PM, removed\_email\_address@domain.invalid 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

---

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [December 12, 2007, 8:35pm UTC](https://www.ruby-forum.com/t/ocd-end-statements-and-vertical-space/122684/6 "2007-12-12T20:35:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [December 13, 2007, 1:05am UTC](https://www.ruby-forum.com/t/ocd-end-statements-and-vertical-space/122684/7 "2007-12-13T01:05:59Z")

</div>

On Dec 10, 5:39 pm, thefed [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![jay-levitt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jay-levitt](https://www.ruby-forum.com/u/jay-levitt)\
**Post date:** [December 13, 2007, 12:29pm UTC](https://www.ruby-forum.com/t/ocd-end-statements-and-vertical-space/122684/8 "2007-12-13T12:29:33Z")

</div>

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?
