To return or to set a property

Sometimes I’m a reall doubting person… and a nitpicker :smiley:

I’m writing a class with lots of private internal methods, and I started wondering on what is best. number one or number two?

nr one:


private function foo()
{
    return $result;
}


private function bar($myfoo)
{
    //use $myfoo;
}

private function bar()
{
      $myfoo = $this->foo();
      $mybar = $this->bar($myfoo)

}

nr two:


private function foo()
{
    $this->_foo;
}


private function bar()
{
    //use $this->_foo;
}

private function bar()
{
      $this->foo();
      $this->bar()

}

which design would you pick and why?

thanks for the points of view :smiley:

The second complies more to OOP principles, the first is rather more procedural.

Though I’d have to ask - if the bar function requires foo to be run beforehand, why not run foo from inside bar?

But what about Unit testing then? If I want to to test a single method, I would need to fetch the property… So looking at the unit testing part, it makes more sense to return a value. I was thinking about that when I was out before I saw your answer.

I’m doing it to get a better overview. I have one main public method. If you read the code of that function, in theory you can see step by step was going on, and don’t have to go through layers of function. It makes the code more understandable when I would need to do an adjustment in a few months.

Unit testing should not execute private methods by themself. Instead, unit testing should set up different the initial public conditions so that the private methods end up being fully exercised.

  1. Set up the initial environment
  2. Exercise the code
  3. Test for the expected outcome

If the code being exercised sets internal state variables, you shouldrun other code that is dependent on those internal state variables so that you can determine what their state is.

It is not advised to add code to expose private variables and functions purely for the sake of testing convenience.

INteresting and good statement :smiley: thanks!

I always looked at the name Unit testing
And a Method is what I concidered a unit of a class

but your point makes more sense.
thanks for that insight :slight_smile:

That’s all right.

The issue with exposing private methods is that they are then no longer private. They are now publicly accessible, so you then need to document the fact that these public methods are only used for testing, which is another way of saying that they’re back doors in to your code.

Another problem with exposing private methods is that you lose some of the benefits of being private. You should not need to know how private methods do their job. They can and they should remain as black boxes, that are called upon by the class to do the work that is required.

A more stable way to deal with classes is to consider each class as a unit in its own right, which contains public methods for access and manipulation. That’s all you should need to know about a class, and it’s all that a test should exercise, because in the end, the only way that the class is going to be used is via its public interface.