jeudi 22 février 2018

Coupling and Performance/readability

I am working on refactoring some Object-Oriented code and I have the following problem:

The code design is mostly highly cohesive but classes are highly coupled together.

There are certain values (e.g. employeeID, project_id and location_id) that are used in many of the classes, so are passed as parameters quite often. The issue is that more often than not, classes use many more parameters (e.g. role, trajectory) that are derived from the first three values. As long as the class has the first three values, it can calculate the rest using helpers or similar.

The list of these secondary parameters can get quite long, but they are necessary. I could remove these arguments and just work them out in the constructor of the classes, but recalculating these values very often may impact the program.

I am concerned about the performance cost and readability. Some values (e.g. role) would be easy to query from the DB using employee_id, project_id and location_id, but others are more resource intensive (e.g. there is a class that calculates trajectories, and has to use geometry libraries, etc.).

What pattern would be desired in this situation? Is it worth reducing coupling at an increased cost? Here is are some examples of how the new classes could look:

Example 1:

class EmployeeMonitor:

    def __init__(self, employee_id, project_id, location_id, role, settings_list, gps_car_trajectory):
        pass

Example 2:

class EmployeeMonitor:

    def __init__(self, employee_id, project_id, location_id):
        role = calcRole()                      # low cost
        settings_list = calcSettings()         # low-ish cost
        gps_car_trajectory = calcTrajectory()  # medium cost (~200ms)

Understand that this is an example as I am not allowed to release any code. Any suggestion would be appreciated! Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire