Zum Hauptinhalt springen
Echtzeit-Radar & Feeds
Alle RSS Feeds ➔
👥 Community & Social
•
IT Security NachrichtenOnePlus/OxygenOS: Schad-App erhält Root-Zugriff ohne Berechtigungen(24.09.2026 um 23:38 Uhr)
•
IT Security NachrichtenRyuk Member Karen Vardanyan Sentenced to Two Years in U.S. Prison(24.09.2026 um 22:50 Uhr)
•••••
Hacking & PentestingRyuk Member Karen Vardanyan Sentenced to Two Years in U.S. Prison(24.09.2026 um 22:50 Uhr)
•
AI & KI NachrichtenWhy the U.N. Still Matters(24.09.2026 um 23:00 Uhr)
•••
IT Security NachrichtenOnePlus/OxygenOS: Schad-App erhält Root-Zugriff ohne Berechtigungen(24.09.2026 um 23:38 Uhr)
•
IT Security NachrichtenRyuk Member Karen Vardanyan Sentenced to Two Years in U.S. Prison(24.09.2026 um 22:50 Uhr)
•••••
Hacking & PentestingRyuk Member Karen Vardanyan Sentenced to Two Years in U.S. Prison(24.09.2026 um 22:50 Uhr)
•
AI & KI NachrichtenWhy the U.N. Still Matters(24.09.2026 um 23:00 Uhr)
••
Intelligence View
⚡ tsecurity.de Intelligence

Refactoring 029 - Replace NULL With Collection

Transform optional attributes into empty collections for cleaner, safer, and polymorphic code, banishing the billion-dollar mistake TL;DR: Replace nullable optional attributes with empty collections to eliminate null checks and leverage…

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

Transform optional attributes into empty collections for cleaner, safer, and polymorphic code, banishing the billion-dollar mistake




TL;DR: Replace nullable optional attributes with empty collections to eliminate null checks and leverage polymorphism.







Problems Addressed 😔








Related Code Smells 💨


























Steps 👣




  1. Identify nullable optional attributes that could be collections

  2. Replace single nullable objects with empty collections

  3. Remove all null checks related to these optional attributes

  4. Update methods to work with collections instead of single objects






Sample Code 💻






Before 🚨






public class ShoppingCart {
private List<Item> items = new ArrayList<>();
private Coupon coupon = null;

public void addItem(Item item) {
this.items.add(item);
}

public void redeemCoupon(Coupon coupon) {
this.coupon = coupon;
}

public double total() {
double total = 0;

for (Item item : this.items) {
total += item.getPrice();
}

// This a polluted IF and null check
if (this.coupon != null) {
total -= this.coupon.getDiscount();
}

return total;
}

public boolean hasUnsavedChanges() {
// Explicit null check
return !this.items.isEmpty() || this.coupon != null;
}

public boolean hasCoupon() {
return this.coupon != null;
}
}









public class ShoppingCart {
private final List<Item> items = new ArrayList<>();

// This version uses Optionals
// Not all programming languages support this feature
private Optional<Coupon> coupon = Optional.empty();

public void addItem(Item item) {
items.add(item);
}

public void redeemCoupon(Coupon coupon) {
// You need to understand how optionals work
this.coupon = Optional.ofNullable(coupon);
}

public boolean hasUnsavedChanges() {
return !items.isEmpty() || !coupon.isPresent();
}

public boolean hasCoupon() {
return coupon.isPresent();
}
}







After 👉







public class ShoppingCart {
private List<Item> items = new ArrayList<>();

// 1. Identify nullable optional attributes
// that could be collections
// 2. Replace single nullable objects with empty collections
private List<Coupon> coupons = new ArrayList<>();

public void addItem(Item item) {
this.items.add(item);
}

// Step 4: Work with collection
// instead of single nullable object
public void redeemCoupon(Coupon coupon) {
this.coupons.add(coupon);
}

// Step 4: Simplified logic without null checks
public double total() {
double total = 0;

for (Item item : this.items) {
total += item.getPrice();
}

// 3. Remove all null checks
// related to these optional attributes
for (Coupon coupon : this.coupons) {
total -= coupon.getDiscount();
}

return total;
}

// Consistent behavior with empty collections
public boolean hasUnsavedChanges() {
// 4. Update methods to work with collections
// instead of single objects
return !this.items.isEmpty() || !this.coupons.isEmpty();
}

// 3. Remove all null checks
// related to these optional attributes
// Collection-based check instead of null check
public boolean hasCoupon() {
return !this.coupons.isEmpty();
}
}







Type 📝



[X] Semi-Automatic





Safety 🛡️



This refactoring is generally safe when you control all access points to the collection attributes.



You need to ensure that no external code expects null values and deal with inside APIs.



The refactoring maintains the same external behavior while simplifying internal logic.



You should verify that all constructors and factory methods initialize collections properly.





Why is the Code Better? ✨



The refactored code eliminates null pointer exceptions and reduces conditional complexity.



Empty collections and non-empty collections behave polymorphically, allowing you to treat them uniformly.



The code becomes more predictable since collections always exist (at least empty) and respond to the same operations.



Method implementations become shorter and more focused on business logic rather than null handling.



The approach aligns with the principle of making illegal states unrepresentable in your domain model, leading to more robust and maintainable code.



Empty collections and non-empty collections are polymorphic.





How Does it Improve the Bijection? 🗺️



In the real world, containers exist even when empty.



By representing optional collections as empty collections rather than null, you create a more accurate model of reality.



Null does not exist in real world and it always breaks the bijection.



This maintains the one-to-one correspondence between real-world concepts and your computational model, creating a good MAPPER.



When you return a collection instead of nulls, you also reduce the coupling.





Limitations ⚠️



This refactoring may not be suitable when null has semantic meaning different from "empty". Some legacy APIs might expect null values, requiring adaptation layers.



You need to ensure all code paths initialize collections consistently to avoid mixed null and empty states.





Refactor with AI 🤖




Suggested Prompt: 1. Identify nullable optional attributes that could be collections 2. Replace single nullable objects with empty collections 3. Remove all null checks related to these optional attributes 4. Update methods to work with collections instead of single objects 5. Test that empty and non-empty collections behave consistently








Tags 🏷️




  • Null





Level 🔋



[X] Intermediate





Related Refactorings 🔄















See also 📚














Credits 🙏



Image by Eak K. on Pixabay






This article is part of the Refactoring Series.




CTI Threat Relationship Graph2 Knoten / 1 Relationen
CVE / Incident Software MITRE ATT&CK CWE Weakness IoC
SOC Incident Playbook: Remote Code Execution (RCE) Defense
Syntax validiert (0 Fehler)
title: Detect Exploitation - Refactoring 029 - Replace NULL With Collection
id: 736331e9-ce6c-400e-83b8-82b452657266
status: experimental
description: Automatisch generierte SIEM-Erkennungsregel basierend auf CTI Intelligence
references:
  - https://tsecurity.de/
author: iShareStuff CTI Automated Detection Engine
date: 2026-09-25
logsource:
  category: network_connection
  product: any
detection:
  selection:
      CommandLine|contains:
        - 'exploit'
  condition: selection
falsepositives:
  - Legitime administrative Zugriffe oder Penetrationstests
level: high
tags:
  - attack.initial_access
Syntax validiert (0 Fehler)
rule CTI_Threat_Indicator {
    meta:
        author = "iShareStuff CTI Automated Detection Engine"
        date = "2026-09-25"
        description = "YARA Signature for "
    strings:
        $str = "Refactoring 029 - Replace NULL" ascii wide
    condition:
        any of them
}
Syntax validiert (0 Fehler)
index=security sourcetype IN ("cisco:asa", "pan:traffic", "zeek_conn", "suricata", "WinEventLog:Security")
("Refactoring 029 - Replace NULL With Coll")
| stats count earliest(_time) as first_seen latest(_time) as last_seen by src_ip, dest_ip, dest_host, signature
| eval first_seen=strftime(first_seen, "%Y-%m-%d %H:%M:%S"), last_seen=strftime(last_seen, "%Y-%m-%d %H:%M:%S")
| sort - count
Syntax validiert (0 Fehler)
message: "*Refactoring 029 - Replace NULL With Coll*"
Syntax validiert (0 Fehler)
CommonSecurityLog
| where Message has "Refactoring 029 - Replace NULL With Coll"
| summarize EventCount = count(), FirstSeen = min(TimeGenerated), LastSeen = max(TimeGenerated) by SourceIP, DestinationIP, DestinationPort, Activity
| extend DetectionRule = "iShareStuff-CTI-Compiled"
| sort by EventCount desc
🎯
MITRE ATT&CK Matrix Navigator 14 Taktiken
Reconnaissance
-
Resource Development
-
Initial Access
Execution
Persistence
-
Privilege Escalation
Defense Evasion
Credential Access
-
Discovery
-
Lateral Movement
-
Collection
-
Command and Control
Exfiltration
-
Impact
tsecurity.de Cognitive Threat RAG
Fokus-Vektor:

Kognitive Analyse für identifizierte Bedrohung: Erhöhte Bedrohungslage im Bereich Refactoring 029 - Replace NULL With Coll.... Basierend auf 368k Vektor-Korrelationen werden sofortige Isolationsmaßnahmen für betroffene Endpunkte empfohlen.

🛡️ Angriffsfläche & Exposure

Netzwerk/Remote-Zugriff ohne Vorauthentifizierung möglich.

⚡ Empfohlene Sofortmaßnahmen
  • 1. Perimeter-Inspektion: Relevante Portfreigaben und exponierte Endpunkte unverzüglich scannen.
  • 2. Patch-Applikation: Hersteller-Hotfix einspielen oder betroffene Daemons in isolierte DMZ-Segmente überführen.
  • 3. Telemetrie & EDR-Alerts: Prozessaufrufe und Child-Processes auf anomale Shell-Spawns überwachen.
🔗 Semantisch verwandte Zero-Days MariaDB 11.7 VEC
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Refactoring 029 - Replace NULL With Collection

Thematisch verwandte Begriffe: Refactoring, Replace, NULL, With · 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-82585 | The Botslab G980H dash camera firmware transmits sensitive information o…
Advisory →
tsecurity.de Icon
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
📂 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 TTP ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...
↗ Original-Quelle