55502f40dc8b7c769880b10874abc9d0

searching for an elegant solution. i think its a basic problem.
do you have an idea. i want to avoid an error flag variable.

cheers

error = false

if `whoami` != "ubuntu"
  puts "You aren't logged in as ubuntu"
  error = true
end

if `ps -ef | grep [s]elenium`.length == 0
  puts "selenium isn't start"
  error = true
end

puts "environment is ready" unless error

Refactorings

No refactoring yet !

B8ba61cc84ecb63c859435be28547dfb

steved

March 5, 2011, March 05, 2011 06:32, permalink

No rating. Login to rate!

(Untested)

def user_ubuntu?
  if `whoami` != "ubuntu"
    puts "You aren't logged in as ubuntu"
    false
  else
    true
  end
end

def selenium_started?
  if `ps -ef | grep [s]elenium`.length == 0
    puts "selenium isn't started"
    false
  else
    true
  end
end

puts "environment is ready" if user_ubuntu? && selenium_started?
F9a9ba6663645458aa8630157ed5e71e

Ants

March 5, 2011, March 05, 2011 09:13, permalink

No rating. Login to rate!

@steved: I actually had the same idea as you have, but that approach changes the behavior. The original behavior would print both "You aren't logged in as ubuntu" and "selenium isn't started", but now if you aren't logged in as ubuntu, you'll never get the the other message.

D41d8cd98f00b204e9800998ecf8427e

steved

March 5, 2011, March 05, 2011 20:34, permalink

No rating. Login to rate!

@ants: Ah, correct. I think avoiding the flag in this case is a questionable requirement. But, this should work

puts "environment is ready" if [user_ubuntu?, selenium_started?].all?
55502f40dc8b7c769880b10874abc9d0

rkr1410.myopenid.com

March 5, 2011, March 05, 2011 20:50, permalink

No rating. Login to rate!

Whether you use a flag or not, breaking the code into subroutines is in my opinion definitely a good refactoring in itself.
You could possibly keep the flag out of the functions and just use the returned value:

success = selenium_started?
success = success && user_ubuntu?
success = success && some_other_check?
...
puts "environment is ready" if success
55502f40dc8b7c769880b10874abc9d0

rkr1410.myopenid.com

March 5, 2011, March 05, 2011 20:52, permalink

No rating. Login to rate!

//deleted - double posting, sorry

A74c34f1043b833b1fcc86ce9f3521ee

pahanix

March 6, 2011, March 06, 2011 06:49, permalink

1 rating. Login to rate!

http://www.ruby-doc.org/core/classes/FalseClass.html#M000419
http://stackoverflow.com/questions/487067/how-to-avoid-short-circuit-evaluation-on

use & instead of && to do no short-circuit evaluation
but you have to make sure that question mark methods return true, false or nil
because for example Fixnum#& does bitwise AND or Array#& sets intersection

puts "environment is ready" if user_ubuntu? & selenium_started?
F9a9ba6663645458aa8630157ed5e71e

Ants

March 6, 2011, March 06, 2011 09:49, permalink

No rating. Login to rate!

Good find pahanix!

So much for the "principle of least surprise" when seeing that code though. Most people from a C/C++ background would assume short circuiting behavior even if it looks like a bitwise operation.

Your refactoring





Format Copy from initial code

or Cancel