What does “Branch condition size too high” mean and how to fix it?

In my Rails application, I use Rubocop to check for problems. Today he gave me this error: Assignment Branch Condition size for show is too high . Here is my code:

 def show @category = Category.friendly.find(params[:id]) @categories = Category.all @search = @category.products.approved.order(updated_at: :desc).ransack(params[:q]) @products = @search.result.page(params[:page]).per(50) rate end 

What does this mean and how can I fix it?

+90
ruby ruby-on-rails code-metrics rubocop
Jun 19 '15 at 7:47
source share
1 answer

Destination branch condition (ABC) size is a measurement of the size of a method. In essence, this is determined by counting the number of A signatures, B ranch and C conditional statements. (more info ..)

To reduce the ABC score, you can move some of these assignments to before_action calls:

 before_action :fetch_current_category, only: [:show,:edit,:update] before_action :fetch_categories, only: [:show,:edit,:update] before_action :fetch_search_results, only: [:show,:edit,:update] #or whatever def show rate end private def fetch_current_category @category = Category.friendly.find(params[:id]) end def fetch_categories @categories = Category.all end def fetch_search_results @search = category.products.approved.order(updated_at: :desc).ransack(params[:q]) @products = @search.result.page(params[:page]).per(50) end 
+92
Jun 19 '15 at 16:18
source share



All Articles