DEV Community

Discussion on: Imperative vs Declarative programming. Your enemy is not object-oriented programming.

Collapse
 
aminmansuri profile image
hidden_dude • Edited

Many forget that the original OO language Smalltalk (Simula was half backed) borrowed heavily from functional programming. In fact, one could argue it was a functional language.

Smalltalk, even more so that Lisp, had no IF statement and no Loop statements. Everything was implemented using blocks (today known as Lambdas). For example:


value ifTrue: [  .. do something true...]
           ifFalse: [ ... do something false...]

In that sense smalltalk was "declarative style" as you call it. (Though not truly declarative like Haskell or SQL)

I think another thing that causes the functional camp to worry about OO is the question of mutability and side effects. One can mitigate that in OO programs by using immutable objects when possible.

However, the attitude among some that are now learning functional programming that they can set aside principles such as code duplication, modularization, and encapsulation just betrays a lack of understanding. Some functional advocates have often pointed out that OO can be implemented in functional and as such should be seen as a subset of the functional style. (Of course, I think they mean that in the sense of having objects and transforming rather than mutating since mutation is frowned upon by purists)

I think the main benefit of functional that we see day to day is that instead of generic for / while loops we have higher level loops like mapcar, reduce, filter, etc.. That makes for a more advanced structures in the program that can help readability and composability.

But the entire design still needs some big architectural principles to hold things together. And that would involve defining and encapsulating data structures and then defining orderly transformations.