[RUBY] I got this error in RuboCop. Assignment Branch Condition size for search is too high.

What this error

Assignment Branch Condition size for search is too high. [<10, 21, 5> 23.79/20] 

I got this error before committing. When I looked it up, it seemed like an error that was too wide.

What is RuboCop? simply

RuboCop's Github https://github.com/rubocop-hq/rubocop/tree/v0.28.0

This RubCop seems to inspect the code and tells you that the code is long, the indentation is weird, and so on. Sometimes it will fix it (correct the unnatural indentation), and sometimes it will just give you a warning and ask you to do something about it yourself. However, it seems that not all of the points from RuboCop are correct.

Regarding this error

Assignment Branch Condition size for search is too high. [<10, 21, 5> 23.79/20] 

As I mentioned at the beginning, it seems to have a naive meaning.

The code that was actually pointed out

  def search
    if params[:city_id]
      pagy, offices = pagy Office.where(city_id: params[:city_id])
      pagy_headers_merge(pagy)
    elsif params[:keyword]
      keywords = params[:keyword].split(/[[:blank:]]+/).select(&:present?)
      pagy, offices = pagy_array([])
      pagy_headers_merge(pagy)
      keywords.each do |keyword|
        offices += Office.where('name LIKE (?) OR
                                 address LIKE (?) OR
                                 near_station LIKE (?) OR
                                 introduction LIKE (?) OR
                                 company LIKE (?)',
                                "%#{keyword}%",
                                "%#{keyword}%",
                                "%#{keyword}%",
                                "%#{keyword}%",
                                "%#{keyword}%")
      end
    else
      pagy, offices = pagy(Office.all)
      pagy_headers_merge(pagy)
    end
    render json: offices, each_serializer: OfficeIndexSerializer, include: '**'
  end

I wrote it myself and thought it was a long time. Vertically long. However, with my current knowledge, I couldn't think of how to make the code more concise. Please let me know.

So what happened after all ...

.rubocop.yml Changed the settings of RuboCop. Looking at the error statement, ...

Assignment Branch Condition size for search is too high. [<10, 21, 5> 23.79/20] 

[<10, 21, 5> 23.79/20]This part seems to represent the score. Looking at this, it says, "You have a MAX score of 20, but you are 23..It's 79. ”I thought I had to do something about it.



 So I changed the settings in .rubocop.yml.

 How did you change it?

``` ruby
Metrics/AbcSize:
  # The ABC size is a calculated magnitude, so this number can be a Fixnum or
  # a Float.
  Max: 15 

I changed the setting of this Max part to 25. And when I commit, I didn't get any suggestions from RubCop.

in conclusion

I hope you can feel it in this article to the extent that you can change the settings.

Recommended Posts

I got this error in RuboCop. Assignment Branch Condition size for search is too high.