in reply to OO - Where to put a method

That really depends upon what you mean by "AvergeScore"?

I could see the case for each of your existing classes having an GetAverageScore method.

Student::GetAverageScore() as an instance method gives the students average score for a given test or all tests. As a class method it might give the average score for all the students for a given test or for all tests to date.

Question::GetaverageScore() as an instance method gives the ave. score for the all answers to the individual question. As a class method it might give the ave. score for all questions.

Teacher::GetAverageScore() as an instance method gives the average score for all questions set by this teacher. Which might be contrasted with the response to a class method of the same name giving the average score for all questions set by all teachers therebye giving a measure of whether the individual teacher tends to set easy or hard questions.

A mechanism for deciding where a method fits is to decide where and how you want to use it. This is the basic idea behind the "Use cases" design methodology. From the context of when and where you want to use a piece of information, it is usually fairly obvious where the method that produces it belongs. It's not always clear cut, but it narrows the field in most cases.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail

Replies are listed 'Best First'.
Re: Re: OO - Where to put a method
by Jaap (Curate) on Oct 08, 2003 at 07:16 UTC
    So you mean that both
    $student1->GetAverageScore() and
    Student::GetAverageScore() should return something different?

      I'm saying they could, not should.

      $student1->GetAverageScore() should return a value pertaining to $student1.

      Student::GetAverageScore() cannot return the average score for an individual student as it isn't being told which student to get an average score for.

      However, it could return an average score for the collective student body, which would make (a certain amount of) sense if the Class Student, represented a permanent storage of all students and Student::new( 'ID' ); returned an instance pertaining to the student who's identifier was 'ID'.

      There could be a Class method, Student::count() that returned the number of students enrolled, but $studunt1->count() probably wouldn't make any sense at all.

      Equally, a class method, Student::AveAge(); makes sense, but $student17->AveAge(); doesn't.

      Like I said, where you put a method really depends upon exactly what it returns (or does) and that in turn depends upon what how you need to use it.

      Don't be tempted to implement methods just because you can, whether class or instance. Only implement those you have a need for. You can always implement them later if the need arises, but pre-judging what might become useful has (at least) two downsides;

      1. It creates work at the start of a project when you are least able to resource it.
      2. It ties you to a specific implementation (or at least a specific interface), before you really know what the requirements are.

      I'm not sure that I've explained myself very well, but that's why I don't write books :)


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail