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

17

u/[deleted] Aug 05 '19

The thing I've realized about "declarative coding" is that it's actually not a separate type of coding. There's no hard line there. In the way they're defined it feels like it is, but in practice, declarative code is basically higher-level code.

And of course, higher-level code is to be preferred. But the most important part about being able to write high-level code is to have the habit of writing your own "declarative" abstractions and high-level tools to express the rest of your code with.

Using what the language offers, like filters/map/reduce, LINQ and so on, that's great. But if you stop at what's built-in, you're missing vast opportunities in making your code higher-level.

Functional programming practitioners do this instinctively. They'd create for themselves highly composable high-order functions designed for a task, so they can express a program in a surprisingly tight and minimal way. Most mainstream programmers would just grunt through, copy-pasting samples from the web, and using whatever APIs are available out of the box.

So in a way, it's not that declarative coding makes you a better programmer as much as... being a better programmer is in large part about factoring your code declaratively (high-level).

1

u/[deleted] Aug 05 '19

Can you go a little more into what a functional programmer would do instinctively?

4

u/[deleted] Aug 05 '19 edited Aug 05 '19

It's a subtle topic, but I'll use a very small example to illustrate a principle. Functional programmers commonly use a principle called partial application, commonly through "currying", which take a highly general-purpose function with, say, 5-6 arguments, and produces a more specialized function with 2-3 arguments by providing "default" values for some of the arguments.

So this means you can have your complex to call general purpose function, but you don't have to call it directly in your code, except in the few places where you "partially apply" it for a specific purpose (in OOP similar to: Facade pattern).

In functional programming two trends stand out that I don't see as often in OOP, and not because OOP can't do that, on the contrary: it's built for it; rather it's about culture and experience.

One is, when you're building general purpose code make it extremely flexible through closures taking closures (Strategy pattern, Decorator pattern, Proxy pattern, etc. in OOP for ex.), returning closures (Factory pattern, Builder pattern etc.). Note that objects/closures are interchangeable units of code in these examples.

Two is, you take these very general purpose, sometimes complex closures/functions, and then you produce simpler "wrappers" for a smaller part of your object. Then you proceed to use these "wrappers" for most of your code, not the general purpose (and much more verbose and complex) APIs.

And then of course there's a third concept, which is using very simple, very universal interfaces where possible, so you can compose vastly different units of code together into solutions through their shared simple interfaces (in functional programming one example is monads, well LINQ is basically monads, so are streams in Java).

Sorry if this sounds fuzzy, it's something best understood in practice, not in theory.

EDIT: Slight terminology mix-up.