load_xbm - arnthor89/pygame GitHub Wiki

load_xbm

Description

The function reads a pair of XBM files(a image and a mask for the image), representing a cursor image, into a format that can be sent to the set_cursor function.

Complexity

The reason for the high complexity in the function is that the function contains 6 for loops and a lot of if-statements. A contributing factor to the high CC can also be that it has an inner function definition. A reason for the high amount of for loops is that the function handles two images in the same way but uses almost code for handling them, thus having twice the amount of for-loops necessary if functions were used.

Refactoring strategy

A simple way to decrease the CC would be to split the function into smaller functions. The two for loops between line 263 and line 270 are just use to remove comments from the image and mask file. The two for loops are doing the exact same thing but on the two images. One could take the one of the for loops and put it into a function called remove_comments. This function would take the list of file's lines and return a list of lines which does not contain comments.

Similarly, the 4 remaining for loops between line 283 to 299 could be substituted for a function with two for loops which would be called extract data. (There are 4 for loops but they do the same twice but for the two different files)