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

N

Nikolai Weibull

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
 
D

daz

Nikolai said:
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
 
N

Nikolai Weibull

daz said:
<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
 
D

Doug Kearns

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
 
D

David Vallner

Nikolai said:
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)
 
N

Nikolai Weibull

Christian said:
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
 
N

Nikolai Weibull

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
 
D

Daniel Brockman

Nikolai Weibull said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top