defensive-programming

Enforce defensive coding principles across C++, Java, Python, and Rust projects.

1|Updated Mar 22, 2026
One-click install
npx skills add https://github.com/tswr/engineering-mastery-plugin --skill defensive-programming-tswr
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: defensive-programming
Source: https://github.com/tswr/engineering-mastery-plugin/tree/main/skills/defensive-programming
Command: npx skills add https://github.com/tswr/engineering-mastery-plugin --skill defensive-programming-tswr

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill solves the pervasive pain of writing code that is prone to accidental misuse, silent state corruption, and hard-to-reproduce bugs in large, long-lived codebases, where unenforced invariants and overly permissive access lead to costly maintenance, unexpected outages, and security gaps from unintentional errors.

Core Features & Use Cases

  • Defensive Coding Principles: Enforces immutability by default, minimal visibility, explicit design by contract, boundary validation, and resource safety to eliminate entire classes of common bugs.
  • Language-Specific Idioms: Provides idiomatic implementation patterns for C++, Java, Python, and Rust, so you can apply defensive practices without fighting your language's standard conventions.
  • Real-World Use Case: When building a public SDK used by external teams, use this Skill to ensure all public APIs have strict preconditions, internal state is fully protected from accidental modification, and resources are always cleaned up properly to prevent leaks in long-running processes.

Quick Start

Use the defensive-programming skill to refactor the existing UserService class to make all internal fields immutable, restrict all helper methods to private visibility, and add precondition assertions to the public create_user() method.

Frequently Asked Questions about defensive-programming

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I prevent silent state corruption and accidental misuse in my codebase?

Prevent silent state corruption by enforcing defensive programming principles like immutability by default, minimal visibility, and boundary validation. This eliminates accidental misuse and hard-to-debug bugs by strictly protecting internal state from modification.

Can I apply design by contract and immutability patterns across different programming languages?

Yes, design by contract and immutability patterns apply across C++, Java, Python, and Rust. The defensive programming approach provides idiomatic implementation patterns for each language so you can enforce preconditions and boundary validation without fighting standard conventions.

What's the best way to ensure deterministic resource cleanup and prevent leaks in long-running processes?

Ensure deterministic resource cleanup by applying defensive resource safety principles during class design and API construction. This enforces strict cleanup protocols that prevent resource leaks and reduce unexpected outages in long-running processes.

Does defensive programming work for building public SDKs used by external teams?

Defensive programming works effectively for public SDKs by enforcing strict preconditions on public APIs, protecting internal state with minimal visibility, and adding boundary validation. This prevents external teams from accidentally breaking invariants or triggering runtime failures.

How do I refactor an existing class to make internal fields immutable and restrict helper methods?

Refactor existing classes by making all internal fields immutable, restricting helper methods to private visibility, and adding precondition assertions to public methods. This eliminates accidental state modification and enforces strict design by contract.

Why should I enforce minimal visibility and boundary validation in my software architecture?

Enforce minimal visibility and boundary validation to reduce long-term maintenance costs and prevent runtime failures. These defensive coding principles eliminate entire classes of common bugs by ensuring only explicitly permitted access can modify state.