Question

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?

Was it helpful?

Solution

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 ;-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top