mardi 8 septembre 2020

Should I use generic or concrete setters and getters to handle mapping entities?

I have an application where users are able to change some settings. Users can change (set) and read (get) their settings, it's a mapping-like structure. I made an UserSettingsInteractor object that implements the use cases of changing and reading settings, in Python it looks like:

class UserSettingsInteractor:

    def __init__(self, ...): ...

    def set_foo(self, user_id: int, value): ...
    def get_foo(self, user_id: int): ...
    
    def set_bar(self, user_id: int, value): ...
    def get_bar(self, user_id: int): ...

    ...

The good point in it is that the existing settings, foo and bar, are explicit. But writing a setter and getter makes it much verbose. It could be a generalized as:

class UserSettingsInteractor:

    def __init__(self, ...): ...

    def set(self, user_id: int, setting: str, value): ...
    def get(self, user_id: int, setting: str): ...

Now the client code knows how a setting is represented, it's a key-value pair. Does not it make the code more fragile?

Which approach is better?

Aucun commentaire:

Enregistrer un commentaire