Evade Object Disorientation

As you investigate hotspots, you’ll likely encounter several violations of the principles of information hiding and encapsulation, often more subtle than the code smells seen so far. Two common offenses are primitive obsession and breaking the Law of Demeter. Let’s discuss them in detail since both problems serve as an open invitation for letting more complexity into our code.

Avoid Primitive Obsession

Quick, how many times have you come across code similar to the next listing?

 boolean​ ​changePassword​(
  String userName,
  String existingPassword,
  String newPassword) {
  ...
 }

The preceding code uses a String for representing two separate domain concepts, which is a poor choice in any API and just reflects a ...

Get Your Code as a Crime Scene, Second Edition, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.