What for?

If ever code you’ve written, or perhaps legacy code you encounter, is unmanageable and you don’t know where to start in unpacking its behaviour, fear not - give your code a good flogging. Flog is a gem that measures the complexity of code.

ruby_sadist_title

According to the Ruby Sadist, “Flog shows you the most torturous code you wrote. The more painful the code, the higher the score.”

What’s a good flog score?

According to a conversation initiated by Jake Scruggs 15 years ago:

flog_scoring

Example of usage

To make things relatable I’ll use the code from the previous blog entry, The GildedRose kata. It can be found here .

benjamin@benjamin.local /projects/gilded_rose
% flog lib/gilded_rose.rb                                                                                                                                 
   107.5: flog total                                                                                                                                      
    26.9: flog/method average                                                                                                                             
                                                                                                                                                          
   102.0: GildedRose#update_quality        lib/gilded_rose.rb:12-53         

With this score, the initial GildedRose solution is well beyond the danger zone, any attempt at the kata can attest to this. The score we get for the update_quality method is above 100, which indicates an outrageous amount of complexity.

After the refactor that was done through the two previous blog entries, we can flog the resulting code again to see the difference in complexity:

benjamin@benjamin.local /projects/gilded_rose
% flog lib/gilded_rose.rb                                                                                                                                 
    29.1: flog total                                                                                                                                      
     5.8: flog/method average                                                                                                                             
    15.0: GildedRose#update_quality        lib/gilded_rose.rb:32-39                                                                                       
     6.6: main#none      

With this new score (29), we’re in a far better place, but we’re not yet in an ideal place according to the scoring chart above. It’s nice to note that sometimes the scoring can be deceptive.The scoring of 29 doesnt really tell what the complexity of the ‘gildedrose.rb’ file actually is.

Instead, we should look at the scoring of individual methods, if we do that we notice that in the example above the scoring of the update_method, after the refactor, is 15. Which is actually good enough.

We can use this flogging tool to determine which methods would benefit from refactoring, in order to keep things modular, and stand by the single responsibility priniciple. If a flog score is too high, we can assume that SRP has not been respected and that the method in question is juggling multiple responsibilities that may lead to breaks and failures down the line, let alone difficulty to manually decipher the code in question.