Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
IT NachrichtenDNA-Computer rechnet ohne Strom und bricht Tempo-Rekord(23.09.2026 um 13:39 Uhr)
IT NachrichtenApple arbeitet an Fitnesstracker ohne Display(23.09.2026 um 12:37 Uhr)
IT NachrichtenDiese Serien scheiterten mit Staffel 2(23.09.2026 um 13:20 Uhr)
IT NachrichtenDNA-Computer rechnet ohne Strom und bricht Tempo-Rekord(23.09.2026 um 13:39 Uhr)
IT NachrichtenApple arbeitet an Fitnesstracker ohne Display(23.09.2026 um 12:37 Uhr)
IT NachrichtenDiese Serien scheiterten mit Staffel 2(23.09.2026 um 13:20 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

🧠 From Chaos to Clean Code: My Java Refactor Journey - Part 2 of 6

🔧 Step 1: Separating Responsibilities – From Spaghetti to Structure The first class I refactored was a typical all-in-one mess: controller, business logic, and in-memory storage were all crammed into the same plac…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!




🔧 Step 1: Separating Responsibilities – From Spaghetti to Structure



The first class I refactored was a typical all-in-one mess: controller, business logic, and in-memory storage were all crammed into the same place.




@RestController
@RequestMapping("/things")
public class ControllerService {
private List<String> list = new ArrayList<>();
// ...
}









@RestController
@RequestMapping("/items")
public class ModelRepoController {
private Map<Long, Item> storage = new HashMap<>();
// ...
}






This class did everything — which is exactly what we want to avoid in domain-oriented architecture.






🔄 The refactor



Split the logic into four separate components:





  • ThingController/ItemController: exposes the HTTP API


  • ThingService/ItemService: handles the business logic


  • ThingRepository/ItemRepository: manages data access (in-memory for now)


  • Thing/Item: represents the domain entity



The logic stayed the same, but now it's clean, testable, and aligned with Clean Architecture principles.



💡 Before: logic and storage were tightly coupled in the controller

💡 After: the controller simply orchestrates calls to the service



🔗 Check out the before and after in the repo.





🗂️ Project structure after the refactor:





com.example.spaghetti
├── controller
│ └── ThingController.java
│ └── ItemController.java
├── service
│ └── ThingService.java
│ └── ItemService.java
├── repository
│ └── ThingRepository.java
│ └── ItemRepository.java
├── model
│ └── Thing.java
│ └── Item.java







🔧 Step 2: From “Useless Bean” to Simple Validation



In the initial version of the code, I had a leftover bean:




@Bean
public String uselessBean() {
return "I am a useless bean";
}






🫠 It did absolutely nothing — until now.



Instead of deleting it, I replaced it with a ✨ simple validation rule:

✅ check if a name starts with an uppercase letter.




@Bean
public Predicate<String> nameStartsWithUppercaseValidator() {
return name -> name != null && !name.isEmpty() && Character.isUpperCase(name.charAt(0));
}






I then injected it into the controller and used it to validate input before saving a new item.

No big framework, no annotation magic — just a good old @bean doing something useful.



📦 Where should this bean live?



Since this is a generic validation, not part of the domain logic itself, I moved it to a dedicated config package:




com.example.spaghetti
├── config
│ └── MainConfig.java






According to DDD principles, reusable and infrastructure-related beans like this one shouldn't live inside your domain or application logic.

Placing it under config (or infrastructure.config) keeps your architecture clean and responsibilities well separated.



💡 Small win: the bean now enforces a business rule — and the code stays clean and reusable.



🧠 Even small refactors like this help keep things tidy and meaningful.





🔧 Step 3: Giving Purpose to the Utils Class



Previously, our Utils class had two static methods that weren’t actually used anywhere.



Now, we brought it to life by adding a new method that counts the number of letters in a given string:




public int countLetters(String input) {
if (input == null) return 0;
return (int) input.chars()
.filter(Character::isLetter)
.count();
}






This method helps us process input names more meaningfully.



In the controller, we call this method to log how many letters the submitted name has before saving it.



Small steps like this turn “unused helpers” into valuable tools for our application!






🚀 Conclusion: Setting the Stage for Clean Architecture and DDD



We’ve cleaned up the code by separating responsibilities and giving purpose to unused parts like the validation bean and utils. 🧹✨



This foundation makes our codebase cleaner and easier to maintain. 🏗️🧱



Next, we’ll introduce Clean Architecture layers and apply proper domain modeling with DDD to take our design to the next level. 📐📚



Stay tuned! 👀

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten 🧠 From Chaos to Clean Code: My Java Refactor Journey - Part 2 of 6

Thematisch verwandte Begriffe: From, Chaos, Clean, Code · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-19438 | Improper Limitation of a Pathname to a Restricted Directory ('Path Trave…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick