The Foundation of Clean Code

Master Software Contracts

Learn why understanding pre-conditions, post-conditions, and invariants is the foundation of all good codeβ€”and how it eliminates the need for defensive programming

The Problem

Most Code Is Drowning in Defensive Checks

Null checks everywhere. Validation scattered across codebases. Business logic buried under 40 lines of defensive programming. The type system provides zero help.

57 lines: 42 validation, 15 business logic

The Solution

Understand Contracts

When you understand pre-conditions, post-conditions, and invariants, defensive programming becomes unnecessary. Validation happens once. Types guarantee correctness. Business logic becomes clear.

8 lines: 0 validation, 8 business logic

The Three Pillars of Correctness

1

Pre-conditions

What must be true before an operation executes. Encode them in types, not runtime checks.

withdraw(PositiveAmount)
// Type enforces positivity
2

Post-conditions

What the operation guarantees after execution. Make them explicit in return types.

Result<Success, Error>
// No silent failures
3

Invariants

Properties that remain true. Maintain by construction, protect with encapsulation.

balance >= 0
// Structurally impossible to violate
🎯

Types Encode Pre-conditions

Instead of checking if a quantity is positive in every method, create a PositiveInteger type that cannot represent non-positive values.

❌ if (quantity <= 0) throw
βœ… Quantity.of(value)
// validates once

πŸ”’

Invariants By Construction

Maintain invariants through immutability and encapsulation, not scattered runtime checks. Make invalid states impossible to construct.

❌ if (balance < 0) fix
βœ… Money type prevents it
// structurally impossible

πŸ“‹

Explicit Post-conditions

Return types that explicitly represent success or failure. No null returns. No exceptions for control flow. Clear contracts in signatures.

❌ User findUser() // null?
βœ… Result<User, NotFound>
// explicit contract

πŸ—οΈ

Layered Type Systems

Build foundational types that enforce constraints once, then compose them into domain types without re-validation. Each layer builds on guarantees below.

NonEmptyString β†’ ProductId
PositiveInt β†’ Quantity
// compose without checks

But First, You Need to See What's Missing

Before you can encode contracts in types, you need to develop the fundamental skill most developers lack: recognizing missing contracts in code

That's what this training system does. Through 60 real-world examples, you'll train your eye to instantly spot missing pre-conditions, unclear post-conditions, and violated invariants.

Train Your Contract Recognition Skills

Learn to identify what's missing before bugs reach production

You'll Learn to Identify

?

Missing Pre-conditions

What must be true before this code can safely execute?

  • β€’ Is the parameter null?
  • β€’ Is the array empty?
  • β€’ Is the divisor zero?
βœ“

Unclear Post-conditions

What does this code guarantee about its output?

  • β€’ Can it return null?
  • β€’ What type is the result?
  • β€’ What's the range of values?
=

Violated Invariants

What properties should always hold?

  • β€’ Balance never negative?
  • β€’ Array always sorted?
  • β€’ Size matches count?
!

Unhandled Edge Cases

What boundary conditions break this code?

  • β€’ Empty collections?
  • β€’ Overflow/underflow?
  • β€’ Race conditions?

Progressive Training System

1

Basic Checks

Null checks, empty arrays, division by zero

user.name, items[0]
12 questionsβ€’3 ⭐
2

Type Safety

Type mismatches, property access

password.length
12 questionsβ€’3 ⭐
3

Collections

Array bounds, sorting, uniqueness

items.sort()
12 questionsβ€’3 ⭐
4

State Machines

Invalid transitions, ordering

order.ship()
12 questionsβ€’3 ⭐
5

Concurrency

Race conditions, atomicity

pool.acquire()
12 questionsβ€’3 ⭐

See a Real Question

Each question presents buggy code and asks you to identify what's missing

JAVASCRIPT β€’ LEVEL 1EASY

Array First Element

function getFirst(items) {
    return items[0];
}

What could go wrong with this code?

βœ“

items might be null or undefined

Pre-condition violation - would crash when accessing [0]

βœ“

items might be empty

Edge case - empty array returns undefined

βœ—

items must be sorted

No sorting requirement - we just return first element

Real-world impact: E-commerce site crashed when cart items array was empty but code assumed there was always at least one item.

πŸ”

Develop Code Review Instincts

Learn to instantly spot missing contracts during code review. Your team will write fewer bugs because you'll catch them before merge.

Before: "Looks good to me" βœ“
After: "What if user is null?" 🎯

πŸ›

Write Code With Proper Contracts

Understanding what's missing is the first step toward encoding contracts in types. Once you see the pattern, you'll design better from the start.

Pattern: Spot contract β†’ Encode in type β†’ Bug impossible

⚑

Debug Faster

When production breaks, you'll immediately know where to look: which pre-condition was violated? Which invariant doesn't hold?

NullPointerException
β†’ Missing pre-condition check

πŸ“š

Learn From Real World Cases

Every question includes a real-world example of how that missing checks caused production issuesβ€”learn from others' mistakes.

"Connection pool corruption when same connection returned to pool twice"

Practice in Multiple Languages

Contract principles are universal. See how pre-conditions, post-conditions, and invariants apply across:

PythonJavaScriptJava
πŸ›
60
Questions to Analyze
πŸ“ˆ
5
Progressive Difficulty Levels
⭐
15
Stars to Master
🎯
100%
Contract Recognition

Master the Foundation of All Good Code

Train your eye to spot missing contracts. Write cleaner code. Catch bugs before production. Start your journey from defensive programming to contract-based design.

No signup β€’ No installation β€’ 60 questions β€’ Start learning immediately