Class design stuff

Three good class traits

Note that in the following I’m saying class (to emphasize that this is about class design), but I’m really referring to an instance of that class (or an object). anyway …

  • all of the variables in a class are useful for as long as the class lives

  • all of the methods in a class can be called independently of one another and in any order

  • only the following methods are called:
    – methods that belong to the class
    – methods that belong to objects created within the method
    – methods that belong to objects which are arguments to the method
    – methods that belong to objects stored in the classes variables

Thoughts or opinions? None of these things are my creation, but I find them incredibly helpful (in practice) for quickly identifying weak points in my class (besides the usual stuff … no duplication, complexity, etc.).

Try applying these three things to the code you are working with now. Chances are if one of them is being violated it should be moved to a new class (either one that exists or a new one altogether).

Of course there are quite a few other things that can be talked about, but these three things are easy to identify and tend to naturally lead me in the direction of a good design.

  • M

hehe… OOP 101 (but very useful to remember from time to time).

PHP could probably have a few others, because of the way it has to be used.

I’d say that all of the public methods should be able to be called independently. We don’t want to encourage massive public methods that don’t have simple supporting private methods that can be reused throughout the class. :slight_smile:

That is a good clarification. private methods are helper methods.

[quote=Templarian;2340006]hehe… OOP 101 (but very useful to remember from time to time).

PHP could probably have a few others, because of the way it has to be used.[/quote]

I apply this to my code all of the time. It’s my opinion that people don’t focus enough on the small things.