Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Windows Tipps & SecurityGoogles neues Betriebssystem ist da – heißt jetzt aber komplett anders(22.09.2026 um 12:08 Uhr)
Windows Tipps & SecurityGooglebook-Premiere: Alle 5 Modelle ausprobiert – zwei stechen heraus(22.09.2026 um 12:30 Uhr)
Sichere ProgrammierungDeprecation notice: All-platform CodeQL bundle(22.09.2026 um 11:21 Uhr)
Sichere ProgrammierungCodeSOD: The John Cage Variable(22.09.2026 um 08:30 Uhr)
Windows Tipps & SecurityGoogles neues Betriebssystem ist da – heißt jetzt aber komplett anders(22.09.2026 um 12:08 Uhr)
Windows Tipps & SecurityGooglebook-Premiere: Alle 5 Modelle ausprobiert – zwei stechen heraus(22.09.2026 um 12:30 Uhr)
Sichere ProgrammierungDeprecation notice: All-platform CodeQL bundle(22.09.2026 um 11:21 Uhr)
Sichere ProgrammierungCodeSOD: The John Cage Variable(22.09.2026 um 08:30 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

How to write more stable and testable code 👉 use encapsulation

Encapsulation is often introduced as a basic OOP principle: “hide state, expose behavior.” But its real value becomes obvious when you start writing tests — especially for domain logic. Let’s look at a simple finance tracker example: a Wal…

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

Encapsulation is often introduced as a basic OOP principle: “hide state, expose behavior.”


But its real value becomes obvious when you start writing tests — especially for domain logic.



Let’s look at a simple finance tracker example: a Wallet that holds a balance and a list of transactions.






The non-encapsulated wallet







In a non-encapsulated design, the wallet is just a data holder. The business rules live elsewhere — for example, in a WalletRepository.



Tests look reasonable at first:




  • adding INCOME(500) increases the balance

  • adding INCOME(0) throws an exception

  • adding an expense larger than the balance is rejected



These tests pass, and everything seems fine.



But there’s a hidden assumption: all callers must always go through the repository.



The moment a caller bypasses it, the model breaks:




  • anyone can append directly to wallet.transactions

  • anyone can set wallet.balanceCents = -50



The wallet can no longer defend its own invariants.


Tests don’t verify the domain — they verify “correct usage” by convention.



This is the real cost of missing encapsulation.






The encapsulated wallet







In the encapsulated version, the rules move into the Wallet itself.



The wallet:




  • keeps its state private

  • exposes read-only views (List<Transaction>)

  • provides a single domain operation (wallet.add(tx))



Now the tests change in an important way. They no longer care who calls the wallet — only what happens when valid or invalid transactions are applied.



Invalid inputs are rejected at the boundary.


Impossible states cannot be constructed.


Bypassing the rules is no longer an option.



The key difference is not the if statements — it’s where they live.






Why this matters



Encapsulation turns your domain objects into the authority over their own correctness.



As a result:




  • tests become simpler and more meaningful

  • refactors break fewer tests

  • invariants are enforced consistently, everywhere



Encapsulation isn’t just about clean design.


It’s a way to make your tests trustworthy — and your domain harder to misuse.

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten How to write more stable and testable code 👉 use encapsulation

Thematisch verwandte Begriffe: write, more, stable, testable · 6 Treffer

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 ...

© 2015 - 2026 tsecurity.de — Nachrichten- & Content-Portal. Alle Rechte vorbehalten.

SSL 256-bit DSGVO Konform
Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-94493 | A vulnerability was detected in Gigatech PDV5701 1.0.31_240305_112640. T…
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