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

(Nikolai Weibull) #1

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?,
        nikolai

···

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

(daz) #2

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?,
        nikolai

<something> while <test>

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

daz

(Doug Kearns) #3

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

Regards,
Doug

···

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

(David Vallner) #4

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?,
       nikolai

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

  while fred is barney
    frobnicate(womble, fluff)
  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)

(Christian Neukirchen) #5

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

  while predicate?
    code
  end

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

  while predicate?; code; end

···

        nikolai

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org

(Nikolai Weibull) #6

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,
        nikolai

···

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

(Nikolai Weibull) #7

Christian Neukirchen wrote:

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

I generally use

  while predicate?
    code
  end

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

  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,
        nikolai

···

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

(Christian Neukirchen) #8

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

  while foo; bar; baz; quux; end

than to write

  while foo: bar; baz; quux; end

because, after all, this is not allowed:

  while foo: bar: baz: quux: end

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

···

        nikolai

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org

(Nikolai Weibull) #9

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

  while foo; bar; baz; quux; end

than to write

  while foo: bar; baz; quux; end

because, after all, this is not allowed:

  while foo: bar: baz: quux: end

Yes, but you can write it as

  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,
        nikolai

···

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

(Daniel Brockman) #10

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

  while foo; bar; baz; quux; end

[...]

Yes, but you can write it as

  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:

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

   def delta: last - first end

   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:

   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>