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