Two Ways of Thinking
Lesson 1: Two Ways of Thinking
Before you write the first line of any non-trivial program, you've already made a decision โ whether you know it or not. You've picked a programming paradigm: a mental model for how programs should be structured. Every paradigm answers two questions:
- Where does behavior live?
- How does data move through the program?
Object-oriented programming (OOP) and functional programming (FP) give opposite answers โ and both answers are right, in the right context.
The Spectrum: Imperative โ Declarative
These two answers sit on a deeper spectrum. Imperative code tells the computer how to do something, step by step. Declarative code describes what you want and lets the machinery figure out the steps. SQL, HTML, and regular expressions are declarative: you state the result, not the loop. FP leans hard toward the declarative end.
A (Very) Brief History
- 1940sโ70s ยท Procedural: Fortran, C. Step-by-step instructions, functions that mutate global state.
- 1950sโ70s ยท Seeds of both: Lisp introduces first-class functions (FP). Smalltalk invents objects and messages (OOP).
- 1980sโ90s ยท OOP goes mainstream: C++, Java โ "everything is an object" becomes the enterprise default.
- 2000sโnow ยท The FP renaissance: Haskell, Scala, F#, Elixir go production-grade, and FP features (map, reduce, immutability, pattern matching) invade JavaScript, TypeScript, Python, Kotlin, Rust, and even Java's streams.
Why the comeback? Three forces: multi-core CPUs (shared mutable state is the enemy of parallelism), distributed systems (the web is one giant pipeline of transformations), and a testing culture that rewards pure, deterministic logic.
๐ง Knowledge Check
1. In the OOP mental model, a program is best described as:
2. Which statement is true of declarative code?
3. Why has functional programming seen a renaissance in the last two decades?