Long conditional statements

C

Courtland Allen

[Note: parts of this message were removed to make it a legal post.]

Some parts of my code call for really long conditional statements of the
form:

if a or b or c or d or e or f

doSomething()

end


Is there a more aesthetically-pleasing way to accomplish this?
 
W

w_a_x_man

[Note:  parts of this message were removed to make it a legal post.]

Some parts of my code call for really long conditional statements of the
form:

if a or b or c or d or e or f

  doSomething()

end

Is there a more aesthetically-pleasing way to accomplish this?

x = 4 ; y = 9 ; z = -2
==>-2
def do_something
puts "Something done."
end
==>nil
if x.odd? or
y.even? or
z < 0
do_something
end
Something done.
==>nil

x.even? and y.odd? and puts "Done."
Done.
==>nil
 
R

Robert Klemme


Problem is that if a, b, c, d, e and f are expressions you would have to
evaluate them beforehand thus loosing short circuit evaluation:

if [a,b,c,d,e,f].any? {|x| x}
...
end

I don't even find that more aesthetically pleasing. I'd probably rather
refactor, e.g.

if complex_condition(x,y)
do_something
end

Courtland, can you demonstrate a real example of your complex
conditions? Maybe there are other ways to transform them and make them
less complex.

Cheers

robert
 
C

Courtland Allen

[Note: parts of this message were removed to make it a legal post.]

Courtland, can you demonstrate a real example of your complex conditions?
Maybe there are other ways to transform them and make them less complex.

Cheers

robert
It happens in a variety of different ways in my code, so the more techniques
you guys list, the better (I hadn't known about any?).

But here's one example anyway:

if somehash[:key1].blank? or somehash[:key2].blank?
or somehash[:key2].blank? or somehash[:key2].blank?

doSomething()

end
 
B

Brian Candler

Courtland Allen wrote in post #955781:
Some parts of my code call for really long conditional statements of the
form:

if a or b or c or d or e or f

doSomething()

end


Is there a more aesthetically-pleasing way to accomplish this?

You can split them after the operator and continue them on the next
line, without needing any explicit continuation character.

if a or
b or
c or
d or
e or
f
doSomething
end
 
R

Robert Klemme

On Wed, Oct 20, 2010 at 11:25 AM, Robert Klemme
It happens in a variety of different ways in my code, so the more techniques
you guys list, the better (I hadn't known about any?).

But here's one example anyway:

if somehash[:key1].blank? or somehash[:key2].blank?
or somehash[:key2].blank? or somehash[:key2].blank?

doSomething()

end

if [:key1, :key2].any? {|k| somehash[k].blank?}
do_something
end

Not much of an improvement though.

Cheers

robert
 
W

w_a_x_man

It happens in a variety of different ways in my code, so the more techniques
you guys list, the better (I hadn't known about any?).
But here's one example anyway:
if somehash[:key1].blank? or somehash[:key2].blank?
or somehash[:key2].blank? or somehash[:key2].blank?
   doSomething()

if [:key1, :key2].any? {|k| somehash[k].blank?}
   do_something
end

Not much of an improvement though.

if somehash.values_at( :key1, :key2 ).index( "" )
do_something
end
 
S

Shadowfirebird

Call me odd, but I quite like:

def something()
...
end

something() if a
something() if b
something() if c
something() if d

Not to everyone's taste, I know...
 
R

Robert Klemme

Call me odd, but I quite like:

=A0 =A0def something()
=A0 =A0 =A0 =A0...
=A0 =A0end

=A0 =A0something() if a
=A0 =A0something() if b
=A0 =A0something() if c
=A0 =A0something() if d

Not to everyone's taste, I know...

That's not the same as "something if a || b || c || d"!

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
S

Shadowfirebird

Excerpts from Robert Klemme's message of Wed Oct 20 21:44:25 +0100 2010:
That's not the same as "something if a || b || c || d"!

Arse. No, of course not. Sorry, long day.

I don't think I can beat

something() if (a || b || c || d)

The important thing, I think, is to factor out the conditional code into something simple to make the whole statement more readable. But of course if I had

something() if (a || (b && c || a && c))

I might resort to:

complextest = (a || (b && c || a && c)

something() if complextest

Or I might even make complextest a function.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top