Imagine the following method converting a boolean into an int:
def bool_to_int(bool)
bool ? 1 : 0
end
bool_to_int(false) # => 0
bool_to_int(true) # => 1
Because of the conditional, the cyclomatic complexity score is two. Does the score drop to one by utilizing refinements in Ruby?:
module Extension
refine FalseClass do
def to_int; 0; end
end
refine TrueClass do
def to_int; 1 end
end
end
using Extension
false.to_int # => 0
true.to_int # => 1
In other words; does dynamic dispatching reduce the cyclomatic complexity score or are we just hiding the complexity by letting Ruby do the “heavy lifting”?
Cyclomatic complexity is based on the nodes and edges available in them. By implementing dynamic dispatching, you are just implementing conditions in a different way.
So, the cyclomatic complexity stays constant.
Edit based on the below comments.
Yes, the cyclomatic concept is bound tightly with conditional statements. Reducing if/else statements results in less cyclomatic complexity. But the paths are constants. But now they are more isolated and not based on the conditions.
So I feel, the answer should be, Yes, the complexity gets reduced if you are successful in removing the conditional statements. But paths are constants.