Contents

Computer Science › Compilers & Languages

Programming Paradigms

Styles of programming: imperative, object-oriented, functional, declarative.

Also known as: programming paradigms, paradigm, imperative, object-oriented, functional

A programming paradigm is a style, or way of thinking, about how to structure a program. Most languages support several, and real code mixes them.

ParadigmCore ideaExamples
Imperative / procedurala sequence of commands that change state, organized into proceduresC, shell scripts, much everyday Python
Object-orientedobjects bundle data and behavior, and communicate by calling methods (OOP)Java, C#, Python, Ruby
Functionalbuild programs from functions that avoid shared mutable state (functional programming)Haskell, Elixir, and parts of JavaScript and Python
Declarativedescribe the result you want, and let the system work out how (declarative vs imperative)SQL, HTML, configuration files
Event-driven / reactivethe flow is controlled by events and callbacksUI code, Node.js servers
Logicstate facts and rules, and let an engine search for answersProlog
# The same task in two styles
total = 0
for n in nums:                    # imperative
    if n % 2 == 0:
        total += n

total = sum(n for n in nums if n % 2 == 0)     # functional/declarative flavor

Why learn about them

  • They give you more tools. Some problems fit objects, some fit data transformation, some fit rules.
  • You can read more code. Frameworks lean on a style: React is functional and declarative, Java frameworks are object-oriented.
  • Languages differ in what they encourage, and understanding the paradigm explains the language’s idioms.

Don’t be dogmatic

No paradigm is best. Modern languages are multi-paradigm, and good engineers choose the style that suits each part: a pure functional core, objects at the boundaries, declarative queries for data. Learn the ideas, and let the problem decide.