mardi 11 juin 2019

Single Responsibility Principle when method can accept various types of data?

Best practice recommends a method or function should do one thing, and do it well, so how can I apply SRP in the following scenario:

Summary: I have an API wrapper which sends a HTTP Post request, however in order to provide the json I want to allow the user multiple options, Lets say my function can accept any of the following:

def function_for_srp(jsonable_data: Union[Entity, Domain, str]):
    # Pseudo code (Violation of SRP?)
    if jsonable_data is instance of entity or domain:
        jsonable_data = json.dumps(jsonable_data) 
    else do nothing, as its a json encoded string of data already
    some_api_wrapper.post_data(jsonable_data) 

This function is doing multiple things depending on the data type passed to it, so its in violation of SRP? How do I overcome this design problem in a clean manner, ideally I am thinking something like this:

def function_for_srp_using_entity(entity: Entity): pass
def function_for_srp_using_domain(domain: Domain): pass
def function_for_srp(json_encoded_data: str): pass

Is the above 'pythonic' ? Is there a better way to do it?

# possible alternative?
def function_for_srp(jsonable_data: Union[Entity, Domain, str]):
    json = some_other_function(jsonable_data)
    some_api_wrapper.post_something(json)
    # Is this still a violation?

def some_other_function(jsonable_data: Union[Entity, Domain, str]):
    # Figure out the type and return a json encoded string that is suitable
    if isinstance of entity/domain, json dump and return
    else check if is valid json encoded string, if not make it valid and return it

Aucun commentaire:

Enregistrer un commentaire