Archive for October 25th, 2007

Cyclomatic complexity in GIMP code

Thursday, October 25th, 2007

After reading Xan’s article The Cyclomatic Horror From Outer Space analyzing the complexity of some GTK functions, I was curious and I wanted to run the same test in the GIMP source tree in order to see what parts of the code would be the hardest to test. This test is very simple and can be summarized as counting the number of decision points in every function in a program (so you get an idea of the number of possible code paths).

I did as suggested and I started with “apt-get install pmccabe“, followed by “pmccabe app/*/*.c | sort -nr | head -10” to get the 10 functions with the highest (worst) results. This gave me the following table:

Cyclomatic complexity Lines of code Function name
113 892 gimp_display_shell_canvas_tool_events
100 123 layers_actions_update
69 416 update_box_rgb
61 359 border_region
60 445 siox_foreground_extract
56 452 render_image_tile_fault
54 327 combine_inten_a_and_inten_a_pixels
53 194 gimp_plug_in_procedure_add_menu_path
47 275 gimp_drawable_offset
46 240 gimp_vector_tool_oper_update

According to the CMU page on cyclomatic complexity, numbers between 21 and 50 reveal a “complex, high risk program” and numbers above 50 only occur in an “untestable program (very high risk)“.

What does this mean for GIMP? Not much. But if you touch one of these functions, please be careful… you might break things and it will be very hard to find where the bugs are hiding in that code.