EzDevInfo.com

reek

Code smell detector for Ruby troessner/reek · GitHub reek - code smell detector for ruby

Teach Me Code. Boolean parameters in methods. Why not?

I'm using

reek
as an analyzing tool for best practices in my code. However recently I found that if in the method, I have boolean parameters, such as.

def method (flag = true)

Reek gives me a warning.

Why does reek report tells me that it is a warning?


Source: (StackOverflow)

No such file to load while running rake reek task

https://github.com/kevinrutherford/reek/wiki/Rake-Task

I followed all the instructions in the above page to install the reek gem and run it, but I get the following error while running the rake reek task.

rake reek
rake aborted!
no such file to load -- reek/rake/task

(See full trace by running task with --trace)

gem list --local (shows you the right gem to have been installed)


Source: (StackOverflow)

Advertisements

reek complaints for DuplicateMethodCall format.json

I have this code reek complains for DuplicateMethodCall (calls format.json twice)

if object_error.blank?
  format.json { render json: order }
else
  format.json { render json: object_error, status: :unprocessable_entity }
end

What is the best way to do it in rails? or is this false positive?


Source: (StackOverflow)

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?


Source: (StackOverflow)

Ruby: Using Reek as a Training Tool

Would Reek be useful in training a ruby noob in good practices or does it require an experienced ruby eye to use and interpret?

I have mumble-muble years or programing experience but mostly in C variants. I've used Ruby lightly for the last few years as a utility scripting language but my ruby code is obviously largely just transliterated C. Now I wish to use it as a serious tool and I want to learn the "ruby way."

I've planned to use TDD/BDD from the start to provide the necessary training feedback and it looks like perhaps Reek might be useful in providing feedback about non-standard forms and uses. However, by long experience, I know that such tools can be two-edged swords that require prior experience to use and in hands of novice cause more problems than they solve.

Does anyone have experience in using reek (or a similar tool) in this way?

If it matters, I will be focusing initially on writing stand-alone applications for MacOS X i.e. no rails, server-stuff, etc.


Source: (StackOverflow)

If we cache params into a local var in an action, will it help or its the same?

So we run a code quality tool called reek once in a while as part of our project. The tool basically looks for code smells and reports them. Here, we observed that we get "Duplication" smell every time we try to access a key in params more than once (As if we are making a method-call twice with same parameters or we are duplicating an if condition etc). However, params is just a Hash, right? Other hashes don't get duplication smell when their keys are accessed more than once.

Why is this so? What are params exactly? Does it make sense to cache params in a local variable then use them? Will it help or its the same? Or is there something wrong with the tool? Help!


Source: (StackOverflow)

Reek codesmell Duplicate Method call fix

I'm getting below errors from reek:

lib/actions.rb -- 5 warnings:
  Actions#move_forward calls (self.x_coordinate + unit) twice (DuplicateMethodCall)
  Actions#move_forward calls place((self.x_coordinate + unit), self.y_coordinate, self.direction) twice (DuplicateMethodCall)
  Actions#move_forward calls self.direction 5 times (DuplicateMethodCall)
  Actions#move_forward calls self.x_coordinate 4 times (DuplicateMethodCall)
  Actions#move_forward calls self.y_coordinate 4 times (DuplicateMethodCall)

Below is the method move_forward

def move_forward(unit = 1)
        case self.direction
        when Direction::SOUTH
            place(self.x_coordinate, self.y_coordinate - unit, self.direction)
        when Direction::EAST
            place(self.x_coordinate + unit, self.y_coordinate, self.direction)
        when Direction::NORTH
            place(self.x_coordinate, self.y_coordinate + unit, self.direction)
        when Direction::WEST
            place(self.x_coordinate - unit, self.y_coordinate, self.direction)
        else

        end
    end

I want to remove all errors especially duplicate method call. What would be the best way to fix all warnings in this case?


Source: (StackOverflow)

Reek error RepeatedConditional

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?


Source: (StackOverflow)

How to rectify DuplicateMethodCall from reek

How to rectify DuplicateMethodCall from reek on the following calls

def to_str
    foo.blank? ? 'Value' : foo
end 

How should I handle params[:some] should I declare it separately.

if params[:some] == 'Action1'

elsif params[:some] == 'Action2'

elsif params[:some] == 'Action3'

elsif params[:some] == 'Action4'

end

Source: (StackOverflow)

Why does Eclipse complain about "Feature envy" smell in my code?

Eclipse (RedRails) complain about "Feature envy" in the following code:

if input_text =~ /^(---\s*\n.*?\n?)(---.*?)/m
  content_text = input_text[($1.size + $2.size)..-1] # warning in $1

  header = YAML.load($1)

  @content = content_text.strip()
  @title = header["title"]
end

My understanding is that I safe to ignore this warning. But I am wandering why this warning is generated. I cannot understand how I can extract method for $1.size and $1.


Source: (StackOverflow)