Encapsulation vs Extract Class?

When I am working on implementing functionality of a class, I usually like to have minimum public interface for that class and extract some of detail implementation into private methods, but I too often feel hard to decide either to keep the knowledges in private method or extract those into new classes even I know that currently those new classes aren’t used by anywhere else.

Could you share your guideline/experience on how you decide to choose one over the other?

Hey, a concrete example would be helpful to form a better opinion, but what if you start thinking from the Single Responsibility Principle standpoint?

Some of the heuristics I use for this:

  • If the private part of the class is significantly larger than the public part, then I treat that a sign that it’s maybe time to extract some objects
  • If the specs are becoming complex, e.g. using many levels of nested contexts or lots of setup code.
1 Like

@amedr here is an example signup_inviter.rb · GitHub . In this example, I put everything inside my public class interface.