Skip to content

The Art of Thinking in Objects

Why Clean Object-Oriented Code Demands More Than Just Writing Classes

White Balloon

Photo from Kiran CK on Unsplash

The Higher Level of Modularization

Today we focus on the next level of software modularization: objects. At this point, the topic of Clean Code becomes more challenging, as object-oriented programming opens up entirely new design possibilities – and consequently requires additional knowledge and new rules. A similar jump in complexity also occurs with functional programming languages, where we model with functions instead of objects.

When Is Object-Oriented Code Truly "Clean"?

The short answer: When object-oriented design principles are consistently followed. Behind this simple statement, however, lies an entire world of concepts and practices. The basic rule is: If an object-oriented language like Java is being used, one should actually think and program in an object-oriented way.

The Current Reality

In practice, we often see procedural code disguised as object-oriented. A typical example is the classic Spring Boot application with three layers. This is understandable because true object orientation is demanding and requires solid knowledge in areas such as object-oriented analysis and design (OOAD) and design patterns. Those who actually want to program procedurally might consider modern cloud-oriented languages like Go, which are optimized for this approach.

Essential OO Principles for Clean Code

When using a hybrid programming approach, one should at least pay attention to these fundamental aspects:

  • True encapsulation: Avoid object.getA().getB() and replace with meaningful methods like object.doSomething()

  • Small classes: Like functions, classes should remain manageable in size

  • Single Responsibility Principle: Each class should be responsible for only one topic/concept (from the well-known SOLID catalog)

  • High Cohesion, Low Coupling: High internal cohesion with low external dependencies

  • And additional important principles...

The Most Common Pitfalls in Practice

In my experience, these are the most widespread problems:

  1. Developers pack too many responsibilities into a single class

  2. Too little attention is paid to class naming

  3. The public interface of a class is not carefully thought out

The Actual Goal

Object-oriented programming should simplify development by allowing us to directly map the terms and concepts of the domain with classes. This is a specific method of modeling – and as with any method: It will only work if you follow its basic principles. Otherwise, more problems than benefits arise.

The Path to Successful OO Design

Understanding Java as a language doesn't automatically mean you can build good object-oriented systems with it. Without solid knowledge and application of OO principles, code quickly becomes difficult to maintain. Therefore, it's crucial to thoroughly learn these concepts.

You don't need to immediately create highly flexible, perfectly extensible designs – it's sufficient to initially adhere to some basic rules. The greatest challenge lies in modeling classes to be closed to changes but open for extensions – a principle that becomes particularly relevant in framework development.

Author

Sebastian Bergandy
Sebastian Bergandy