liveBook · Manning

Class

As explained in Simple Object-Oriented Design: Create clean, maintainable applications

A programming-language construct that defines the structure and behavior of objects and can be extended or instantiated according to the language's object model.

Keep classes small as complexity grows. A class with too many methods becomes harder to understand, maintain, refactor, test, and evolve. section 1.2.1

Control class growth by:

  • Moving new complexity into a separate location instead of continually extending an existing class. section 2.3
  • Giving complex business logic its own cohesive class when the rule spans multiple classes or requires many dependencies. section 2.3.1
  • Keeping business logic close to the class it acts on; for example, let Enrollment mark itself canceled. section 2.3.1
  • Making the class that contains data enforce its consistency rather than requiring callers to coordinate related updates. section 3.1.1

Isolating complexity and consistency

Instead of adding a complex feature to an already growing class, place that feature in a dedicated class with explicit dependencies. Separately, let Offering reject enrollment beyond its participant limit and reduce available seats when enrollment succeeds. section 2.3.1 section 3.1.1

A practical design question

Ask whether you prefer many small classes or one larger class, and define what maximum method size would be reasonable. Use the answer to examine maintainability rather than enforce an unexplained absolute rule. section 2.4

Figure 1.2 Simple code becomes complex over time and, consequently, very hard to maintain.

Use the figure to trace how initially simple classes and methods become difficult to maintain as complexity accumulates. section 1.2.1

Figure 3.1 An entity ensuring consistency

Use the figure to see an entity class enforcing its own consistency. section 3.1.1

How Simple Object-Oriented Design: Create clean, maintainable applications covers it

How deep it goes
Explains class size, cohesion, isolation, and consistency responsibilities, while deferring encapsulation, state, and broader abstractions.
Examples
Uses Enrollment cancellation, Offering enrollment limits and seats, dedicated feature classes, and class-size reflection exercises.
What you can do after
Apply it to split growing responsibilities, isolate complex features, and enforce data consistency inside the responsible class.
How it connects
Connects classes with methods, business logic, cohesion, dependencies, abstraction, extension points, encapsulation, state, and consistency.

Also explained in

Compare how 21 books explain class →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Simple Object-Oriented Design: Create clean, maintainable applications. The section links point to the parts of the book it draws on.