You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
Designing good classes
|
|
|
|
----------------------
|
|
|
|
* To guide design on the class level, ask: "How will this class be used?"
|
|
|
|
|
|
|
|
* A class should represent one coherent abstraction or concept.
|
|
|
|
|
|
|
|
* Look for code reuse opportunities (would this class be useful elsewhere?)
|
|
|
|
|
|
|
|
* When working with a class externally, you should not have to understand the internal implementation.
|
|
|
|
|
|
|
|
* Every constructor and public method should leave the instance variables in a consistent and correct state for the next public method call.
|
|
|
|
|
|
|
|
* Generally avoid going over 7 (plus or minus 2) instance variables in a class.
|
|
|
|
|
|
|
|
* Do not repeat data unless truly necessary.
|
|
|
|
|
|
|
|
* Do not give variables more scope than they require.
|
|
|
|
|
|
|
|
Immutable objects
|
|
|
|
-----------------
|
|
|
|
Benefits:
|
|
|
|
Thread-safe
|
|
|
|
Can freely share pointers to objects; no hidden side effects if something changes
|
|
|
|
Drawback:
|
|
|
|
If there are lots of changes then immutability may result in memory burn and slowdowns
|