3 ms·
What kind of abstractions are the most trustworthy? In my experience, the ultimate in abstraction is anything that works solely with immutable data. This inclu
by chousuke 17y ago
What kind of abstractions are the most trustworthy?
In my experience, the ultimate in abstraction is anything that works solely with immutable data. This includes, obviously, pure functions, but also objects which have no mutable state. Such things are trivial to test and perfectly composable. It's easier to gain trust in them as you rarely need to know exactly what they do, only whether they appear to work or not.
Obviously immutability alone is not enough for many abstractions, but I would still like to see it used more.
The current object-oriented models seem to encourage encapsulating state, rather than encapsulating data, which seems to lead programmers to use mutable things even when they are unnecessary.
Unless the abstraction requires it (eg. IO), I think any state held within an object is, in a way, leaky abstraction. As long as there is state within an object, it is not obvious whether you can, at any one time:
1) call its methods
2) hold a reference to it
3) delete it, or your reference to it (Mostly applies to non-GC languages)
4) Pass it as a parameter to something
All these worries disappear with immutable values.
- wisty 17y agoPure functions have one problem - they don't have state, and sometimes you need state. What's needed is pure functions for logic, and dumb objects holding data, and function pointers.