Imperative and Declarative
Implicit and Declarative
Programming paradigms serve as guiding principles, delineating different styles of constructing software code. Among these paradigms, two stand out as foundational: imperative programming and declarative programming.
Imperative Programming
"The Classic Paradigm": Rooted in the earliest days of computing, imperative programming forms the basis of traditional programming languages like Pascal, C and assembly languages. At its core, it revolves around issuing explicit commands or instructions to direct the computer on how to accomplish a task, reflecting its etymological roots in the Latin word "imperare," meaning "to command."
Within imperative programming, several paradigms emerge, each refining the approach to writing and organizing code:
- Structured Programming: Introduces control structures like loops and conditionals to enhance code readability and maintainability.
- Procedural Programming: Modularizes code by encapsulating algorithms into functions or procedures, paving the way for better code reuse and abstraction.
- Modular Programming: Extends the procedural paradigm to larger software projects by breaking down code into logical, independent modules for improved clarity and manageability.
Declarative Programming
Paradigm of the Recent Past In contrast to the explicit directives of imperative programming, declarative programming presents a paradigm shift towards expressing what the program should achieve rather than how to achieve it. This approach, emerging as a response to evolving hardware (back in the days), emphasizes succinctness and abstraction, often resulting in code that is more concise but potentially harder to grasp.
Instead of telling the programm what to do, step by step (imperative), the program is asked a question (declarative). The parade example here would be a SQL query, that asks, "are there any users with the name X?".
The two primary methodologies within declarative programming are:
- Functional Programming: Centers around the evaluation of mathematical functions and emphasizes immutability and higher-order functions. Programs are composed of function calls and transformations, facilitating elegant solutions to complex problems.
- Logic Programming: Grounded in mathematical logic, logic programming represents programs as collections of facts and rules. Instead of specifying step-by-step procedures, developers define relationships and constraints, allowing the interpreter to derive solutions from logical deductions.
While imperative programming adheres to the tradition of instructing computers through explicit commands, declarative programming embraces a more abstract and concise approach, defining the desired outcome without prescribing the exact steps to achieve it. Both paradigms offer distinct advantages and have contributed significantly to the diverse landscape of software development.