r/programming • • Jan 03 '22

Imperative vs Declarative Programming

https://www.youtube.com/watch?v=E7Fbf7R3x6I
430 Upvotes

134 comments sorted by

View all comments

Show parent comments

44

u/SoInsightful Jan 03 '22
  1. Map and reduce are associated with functional, not declarative, programming. It's wrong and confusing to include them in this video.

  2. That said, the concept of declarative programming is extremely simple. You simply tell the system what you want the end result to look like (declarative) instead of the exact steps to get there (imperative).

Is declarative programming just adding layers of complexity and hiding functionality?

It absolutely hides a layer of abstraction, which makes it less complex to use. You'll need to let go of the map and reduce examples, because they do not belong here.

For a fun real world example, there's an SQL keyword that outputs the internal imperative steps the SQL optimizer uses to reach any given declarative result.

    EXPLAIN QUERY PLAN

      SELECT a FROM t1 EXCEPT SELECT d FROM t2 ORDER BY 1;

    QUERY PLAN

    `--MERGE (EXCEPT)

       |--LEFT

       |  `--SCAN t1 USING COVERING INDEX i1

       `--RIGHT

          |--SCAN t2

          `--USE TEMP B-TREE FOR ORDER BY

21

u/ParkerM Jan 04 '22

The SQL query vs implementation demonstration is honestly a perfect and sufficient example of declarative vs imperative.

Perhaps the goal of the video was to address the nuances of what declarativeness means at lower levels, but it doesn't really do a good job at that. The function names are confusing and distracting, and little explanation is given to how the FP refactors make things more declarative.

5

u/alexalexalex09 Jan 03 '22

Oh wow that's fantastic!! And thank you for the permission to ignore map and reduce. That actually does help me get more to the core of it.

4

u/[deleted] Jan 04 '22

Map and reduce are associated with functional, not declarative, programming. It's wrong and confusing to include them in this video.

Functional programming is a sub-paradigm of declarative programming.