Question

Example: I have a method that formats the output based on a parameter from an object. The question is, should I pass the object that holds the parameter to the method, or just the required param?

Still trying to grasp clean coding so I might be wrong, my take is that the 2nd approach is cleaner because it doesn't require you to pass the entire object, and is easy to test.
However, it makes calling the method ugly now since I have to check for the param every time when I call the method. And its used very very often.

In contrast, the first approach while not that easy to test, because I have to mock the requestObject, is much easier to use.

So which method should I go with? Or is there an even better approach?

// The output method with object as input
public function outputResponse($message, $requestObject) {
    if (isset($resquestObject->param['response_type']) && $requestObject->param['response_type'] == 'json') return json_ecode($message);
    return xmlOutput($message);
}

// So you call it like
$this->outputResponse($message, $requestObject);

The other approach...

// The output method with just the param as input
public function outputResponse($message, $responseType) {
    if (responseType == 'json') return json_ecode($message);
    return xmlOutput($message);
}

// So you call it like
$responseType = (isset($resquestObject->param['response_type']) && $requestObject->param['response_type'] == 'json') ? 'json' : '';
$this->outputResponse($message, $responseType);

No correct solution

Licensed under: CC-BY-SA with attribution
scroll top