Skip to content

Latest commit

 

History

History
24 lines (15 loc) · 831 Bytes

m1.md

File metadata and controls

24 lines (15 loc) · 831 Bytes

Classes and interface

Minimize accessibility

Classes and members should be as inaccessible as possible, because this allows us to test components in isolation. A class or member that is not private is part of the API and must be supported forever.

Accessor methods instead of public fields

Accessor methods (getters and setters) are better than allowing access to a field directly because you can change the internal representation without changing the API

Minimize mutabiilty

Rules to make class immutable:

  1. Make fields private and final
  2. Don't provide setters
  3. Prevent class extending (make class final)
  4. Prevent access to reference of mutable components

Benefits of immutable objects:

  • They are thread-safe, require no synchronization
  • Can be shared freely
  • Great building blocks for other objects