Everything I Know
  • Hello
  • Meta
  • Computer Science
    • Overview
    • Algorithms
      • Analysis of Algorithms
      • Searching
      • Sorting
      • Greedy
      • String Processing (Pattern Matching)
      • Backtracking
      • Dynamic Programming
      • Divide and Conquer
      • Graph Algorithms
    • Data Structures
      • Linear Data Structures
        • Static
          • Arrays
        • Dynamic
          • Stack
          • Queues
          • LinkedLists
      • Non-Linear Data Structures
        • Trees
        • Graphs
    • System Design
    • Database
      • Theory
    • Artificial Intelligence
      • Machine Learning
      • Deep Learning
    • Compilers
    • Operating System
      • Overview
    • Software Engineering
      • Software Design and Architecture
        • SOLID Principles
          • Java Example
        • KISS Principles
        • Design Data-Intensive Applications
    • Programming
      • Languages
        • Java
        • C++
        • JavaScript
          • Cheatsheet
        • Python
      • Paradigms
        • Object-Oriented Programming
        • Functional Programming
        • Aspect-Oriented Programming
  • Web Development
    • Frontend Development
    • Backend Development
  • Data Science
    • Overview
    • Data Science
    • Data Analytics
    • Data Engineering
      • Big Data Engineering
    • Data Visualization
  • Education
    • Overview
    • Philosophy of Education
    • Pedagogies
    • Education Technology
      • Education Technology Landscape
  • UI&UX Design
    • User Interface
    • User Experience
  • Useful Software Products
    • Note Taking
    • Brainstorming
    • Automation
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. Computer Science
  2. Programming

Paradigms

PreviousPythonNextObject-Oriented Programming

Last updated 4 years ago

Was this helpful?

Programming paradigms are a way to classify based on their features. Languages can be classified into multiple paradigms.

Some paradigms are concerned mainly with implications for the of the language, such as allowing , or whether the sequence of operations is defined by the execution model. Other paradigms are concerned mainly with the way that code is organized, such as grouping a code into units along with the state that is modified by the code. Yet others are concerned mainly with the style of syntax and grammar.

Common programming paradigms include:

  • in which the programmer instructs the machine how to change its state,

    • which groups instructions into procedures,

    • which groups instructions with the part of the state they operate on,

  • in which the programmer merely declares properties of the desired result, but not how to compute it

    • in which the desired result is declared as the value of a series of function applications,

    • in which the desired result is declared as the answer to a question about a system of facts and rules,

    • in which the desired result is declared as the solution of an optimization problem

    • in which the desired result is declared with data streams and the propagation of change

Declarative programming is when you say what you want, and imperative language is when you say how to get what you want.

A simple example in Python:

# Declarative
small_nums = [x for x in range(20) if x < 5]

# Imperative
small_nums = []
for i in range(20):
    if i < 5:
        small_nums.append(i)

The first example is declarative because we do not specify any "implementation details" of building the list.

To tie in a C# example, generally, using LINQ results in a declarative style, because you aren't saying how to obtain what you want; you are only saying what you want. You could say the same about SQL.

One benefit of declarative programming is that it allows the compiler to make decisions that might result in better code than what you might make by hand. Running with the SQL example, if you had a query like

SELECT score FROM games WHERE id < 100;

the SQL "compiler" can "optimize" this query because it knows that id is an indexed field -- or maybe it isn't indexed, in which case it will have to iterate over the entire data set anyway. Or maybe the SQL engine knows that this is the perfect time to utilize all 8 cores for a speedy parallel search. You, as a programmer, aren't concerned with any of those conditions, and you don't have to write your code to handle any special case in that way.

References

programming languages
execution model
side effects
[1]
[2]
[3]
imperative
procedural
object-oriented
declarative
functional
logic
mathematical
reactive
https://en.wikipedia.org/wiki/Programming_paradigm
https://www.freecodecamp.org/news/imperative-vs-declarative-programming-difference/
https://www.youtube.com/watch?v=9bW8dp1M1Ac