简体   繁体   中英

Ruby's Reek quality check

I'm getting below error in my Robot class:

Commands tests @robot.placed at least 4 times (RepeatedConditional)

This is the problematic code that's causing it:

def move
  @robot.move_forward if @robot.placed
end

def left
  @robot.left if @robot.placed
end

def right
  @robot.right if @robot.placed
end

def report
  puts @robot.report_current_position if @robot.placed
end

How would we re-organise this to avoid this warning?

you should refactor it out in a single method

def robot_placed?
  @robot.placed
end

and then call the method in your methods

def right
  @robot.right if robot_placed?
end

And put robot_placed? in the private section of your class ;-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM