# Aesthetics of while ... do ... end versus while ...: ... end

**URL:** <https://rubytalk.org/t/aesthetics-of-while-do-end-versus-while-end/20482>\
**Category:** ruby-talk\
**Created:** [29 August 2005 23:52 UTC](https://rubytalk.org/t/aesthetics-of-while-do-end-versus-while-end/20482 "2005-08-29T23:52:12Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nikolai\_Weibull](https://avatars.discourse-cdn.com/v4/letter/n/ce7236/32.png) [@Nikolai\_Weibull](https://rubytalk.org/u/Nikolai_Weibull)\
**Post date:** [29 August 2005 23:52 UTC](https://rubytalk.org/t/aesthetics-of-while-do-end-versus-while-end/20482/1 "2005-08-29T23:52:12Z")

</div>

OK, this is really quite lame, but what do you people prefer:

while \<test\> do \<something\> end

or

while \<test\>: \<something\> end

?

The latter is shorter, but the puncutation sometimes meshes badly with  
the test. Any insights?,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nikolai

> **···**
>
> --  
> Nikolai Weibull: now available free of charge at [http://bitwi.se/](http://bitwi.se/)!  
> Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.  
> main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}

---

<div class="post-metadata">

**Author:** ![daz](https://avatars.discourse-cdn.com/v4/letter/d/87869e/32.png) [@daz](https://rubytalk.org/u/daz)\
**Post date:** [30 August 2005 04:11 UTC](https://rubytalk.org/t/aesthetics-of-while-do-end-versus-while-end/20482/2 "2005-08-30T04:11:23Z")

</div>

Nikolai Weibull wrote:

> OK, this is really quite lame, but what do you people prefer:
> 
> while \<test\> do \<something\> end
> 
> or
> 
> while \<test\>: \<something\> end
> 
> ?
> 
> The latter is shorter, but the puncutation sometimes meshes badly with  
> the test. Any insights?,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nikolai

\<something\> while \<test\>

x = 3; p x -= 1 while x \> 0

daz

---

<div class="post-metadata">

**Author:** ![Doug\_Kearns1](https://avatars.discourse-cdn.com/v4/letter/d/e9a140/32.png) [@Doug\_Kearns1](https://rubytalk.org/u/Doug_Kearns1)\
**Post date:** [30 August 2005 11:23 UTC](https://rubytalk.org/t/aesthetics-of-while-do-end-versus-while-end/20482/3 "2005-08-30T11:23:42Z")

</div>

> OK, this is really quite lame, but what do you people prefer:
> 
> while \<test\> do \<something\> end

I always use 'do' with 'while', 'until' and 'for' - probably the result  
of too much shell programming.

> or
> 
> while \<test\>: \<something\> end

I don't think I've ever actually seen this used in anger and a quick  
search of lib/ didn't seem to turn up any.

> ?
> 
> The latter is shorter, but the puncutation sometimes meshes badly with  
> the test. Any insights?,

Not really. 😉

Regards,  
Doug

> **···**
>
> On Tue, Aug 30, 2005 at 08:52:12AM +0900, Nikolai Weibull wrote:

---

<div class="post-metadata">

**Author:** ![David\_Vallner](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@David\_Vallner](https://rubytalk.org/u/David_Vallner)\
**Post date:** [30 August 2005 13:43 UTC](https://rubytalk.org/t/aesthetics-of-while-do-end-versus-while-end/20482/4 "2005-08-30T13:43:28Z")

</div>

Nikolai Weibull wrote:

> OK, this is really quite lame, but what do you people prefer:
> 
> while \<test\> do \<something\> end
> 
> or
> 
> while \<test\>: \<something\> end
> 
> ?
> 
> The latter is shorter, but the puncutation sometimes meshes badly with  
> the test. Any insights?,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nikolai

I could've sworn you could ommit the "do", as in:

&nbsp;&nbsp;while fred is barney  
&nbsp;&nbsp;&nbsp;&nbsp;frobnicate(womble, fluff)  
&nbsp;&nbsp;end

if you do yourself some good and resist the urge to golf the loop on a single line.

David Vallner  
(hate not having Ruby and an internet connection on the same computer)

---

<div class="post-metadata">

**Author:** ![Christian\_Neukirche1](https://avatars.discourse-cdn.com/v4/letter/c/ee7513/32.png) [@Christian\_Neukirche1](https://rubytalk.org/u/Christian_Neukirche1)\
**Post date:** [30 August 2005 14:46 UTC](https://rubytalk.org/t/aesthetics-of-while-do-end-versus-while-end/20482/5 "2005-08-30T14:46:11Z")

</div>

Nikolai Weibull \<mailing-lists.ruby-talk@rawuncut.elitemail.org\> writes:

> OK, this is really quite lame, but what do you people prefer:
> 
> while \<test\> do \<something\> end
> 
> or
> 
> while \<test\>: \<something\> end
> 
> ?
> 
> The latter is shorter, but the puncutation sometimes meshes badly with  
> the test. Any insights?,

I generally use

&nbsp;&nbsp;while predicate?  
&nbsp;&nbsp;&nbsp;&nbsp;code  
&nbsp;&nbsp;end

or, if I want it to be on a single line (can't remember doing that for  
while-loops):

&nbsp;&nbsp;while predicate?; code; end

> **···**
>
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nikolai
> 
> --  
> Christian Neukirchen \<chneukirchen@gmail.com\> [http://chneukirchen.org](http://chneukirchen.org)

---

<div class="post-metadata">

**Author:** ![Nikolai\_Weibull](https://avatars.discourse-cdn.com/v4/letter/n/ce7236/32.png) [@Nikolai\_Weibull](https://rubytalk.org/u/Nikolai_Weibull)\
**Post date:** [30 August 2005 11:07 UTC](https://rubytalk.org/t/aesthetics-of-while-do-end-versus-while-end/20482/6 "2005-08-30T11:07:39Z")

</div>

daz wrote:

> \<something\> while \<test\>
> 
> x = 3; p x -= 1 while x \> 0

Yeah, that's what I'd use, but the problem is that x is first defined in  
the test, so I have to use one of the two forms listed above,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nikolai

> **···**
>
> --  
> Nikolai Weibull: now available free of charge at [http://bitwi.se/\](http://bitwi.se/%5C)!  
> Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.  
> main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}

---

<div class="post-metadata">

**Author:** ![Nikolai\_Weibull](https://avatars.discourse-cdn.com/v4/letter/n/ce7236/32.png) [@Nikolai\_Weibull](https://rubytalk.org/u/Nikolai_Weibull)\
**Post date:** [30 August 2005 15:25 UTC](https://rubytalk.org/t/aesthetics-of-while-do-end-versus-while-end/20482/7 "2005-08-30T15:25:31Z")

</div>

Christian Neukirchen wrote:

> Nikolai Weibull \<mailing-lists.ruby-talk@rawuncut.elitemail.org\> writes:
> 
> I generally use
> 
> &nbsp;&nbsp;while predicate?  
> &nbsp;&nbsp;&nbsp;&nbsp;code  
> &nbsp;&nbsp;end
> 
> or, if I want it to be on a single line (can't remember doing that for  
> while-loops):
> 
> &nbsp;&nbsp;while predicate?; code; end

I'd rather use : or do than ; here. My question was with regards to  
putting the whole while on one line, yes. That's why I wrote my  
templates the way I did. I guess I should have been a bit more explicit  
about it, though, as two of you commented on this. I'd write one-liners  
as

\<something\> while \<test\>

but the problem is that \<something\> sometimes uses a variable defined in  
\<test\>, so this won't always pass initial checking,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nikolai

> **···**
>
> --  
> Nikolai Weibull: now available free of charge at [http://bitwi.se/\](http://bitwi.se/%5C)!  
> Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.  
> main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}

---

<div class="post-metadata">

**Author:** ![Christian\_Neukirche1](https://avatars.discourse-cdn.com/v4/letter/c/ee7513/32.png) [@Christian\_Neukirche1](https://rubytalk.org/u/Christian_Neukirche1)\
**Post date:** [30 August 2005 16:03 UTC](https://rubytalk.org/t/aesthetics-of-while-do-end-versus-while-end/20482/8 "2005-08-30T16:03:10Z")

</div>

Nikolai Weibull \<mailing-lists.ruby-talk@rawuncut.elitemail.org\> writes:

> Christian Neukirchen wrote:
> 
> > &nbsp;&nbsp;while predicate?; code; end
> 
> I'd rather use : or do than ; here. My question was with regards to  
> putting the whole while on one line, yes.

I disagree, and I don't see why everyone hates the semicolon.

I think it is more consistent to write

&nbsp;&nbsp;while foo; bar; baz; quux; end

than to write

&nbsp;&nbsp;while foo: bar; baz; quux; end

because, after all, this is not allowed:

&nbsp;&nbsp;while foo: bar: baz: quux: end

";" just means the same as a newline, and is meant to be used that  
way, YMMV.

> **···**
>
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nikolai
> 
> --  
> Christian Neukirchen \<chneukirchen@gmail.com\> [http://chneukirchen.org](http://chneukirchen.org)

---

<div class="post-metadata">

**Author:** ![Nikolai\_Weibull](https://avatars.discourse-cdn.com/v4/letter/n/ce7236/32.png) [@Nikolai\_Weibull](https://rubytalk.org/u/Nikolai_Weibull)\
**Post date:** [30 August 2005 21:09 UTC](https://rubytalk.org/t/aesthetics-of-while-do-end-versus-while-end/20482/9 "2005-08-30T21:09:53Z")

</div>

Christian Neukirchen wrote:

> Nikolai Weibull \<mailing-lists.ruby-talk@rawuncut.elitemail.org\> writes:

> \> Christian Neukirchen wrote:

> \> \> while predicate?; code; end

> \> I'd rather use : or do than ; here. My question was with regards to  
> \> putting the whole while on one line, yes.

> I disagree, and I don't see why everyone hates the semicolon.
> 
> I think it is more consistent to write
> 
> &nbsp;&nbsp;while foo; bar; baz; quux; end
> 
> than to write
> 
> &nbsp;&nbsp;while foo: bar; baz; quux; end
> 
> because, after all, this is not allowed:
> 
> &nbsp;&nbsp;while foo: bar: baz: quux: end

Yes, but you can write it as

&nbsp;&nbsp;while foo: bar; baz; quux end

Which, in my opinion, clearly separates the foo from the bar and the  
baz.

> ";" just means the same as a newline, and is meant to be used that  
> way, YMMV.

And so is : or do after a while. I have nothing against ; per se, but  
in this case I'd rather use : or do,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nikolai

> **···**
>
> --  
> Nikolai Weibull: now available free of charge at [http://bitwi.se/\](http://bitwi.se/%5C)!  
> Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.  
> main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}

---

<div class="post-metadata">

**Author:** ![Daniel\_Brockman](https://avatars.discourse-cdn.com/v4/letter/d/ed8c4c/32.png) [@Daniel\_Brockman](https://rubytalk.org/u/Daniel_Brockman)\
**Post date:** [31 August 2005 16:32 UTC](https://rubytalk.org/t/aesthetics-of-while-do-end-versus-while-end/20482/10 "2005-08-31T16:32:52Z")

</div>

Nikolai Weibull \<mailing-lists.ruby-talk@rawuncut.elitemail.org\> writes:

> > I disagree, and I don't see why everyone hates  
> > the semicolon.
> > 
> > I think it is more consistent to write
> > 
> > &nbsp;&nbsp;while foo; bar; baz; quux; end
> > 
> > [...]
> 
> Yes, but you can write it as
> 
> &nbsp;&nbsp;while foo: bar; baz; quux end
> 
> Which, in my opinion, clearly separates the foo from the  
> bar and the baz.

Since we're already discussing mitutiae, I'll chime in and  
say that I too prefer the colon here, simply because it  
makes for more readable code. I'd write it as follows:

&nbsp;&nbsp;&nbsp;while foo: bar ; baz ; quux end

I always put spaces around semicolons in Ruby code ---  
perhaps to reflect their rather symmetrical nature...

Actually, I'd probably use `do' rather than the colon.

I'd like to see `do' and `:' allowed after `def'.

&nbsp;&nbsp;&nbsp;def delta: last - first end

&nbsp;&nbsp;&nbsp;def request\_foo do send "NEED FOO" end

In re while modifiers, I have defined a high-precedence  
statement separator operator called `and then', which I  
occasionally use when writing two-statement while loops:

&nbsp;&nbsp;&nbsp;lather and then rinse while dirty?

Apart from the higher precedence, the semantics of `foo and  
then bar' is the same as `foo ; bar'. The preprocessor  
translates `foo and then bar' into `foo or true and bar'.

No punchline, just some random thoughts.

> **···**
>
> --  
> Daniel Brockman \<daniel@brockman.se\>
