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] : [];
}
2
  • What language is that ? Commented Aug 29, 2014 at 1:49
  • PHP: Hypertext Preprocessor. Commented Aug 29, 2014 at 3:49

1 Answer 1

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.

1
  • 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). Commented Aug 28, 2014 at 18:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.