Programming Paradigms
A programming paradigm is a fundamental style or approach to programming. Understanding different paradigms helps you choose the right tool for the job and write more effective code.
Major Paradigms
+---------------------------------------------------------+
| PROGRAMMING PARADIGMS |
+---------------------------------------------------------+
| |
| IMPERATIVE |
| - Step-by-step instructions |
| - Changes program state directly |
| - Examples: C, Pascal, BASIC |
| |
| OBJECT-ORIENTED |
| - Organizes code into objects |
| - Encapsulates data and behavior |
| - Examples: Java, C++, Python |
| |
| FUNCTIONAL |
| - Treats computation as math functions |
| - Avoids mutable state and side effects |
| - Examples: Haskell, Lisp, Erlang |
| |
| DECLARATIVE |
| - Describes WHAT to compute, not HOW |
| - Examples: SQL, HTML, CSS |
| |
+---------------------------------------------------------+
Imperative Programming
Imperative programming tells the computer exactly how to do something through a sequence of statements that change program state. It is the most common paradigm and closely mirrors how computers actually work.
// Imperative approach
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum = sum + i;
}
// sum now contains 55
Object-Oriented Programming
OOP organizes software around objects โ entities that combine data (attributes) and behavior (methods). It promotes encapsulation, inheritance, and polymorphism.
class BankAccount {
private double balance;
public void deposit(double amount) {
this.balance += amount;
}
public double getBalance() {
return this.balance;
}
}
Functional Programming
Functional programming treats computation as the evaluation of mathematical functions. It avoids changing state and mutable data, leading to code that is often easier to test and reason about.
// Functional approach
List<Integer> numbers = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
int sum = numbers.stream()
.mapToInt(Integer::intValue)
.sum();
Key Takeaways
- Each paradigm has strengths suited to different problems
- Most modern languages support multiple paradigms
- Understanding paradigms helps you write more effective code
- Mixing paradigms is common in real-world projects