0

Is the following method considered to be doing one thing only?

I'm wondering about that since it takes an optional argument.

public function findErrors($name = null)
{
    if ($name) {
        return isset($this->errors[$name]) ? $this->errors[$name] : [];
    }

    return $this->errors ?: [];
}

If not, would it be better / matter to have it separated like the following:

public function findErrors()
{
    return $this->errors ?: [];
}

public function findErrorsOf($name)
{
    return isset($this->errors[$name]) ? $this->errors[$name] : [];
}
3

No, not really. It's clearly got two independent paths. It would be better to separate them into two (possibly overloaded) functions to better decouple them.

Better yet, I would look to eliminate the name specific behavior unless it's really that common. What happens if you want to find errors since Thursday? What about errors that contain the word "Banana"? You shouldn't go back to edit this code every time you have a new search criteria - and you shouldn't have one way to find names and another way to find bananas.

  • I think I would keep the name specific behavior. These methods are from a Validator. So I would be able to get all the validated parameters errors with findErrors() or just from a specific parameter only with findErrorsOf($name). – Kid Diamond Aug 28 '14 at 18:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.