Two programmers can solve the same problem in very different ways. A paradigm is the way of thinking that shapes how the program is organised: what the main building blocks are and how data and code fit together.
A programming paradigm is a style or way of organising a program. Two paradigms are compared in this unit.
Procedural programming
The program is a collection of procedures (functions) that operate on data. Data and functions are separate. The program is designed by breaking the task into functions and calling them in order. Examples: C, Pascal, early BASIC.
// Procedural style: data and functions are separate
static double balance = 1000;
static void deposit(double amount) {
balance = balance + amount;
}
static void withdraw(double amount) {
if (amount <= balance) balance = balance - amount;
}Problem: any part of the program can change balance directly. With hundreds of accounts you need hundreds of variables or arrays, and one mistake anywhere can corrupt the data.
How a procedural program is designed
Procedural design is top-down: you take the whole task and break it into smaller sub-tasks, then break those into smaller ones again, until each piece is simple enough to write as one function. For a payroll program: calculate payroll becomes read hours, calculate pay, calculate tax and print payslip. The main program then calls the functions in order.
This works very well for small and medium programs, and it is easy to understand because the code follows the steps of the task. The problems start when the program grows:
- Shared data: many functions read and change the same global variables. If the balance is wrong, any one of dozens of functions could be the cause.
- Ripple effects: if the way data is stored changes (for example, the balance becomes an array of accounts), every function that uses it must change too.
- Weak link to the real world: the code is organised around steps, but users think in terms of things: customers, accounts, orders.
Object-oriented programming (OOP)
The program is a collection of objects. Each object bundles its own data (attributes) and the functions (methods) that work on that data. Objects communicate by calling each other's methods. Examples: Java, C++, C#, Python.
class BankAccount {
private double balance; // data
void deposit(double amount) { // behaviour
balance = balance + amount;
}
void withdraw(double amount) {
if (amount <= balance) balance = balance - amount;
}
}Each account is a separate object with its own balance, and outside code can only change it through the methods.
How an object-oriented program is designed
OOP design starts by asking what things (objects) take part in this problem? and then what does each one know, and what can each one do? A useful trick is to underline the nouns in the problem description (they are candidate classes and attributes) and the verbs (candidate methods).
"A customer opens an account, deposits money and withdraws money. The bank checks that the balance never goes below zero."
The nouns give the classes Customer and Account, and the attribute balance. The verbs give the methods open, deposit and withdraw. The rule "never below zero" belongs inside withdraw, where it is checked every time.
In the BankAccount example above, balance is private, so the only way to change it is through deposit() and withdraw(). If a balance is ever wrong, you only have to look in one class. To create a thousand accounts you simply create a thousand objects: new BankAccount(), each with its own balance.
Real-life analogy: a procedural kitchen has one shared fridge that every cook takes from and puts into, so when something goes missing anyone could be responsible. An object-oriented kitchen gives each cook their own labelled station: to get something from a station you must ask its cook.
Neither paradigm is "always better"
Procedural programming is still the right choice for short scripts, small utilities and performance-critical system code (much of an operating system is written in C). OOP shines in large programs that are developed by teams and changed for years: business systems, games, mobile and desktop apps. Many modern languages, including Java, Python and C#, let you use both styles. In Java, even a "procedural" program must live inside a class, and main is a static method.