r/programming • • Aug 05 '19

Why Declarative Coding Makes You a Better Programmer

http://minborgsjavapot.blogspot.com/2019/08/why-declarative-coding-makes-you-better.html
0 Upvotes

15 comments sorted by

View all comments

7

u/Zardotab Aug 05 '19 edited Aug 05 '19

I've often found declarative and functional (D/F) code difficult to debug, and others have shared this observation. With imperative code it's usually easier to examine the intermediate "parts". With D/F you often have to use trial-and-error to tease out the problem by removing constraints or functions within the "function chain" incrementally.

When I bring this up to D/F proponents, they either say something like, "learn to code better so you don't have to debug", or recommend splitting things up into mini-functions or similar. But that often makes for messy or cluttered code, at least to my eyes. Plus, with such splitting, it is still harder to debug than imperative.

I've been in many long debates on this. Some things are subjective, and one has to be careful about extrapolating their own mind to other minds. So far, imperative code is the easiest to x-ray to most. If you are an elite coder of a Sheldon Cooper level, then maybe you can debug just by glancing at the code. But us mortals need the ability to easily dissect problem code at finer levels.

As far as speed improvements, your application code should not be chomping on 10,000+ records; that's the job of the database. If you are reinventing database operations in your app code, then you are probably doing something wrong. I will agree that some domains are inherently speed-constrained or can't use a database. That's fine, but realize you may be trading debug-ability for speed.

I've been leaning toward the idea of using declarative to get a "draft list", and then imperative to "decorate" it with nitty gritty domain specifics. In other words, use declarative when the filters or behaviors are clear-cut and simple, but use imperative to tweak the intermediate results with specialized imperative code that does things like complex conditionals, parsing, string conversion, etc. That way each does what it does best (D/F and imperative).

Overall, I find imperative better at nitty-gritty logic. If you have a clean domain where the "rules" are defined by the laws of physics or good well-factored technical standards, D/F may shine more often. But if the domain is driven by whims of the legal system, office politics, marketers chasing fads, etc., then imperative may better handle all the micro-tweaking needed.

In general, use D/F to do forest-level processing/filtering, but imperative to do tree-level processing/filtering. (I don't mean trees in the data-structure sense). Use-The-Right-Tool-For-The-Job. The code may resemble something like this SQL-ish pseudocode:

SELECT *      
FROM   employees AS e 
  JOIN offices AS o 
    ON   e.officeID = o.ID
WHERE  /* functional-style actions */
    e.salary > 80000 AND o.region = 7
LOOP   /* imperative actions */
    IF e.status = 13 AND o.closedDate NOT NULL
    AND myOwnFunction(e, o) 
    THEN 
      e.adjustedResult = 'foo';   /* generated output column */
      e.lastName = 'Redacted'     /* altered output column */
    END IF
    IF myOwnFunction2(e) THEN
      EXCLUDE RECORD    /* exclude this row from result set */
    END IF
END LOOP