Red Green Repeat Adventures of a Spec Driven Junkie

Understanding assignment branch condition.

Recently, I integrated Rubocop into my work flow and it’s been very humbling. I feel I’m a great coder with all the specs passing and doing a good job of refactoring, Rubocop always finds something wrong with my code.

I usually go back and make fixes based on Rubocop’s suggestions, but I keep running into the same few issues and solving them isn’t as easy as using: rubocop -a (which auto-corrects all the easy ones!)

The toughest offense for me so far: Assignment Branch Condition

This offense is harder to fix really make me think about my coding style in general. I want to learn more about this offense to help me understand. By understanding, my goals is to not make this offense so often and/or make it easier for me to fix in the future.

What is: ABC?

Rubocop message:

The default is 15 and the first pass of my code is always way past this, probably on average twice this value. So cleaning up my code to meet the default value really takes a lot of work.

From the documentation :

Really Understanding: ABC

Let’s understand what ABC is by checking out the definition of ABC :

Broken down:

  • assignments (anything with = )
  • branches (anything that jumps out of the current method)
  • conditionals (anything that tests logic if , case , unary ? )

SO, to reduce the ABC value, reduce assignments (use less intermediate variables), fewer branches (calling other methods), and conditionals (if/else statements).

Computing ABC

The ABC value is not just a counting of them, but a square root of the sum of their squares. If any one of them getting too high will spike the ABC count.

The Rubocop default for ABC metric is 15. What does 15 really mean?

Well, doing the math, to get an ABC score of 15, a method would have:

  • 8 assignments
  • 8 conditionals

(Just working backwards from 15*15 => 225; 225/3 => 75; Math.sqrt(75) ~=> 8.66)

Now that I lay it out that way, an ABC value of 15 is very reasonable. Having eight of each for a method is just enough to do a lot of work in a method, but a value of 15 keeps the method from spiraling out of control in assignments, branches, or conditionals.

Whenever I encountered Rubocop’s ‘ABC is too high’ message, I was annoyed with ABC metric because I didn’t understand how it was computed and I couldn’t refactor efficiently to lower the ABC value quickly.

Now that I spent some effort into researching what Assignment Branch Condition really means, I feel better about creating or refactoring code that has a better ABC score.

IMAGES

  1. Conditional Assignment in Ruby

    ruby assignment branch condition size

  2. Solved Assume that a Ruby variable size is properly

    ruby assignment branch condition size

  3. operators in ruby

    ruby assignment branch condition size

  4. Shorthand Ruby Operators for Assignment Conditionals

    ruby assignment branch condition size

  5. RuboCopでこんなエラーが出た。Assignment Branch Condition size for search is too

    ruby assignment branch condition size

  6. PPT

    ruby assignment branch condition size