Programming paradigms
A paradigm is a style of thinking about programs. Most modern languages support several, and knowing them helps you choose the clearest way to express a solution.
Imperative and procedural programming
Imperative programming tells the computer step by step how to change the program's state. Procedural programming organises imperative code into procedures (functions). C is a classic procedural language. This style mirrors how the processor actually works, which makes it efficient and easy to follow for simple tasks.
Object-oriented programming
Object-oriented programming (OOP) organises code around objects, which bundle data (attributes) and behaviour (methods). A class is a blueprint; objects are instances of it. A BankAccount class might have a balance attribute and deposit and withdraw methods. Java, C#, and Python all support OOP.
Encapsulation, inheritance, and polymorphism
Encapsulation hides an object's internal details behind a public interface, so other code cannot put it into an invalid state. Inheritance lets a class reuse and extend another; a SavingsAccount can inherit from BankAccount and add interest. Polymorphism lets different classes respond to the same method call in their own ways, such as each shape class providing its own area method.
Functional programming
Functional programming builds programs from functions that, ideally, have no side effects: the same inputs always give the same outputs, and nothing outside the function changes. Data is often immutable. Functions can be passed as values, such as map, which applies a function to every item in a list. Haskell is purely functional; JavaScript and Python borrow many functional ideas.
Declarative programming
Declarative languages describe what result is wanted rather than how to compute it. SQL is a major example: a query such as SELECT name FROM students WHERE grade > 90 states the desired data and leaves the method to the database. HTML is declarative too, describing a page's structure rather than drawing steps.
Event-driven programming
In event-driven programs, code runs in response to events such as clicks, key presses, timers, or incoming messages. Programmers write handlers, functions attached to events. Graphical interfaces and web pages work this way: the program mostly waits, then reacts. JavaScript in browsers is heavily event-driven.
Choosing languages
No language is best for everything. Python is popular for learning, data science, and scripting; JavaScript runs in every web browser; Java and C# power large business systems and Android apps; C and C++ suit operating systems, games, and embedded devices; Rust offers C-like speed with stronger memory-safety guarantees. Most professionals learn several.
Worked example
Doubling every number in [1, 2, 3] in three styles. Imperative: create an empty list, loop through each number, and append the number times 2. Functional: map a doubling function over the list, giving [2, 4, 6] without changing the original. Declarative SQL, for a table of numbers: SELECT value * 2 FROM numbers. All give the same result; they differ in how the intent is expressed.
Key takeaways
Imperative and procedural code gives step-by-step instructions that change state. OOP bundles data and methods into objects made from classes, using encapsulation, inheritance, and polymorphism. Functional programming favours pure functions, immutability, and passing functions as values. Declarative languages such as SQL and HTML say what, not how. Event-driven programs react to events with handlers. Different languages suit different jobs, and most support several paradigms.
Flashcard
Style of programming
Check your understanding
A SQL query states which rows it wants and leaves the method to the database. Which paradigm is this?
