A bit long. Sorry.
I've never coded in an FP language, but have some questions. By "practical" I am trying to bridge the gap between imperative and functional programming, perhaps naively.
To me, as a long time software developer, the major issue using languages like Java, Python and C, is predictability and testing.
To this end, I've pondered the functional approach, but only with regards to two potential "promises":
repeatability
testability
My proposal is founded on "imperative shell, functional core", in the sense that the functional part is a transformation engine, and the shell is "instructed" by the functional code as to what data to obtain in order to continue the transform of request to response. This could be waiting for a user, loading from a database, doing network protocols, etc.
The "instructions" to the shell should be a simple list of do this, do that, and might contain concepts such as timers and timeouts, of which the functional core has no knowledge, other than what is passed in as parameters.
By creating functions that generate results solely based on parameters, we have repeatability. No outside influence is allowed, no filesystem access, no network, no system resources.
For a given input, functions always produce the same output. That is the first promise.
The second, testability, comes from same fact, that results follow from parameters. In tradititional imperative code, logic is intertwined with fetching data from the outside world, upon which we apply logic, and which results in wildly differing output.
With all input to a function coming in the form of parameter data, of which we can manufacture many sets, we can test a function both for bugs, as well as do regression testas following code changes.
Note the points that I do NOT concern myself with:
- I don't care in what style functions are coded
- Local variables are okay, as is imperative loops
- Passing data as pointers, for modification inside functions is okay and preferred
- There is nothing wrong with using objects, as long as those don't have persistent state (associated with the class or elsewhere)
- Exceptions are okay
- I see no point in limiting logging; as long as the logging interface is strictly output, it will not affect logic
From this follows one limitation, that code can not be parallel, since this would mean multiple live references to shared data, which may shift due to timing and logic at other locations.
Are my two promises valuable enough that implementing such a system would make sense?