Programming Paradigms

The major styles of organizing programs, what each is good at, and how modern languages mix them.

What is it?

A paradigm is a style of thinking about and structuring a program. It shapes what you consider the basic building blocks and how they fit together.

- Imperative: tell the computer how, step by step, by changing state. Loops, assignments, mutable variables. - Procedural: imperative code organized into procedures (functions) that operate on data. C, Pascal, much of Go. - Object-oriented: bundle data with the methods that operate on it into objects; use encapsulation, polymorphism and (sometimes) inheritance. Java, C#, Smalltalk. - Functional: build programs from pure functions and immutable data; avoid side effects; treat functions as values. Haskell, Clojure, Elm, F#. - Declarative: describe what you want, not how to compute it. SQL, HTML, CSS, regular expressions, Terraform. Functional and logic programming are declarative styles. - Logic: state facts and rules, then ask questions; the engine searches for answers. Prolog, Datalog. - Reactive: model programs as streams of values over time and declare how outputs depend on inputs; changes propagate automatically. RxJS, spreadsheets, signals in UI frameworks.

Most popular languages are multi-paradigm. Python, JavaScript, Kotlin, Scala, Rust and C# all let you mix procedural code, objects and functional techniques. The useful skill is not picking a camp but recognizing which style fits each part of a problem.

Explain like I'm 10

Getting someone to your house: imperative is turn-by-turn directions. Object-oriented is giving them a car that knows how to drive itself to addresses. Functional is a set of transformations on a map that output a route. Declarative is just giving the address and letting the GPS work it out.

Examples

Same task, three styles: total of squares of even numbers (Python)

numbers = [1, 2, 3, 4, 5, 6]

# Imperative / procedural: explicit steps and a mutable accumulator
total = 0
for number in numbers:
    if number % 2 == 0:
        total += number * number

# Functional: compose transformations, no mutation
total = sum(map(lambda n: n * n, filter(lambda n: n % 2 == 0, numbers)))

# Declarative-ish comprehension: describe the result
total = sum(n * n for n in numbers if n % 2 == 0)   # 56 in every case

Declarative and logic: describe the result, not the steps

-- SQL: what rows you want; the query planner decides indexes, join order, etc.
SELECT customer_id, SUM(amount) AS total_spent
FROM orders
WHERE created_at >= DATE '2026-01-01'
GROUP BY customer_id
HAVING SUM(amount) > 1000;

-- Prolog (logic): facts, a rule, and a question
-- parent(alice, bob).  parent(bob, carol).
-- grandparent(X, Z) :- parent(X, Y), parent(Y, Z).
-- ?- grandparent(alice, Who).     % Who = carol

Neither snippet says how to loop or search. The database and the Prolog engine choose the execution strategy.

How it works

Paradigms differ mainly in how they handle state and control flow:

  • Imperative/procedural: structured as statements and procedures; state lives in mutable variables; control flow is an explicit sequence and loops.
  • Object-oriented: structured as objects with methods; state is encapsulated in objects; control flow is method calls and polymorphic dispatch.
  • Functional: structured as pure functions; state is immutable values; control flow is composition, recursion and higher-order functions.
  • Declarative: structured as descriptions or queries; state is hidden from you; the engine decides the control flow.
  • Logic: structured as facts and rules; state is a knowledge base; control flow is search and unification.
  • Reactive: structured as streams or signals; state is values over time; control flow is propagation of changes.

Trade-offs. - Imperative code maps closely to the hardware and is easy to follow step by step, but shared mutable state grows hard to reason about. - OOP is good at modelling entities with identity and lifecycle and at hiding implementation details, but deep inheritance hierarchies become rigid. - Functional code is easy to test and parallelize because pure functions have no hidden inputs or outputs, but real programs still need effects (I/O, time), which have to be pushed to the edges. - Declarative code is concise and lets an engine optimize, but you give up control and must understand the engine when performance matters.

A common mix is "functional core, imperative shell": pure functions for business logic, with a thin outer layer doing I/O and mutation.

Imperative                     Declarative
     (how: steps & state)           (what: describe the result)
        /            \                 /         |          \
  Procedural   Object-oriented    Functional   Logic   Reactive/dataflow
    (C)       (Java, C#, Smalltalk) (Haskell)  (Prolog)  (RxJS, spreadsheets)

  Multi-paradigm: Python, JavaScript/TypeScript, Kotlin, Scala, Rust, C#, Swift

Why does it exist?

Different problems have different natural shapes. Simulations and UIs have long-lived entities (OOP). Data transformations are pipelines (functional). Querying data is best described, not programmed (declarative). Rules engines and type checkers are search problems (logic). Each paradigm grew from a field where it made a class of problems much simpler, and languages borrowed from each other once those ideas proved themselves.

When to use it

- Procedural for scripts, straightforward algorithms, and performance-critical loops. - OOP for domains with stateful entities, plug-in architectures, and long-lived systems where encapsulation protects invariants. - Functional for data transformation, concurrency (immutable data is safe to share), and business rules you want to unit test easily. - Declarative for querying (SQL), layout and styling (HTML/CSS), infrastructure (Terraform), and configuration. - Reactive for UIs and event streams where many outputs depend on changing inputs.

When not to use it

- Don't force one paradigm everywhere: an "everything is a class" design for a data pipeline, or pure functional gymnastics for a simple stateful script. - Avoid heavy reactive stream code for simple one-off async calls; a plain async function is easier to read. - Avoid deep recursion as a loop replacement in languages without tail-call optimization (Python, Java, most JavaScript engines); use iteration. - Don't fight a team's or language's idioms; idiomatic code in a mixed style usually beats "pure" code in a style the language barely supports.

Common mistakes

  • Treating paradigms as rival religions instead of tools; most real codebases benefit from a mix.

  • Equating OOP with inheritance. Encapsulation and polymorphism matter more; composition often beats inheritance.

  • Calling code 'functional' because it uses map and filter while the callbacks still mutate shared state.

  • Assuming declarative means 'no need to understand execution'. Slow SQL queries are fixed by understanding the planner and indexes.

  • Mixing paradigms inconsistently inside one module so readers can't predict where state lives.

Practice exercises

  1. Easy:

    Given a list of orders, compute the total revenue per customer three ways: an imperative loop, a functional pipeline, and an SQL query over an equivalent table.

  2. Medium:

    Model a bank account first in OOP style (class with deposit/withdraw that mutates balance) and then in functional style (pure functions returning new account values). Compare how you'd test each and how each behaves when two threads use it.

  3. Hard:

    Take a small program that mixes I/O and logic (e.g. reading a CSV, computing statistics, writing a report) and restructure it as a 'functional core, imperative shell'. Write unit tests for the core that need no files or mocks.

Interview questions

What is the difference between imperative and declarative programming?

Imperative code specifies how to compute a result through a sequence of state-changing steps. Declarative code specifies what result you want and leaves the how to an engine or runtime. Loops are imperative; SQL queries and CSS rules are declarative.

What are the core ideas of object-oriented programming?

Encapsulation (bundling data with behaviour and hiding internals), abstraction (exposing a simple interface), polymorphism (one interface, many implementations), and inheritance as one, often overused, way to share behaviour. Composition is frequently preferred to inheritance.

What makes a programming style functional?

Pure functions without side effects, immutable data, functions as first-class values (passed and returned), composition of small functions, and expressions rather than statements. The payoff is referential transparency: a call can be replaced by its result.

What is a multi-paradigm language? Give examples.

A language that supports several paradigms well, letting you choose per problem. Python, JavaScript/TypeScript, Kotlin, Scala, C#, Swift and Rust all mix procedural, object-oriented and functional features.

What is logic programming?

Programs are sets of facts and rules; you ask queries and the engine finds bindings that satisfy them through unification and backtracking search. Prolog and Datalog are examples; it's used in rule engines, type inference, program analysis, and some query languages.

What is reactive programming?

Programming with data streams and automatic propagation of change: you declare how values depend on others, and when an input changes, dependents update. Spreadsheets, RxJS observables, and signals in UI frameworks are examples.

Why is functional programming considered good for concurrency?

Immutable data can be shared between threads without locks because nobody can change it, and pure functions have no hidden shared state, so they can run in parallel safely. Effects are isolated, so the remaining synchronization points are few and explicit.

What does 'functional core, imperative shell' mean?

Put decisions and business logic in pure functions that take values and return values, and keep I/O, mutation and time in a thin outer layer that calls them. The core is trivially unit-testable; the shell is small enough to cover with a few integration tests.

Is SQL a programming language, and which paradigm is it?

Yes. Standard SQL with recursive CTEs is Turing complete. It's primarily declarative: you describe the desired result set, and the query planner chooses access paths, join algorithms and order.

How do you choose a paradigm for a new component?

Look at the problem's shape: stateful entities with invariants suit objects; transformations and rules suit pure functions; queries and configuration suit declarative tools; event-driven UIs suit reactive. Then weigh language idioms and team familiarity, and keep the choice consistent within a module.