What is a programming paradigm?
A paradigm is a style or way of thinking about writing programs. The same problem can be solved in different styles. The computer does not care; people do, because a good style makes a program easier to read, test and change.
Our example problem: add the squares of the even numbers from 1 to 6. Answer: 2² + 4² + 6² = 4 + 16 + 36 = 56.
Most modern languages (Python, JavaScript, Java) allow more than one paradigm.
Imperative paradigm: commands and changing variables
An imperative program is a list of commands in order. It uses variables whose values change. This changing stored data is called state. The usual tools are sequence, selection (if) and loops. When commands are grouped into named procedures it is called procedural.
total = 0
for n in range(1, 7):
if n % 2 == 0:
total = total + n * n
print(total) # 56Strength: close to how the computer works and easy to follow step by step. Weakness: many changing variables make big programs hard to check.
Functional paradigm: functions in, functions out
A functional program is built from functions. A pure function gives the same output for the same input and changes nothing outside itself (no side effects). Data is not changed in place; a new value is made instead (immutable data).
- filter keeps items that pass a test.
- map does the same thing to every item.
- fold (or reduce, sum) joins all items into one value.
evens = filter(lambda n: n % 2 == 0, range(1, 7))
squares = map(lambda n: n * n, evens)
print(sum(squares)) # 56Strength: short, easy to test, safe when many things run together. Weakness: can feel unusual at first and is not always best for things that must change, like a game screen.
Object-oriented paradigm: objects that talk
An object-oriented program is made of objects. Each object keeps its own data (attributes) and its own actions (methods). A class is the blueprint; objects are made from it. Objects work together by sending messages (calling methods). Keeping data hidden inside the object is called encapsulation. A new class can reuse an old one (inheritance).
class Num:
def __init__(self, v): self.v = v
def is_even(self): return self.v % 2 == 0
def square(self): return self.v * self.v
class Sum:
def __init__(self): self.total = 0
def add(self, x): self.total += x
s = Sum()
for v in range(1, 7):
n = Num(v)
if n.is_even(): s.add(n.square())
print(s.total) # 56Strength: matches real things (players, bank accounts, buttons). Weakness: for tiny jobs it is more code than needed.
Another paradigm: declarative and logic
A declarative program says what result you want, not how to get it. SQL is an example: SELECT name FROM students WHERE marks > 90 does not say how to search. In logic programming (for example Prolog) you write facts and rules and ask questions; the system finds the answers by itself. Functional programming is also often called declarative.
An event-driven program waits for events (a click, a key) and runs a piece of code for each. Web pages work this way, often together with objects.
Writing and evaluating a program in a paradigm
To write a program in a paradigm, follow its thinking:
- Imperative: list the steps, name the variables, decide the loop.
- Functional: split the job into small pure functions and join them in a pipeline.
- Object: find the "things", give each data and actions, decide who sends messages to whom.
To evaluate (judge) your program, ask: Is the answer correct for several lists? Is it easy to read? Is it easy to test? Can I change it later without breaking things?
Tracing helps: write the variable values after each step. For the imperative code above, total goes 0 → 4 → 20 → 56.
Choosing a paradigm for a problem
No paradigm is best for everything. Weigh these:
- The problem: data transformations suit functional; many things with their own behaviour suit objects; strict order of actions suits imperative.
- Size and team: large teams like objects with clear boundaries.
- Safety and testing: pure functions are the easiest to test.
- Speed and memory: imperative code can be the most direct for the machine.
- Tools and skills: use what the language and the team know.
Real programs mix styles: objects for the structure, functions for the calculations and commands inside methods.
Key formulas and definitions
- Imperative = commands + changing variables (state)
- Functional = pure functions + immutable data; filter → map → fold
- Object-oriented = classes → objects (data + methods) + messages
- Declarative = say WHAT you want, not HOW (SQL, Prolog)
- Example check: sum of squares of evens in 1..6 = 4 + 16 + 36 = 56
Worked examples
1. Trace the imperative program for the list 1..6 and write total after every even number.
Start total = 0. n = 2: total = 0 + 4 = 4. n = 4: total = 4 + 16 = 20. n = 6: total = 20 + 36 = 56. The odd numbers 1, 3, 5 are skipped.
2. Write the functional steps for "double every number, then keep those bigger than 5" for [1, 2, 3, 4].
map doubles: [2, 4, 6, 8]. filter keeps > 5: [6, 8]. Order matters: map first, then filter. The original list [1, 2, 3, 4] is not changed.
3. A game has a Player, an Enemy and a Coin. Which paradigm fits and what could each one hold?
Object-oriented. Player: data = life, place; methods = move, jump. Enemy: data = life, speed; methods = chase, attack. Coin: data = value; method = collect. They send messages such as "attack(player)".
4. Compare the three solutions of the sum-of-squares problem in one line each.
Imperative: a loop changes the variable total. Functional: filter, map and fold make the answer with no variable changed. Objects: each Num object squares itself and sends the value to a Sum object. All three give 56.
Common mistakes
- Thinking a language belongs to only one paradigm. Python and JavaScript support all of the styles shown here.
- Believing functional code is always shorter or faster. It is often shorter, but speed depends on the problem.
- Saying "object-oriented means using objects" only. The idea is data and actions kept together, with messages between objects.
- Mixing order in a pipeline. Filter then map is different from map then filter.