Class Design Tips

Class Design Tips

  1. Classes and instance variables should be nouns
  2. Classes should be singular unless they represent a collection. (Multiple ones come from multiple instantiations.)
  3. Question classes of just one instantiation. (e.g. should it be a class at all?)
  4. Methods should be verbs that describe behaviors/responsibilities.
  5. Use inheritance to add or change behaviors.
    • Other differences can be managed by instance variable values most of the time. Add additional instance variables in subclass only if needed for new behavior, otherwise maybe it belongs in super class?
  6. Use inheritance to avoid code (and instance variable) duplication.
    • If superclass has it, don’t redundantly add it to sub class.
  7. Use an interface to specify behavior (and anything needed for that behavior) that is common across classes without an is-a relationship.
  8. Names matter.
Last modified April 30, 2022: practice coding exam (a2ce8c8)