lundi 2 décembre 2019

How to refactor the code of a class composed of one public method and multiple private method that only do check

There is a code I'm working on that I find kind of annoying. Indeed, the class is composed of one public method and many private methods to check stuff and or to output some logs.

There are 3 types of private method in the code. The firsts private method only do some check on the input data and raise if something is inconsistent. (3 methods) The seconds do the only produce some log reports. (2 methods) And the thirds prepare and save the data (2 methods) (The purpose of this class), those are composed of many other external library that I have to mock.

Then each of the private method are called in the public method of the class that we will call execute().

Every test runs on the execute method that include the whole logic of the code behind private functions. To test the code, I mock many call of external methods inside the method that save and look at the args that are called when I run the execute method in my test, because of this every test I write are 75% function mocking calls. It's annoying.

I would like to split it but each I tried I look at it and find it useless because it's more like I just extract the private method in another.

Here is pseudo code of the class. It's in python but it is irrelevant :

class MyDigustingClass():

    def _first_check(self, args...):
        pass

    def _second_check(self, args...):
        pass

    def _third_check(self, args...):
        pass

    def _log_method_1(self, args...):
        pass

    def _log_method_2(self, args...):
        pass

    def _prepare_data(self, args...):
        pass

    def _save_data(self, args...):
        pass

    def execute(self, args...):
        # Call check methods

        # Call prepare_data

        # Call save_data

        # Call logs method

As I said, I struggle to find a good idea of refactoring because I can't do better than a flat pass. What would you suggest.

Aucun commentaire:

Enregistrer un commentaire