Reek error RepeatedConditional

527 Views Asked by At

I have below error:

  tests @action.placed.!=(true) at least 3 times (RepeatedConditional)

Generated from below code:

    def left
        super unless @action.placed != true
    end

    def right
        super unless @action.placed != true
    end

    def move_forward
        super unless @action.placed != true
    end

How would we get rid of this repeat?

2

There are 2 best solutions below

1
On

Will an alias work?

alias :right :left
alias :move_forward :left
0
On

I think this explains it best: https://github.com/troessner/reek/blob/master/lib/reek/report/code_climate/code_climate_configuration.yml#L619. Because your object is checking the same condition multiple times, it is probably assuming the role of 2 objects and is missing an abstraction.

The solution may be creating 2 classes, one where @action.placed is always true and one where it is not always true. Another could be moving the logic up. Or maybe just combining these methods into 1. Ideally the goal would be to only have to check that condition once.