Tag Archives: cyclomatic complexity

Flog and to_proc_normal

Using metrics like flog can be immensely helpful for cleaning up code. However, metrics are fallible, and flog is no exception.

During a session of some serious code refactoring, flog was returning high scores on a couple of methods that appeared to be relatively clean, and the main culprit for the scores was something called to_proc_normal.

Consider this example. We have a Test class and Fruit class. Within the Test class are the following methods:

  def fruit_names_1
    fruits.map(&:name)
  end 

  def fruit_names_2
    fruits.map do |fruit|
      fruit.name
    end
  end

Both methods return an array containing the names of fruits associated to the Test class. They are, functionally, extremely similar. So how do they match up in flog?

Posted in Uncategorized | Tagged , , , | Leave a comment