Class Design Tips
Categories:
less than a minute
Class Design Tips
- Classes and instance variables should be nouns
- Classes should be singular unless they represent a collection. (Multiple ones come from multiple instantiations.)
- Question classes of just one instantiation. (e.g. should it be a class at all?)
- Methods should be verbs that describe behaviors/responsibilities.
- 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?
- Use inheritance to avoid code (and instance variable) duplication.
- If superclass has it, don’t redundantly add it to sub class.
- Use an interface to specify behavior (and anything needed for that behavior) that is common across classes without an is-a relationship.
- Names matter.
Last modified May 1, 2023: student-sourced updates (1f18b77)