3 new "design and architectural" rules for Java: Monster Class, Brain Method, Singleton

Hello Java developers,

We are introducing new “design and architectural” rules to help you identify and draw your attention to classes or methods that may require refactoring.

A bit of history:
In the past, we used to offer certain features to enhance application design. Those who have been with us for a while may recall the Dependency Structure Matrix (DSM), which was only available for Java code.

[from the SonarQube in Action book by Ann Campbell - 2014]

Since the DSM feature was not that easy to use, actionable, and restricted to one programming language, we discontinued it and shifted our focus to rules that developers could act on directly.
At the same time, we clarified in 2022 our positioning that Sonar is the Home of Clean Code that goes beyond just code quality and security. Clean Code means also structured code, and clean architecture and design.

This is why we introduce today 3 rules dedicated to design:

  • S6541: The Singleton design pattern should be used with care): Methods should not perform too many tasks (aka Brain Method)
  • S6539: Classes should not depend on an excessive number of classes (aka Monster Class)
  • S6548: The Singleton design pattern should be used with care

These rules are designed to highlight classes or methods that may have been poorly designed. However, the findings generated by these rules are not immediately actionable. We do not expect developers to immediately jump into refactoring based on the results. Instead, our goal is to stimulate discussion and encourage action to gradually refactor sections of code that have become difficult to maintain over time.

As we are addressing a new domain with these rules, we have released them as Beta Code Smells with no associated Technical Debt. Therefore, your Quality Gate will not fail due to the findings generated by these rules. Although the concept of ‘Design Hotspot’ does not currently exist in SonarCloud or SonarQube, you can consider these new rules as falling into that category.

We look forward to getting your feedback about these rules. Share your needs about Architecture and Design rules with us by filling out this survey.

This is available now on SonarCloud and will be included in SonarQube 10.1