r/functionalprogramming • u/Alexander_Selkirk • Mar 31 '19
Question Are there "clean code" guidelines on FP?
One thing I am wondering - are there any general guidelines in the style of "clean code" for the paradigm of functional programming?
To give some information on my background, I am familiar with Clojure and Scheme, and I am aware of some Clojure coding style guidelines. What I am looking for is for good practices one level up, like:
how many parameters should functions have?
when should flags be used, when methods which match the same signature?
when should data be combined in records / objects ?
what is the best way to arrange I/O and database access?
what helps to organize larger programs?
I am looking especially for guidelines which are generally applicable, regards of the programming language.
To describe the task at hand, I am currently writing an application in Python which does both a lot of hardware control, data gathering, and a lot of data transformation and presentation. While some of the hardware control seems to be a more naturally fit to OOP with some imperative parts (this is where the changing state of the hardware matters), some seems more suited to OOP with a functional paradigm (similar to Scala's immutable objects), and the data transformation part looks like a very clear match to "pure" FP (which is not all that unusual in code which uses Numerical Python, for example). I have been checking this code using pylint. It uses some complexity evaluations like number of branches, number of parameters, lines of code in methods. These checks tend more frequently to fail in the hardware control parts - especially, it tags some functions as "too complex". Obviously, the checks in pylint draw some criterions from Robert Martin's "Clean Code" guidlines, for example methods and functions with too many branches, too many parameters, or too many lines of code are marked with warnings.
It might be that there, I am simply trying to use too much a "pure functional" style where some sort of OOP approach (with or without side effects) would be better suited. But I am also wondering whether good functional-styled code might simply need somewhat different guidelines. For example, in "Clean Code", Bob Martin says that no function should have more than three parameters. It seems to me that this makes sense for an OOP style, where a lot of additional information is in object member variables, bit might be too little in a pure FP style.
Finally, I want to formulate something which is, let's call it, a bit of a suspicion, it is not a theory, and it corresponds somewhat to the matter of preferred OOP styles and differences to FP styles. The suspicion is that OOP has (often) advantages when used in a single code base. An example for that would be a Unix kernel. However, much of the appeal of OOP in the last three decades seemed to come from the idea that software can be made into independent, reusable parts, which can be fabricated, stored, sold, and used, just like parts of a machine, and independent of the code base in which it is employed. So, this view of OOP sees OOP as a means for re-using code as components. An example for this would be Delphi or GUI toolkits, or Java libraries. A consequence of that approach is that the resulting libraries are often very heavily geared toward general usefulness and maximum flexibility, which leads sometimes to a very high stack of abstractions, and a high complexity - I think the Java libraries are a good example of that. Probably, some clean code guidelines reflect that.
There seems to be a different school of thought which sees abstraction as not necessarily helpful, and thinks that abstraction needs to be justified within each single program - that it is better to re-write some code, than to tug around a huge baggage of unused abstractions. I think for this school of thought, FORTH is a good example.
Now, to formulate my suspicion, it appears to me that in functional programming languages, abstractions tend to be seen more in the way of an intermediate, middle-ground school of thought - they are useful if they help to express the code more clearly, but the primary goal of abstractions is not to get complex, re-usable components, rather it is to organize the code into layers which can be reasoned about independently. It seems to me that languages like Lisp and Clojure are much more centered around sharing and re-using of source code, than of binary software components. And this preference for code-sharing over component-sharing might be one of the reasons why FP is (or was) less popular in the parts of the commercial software industry which sell licensed software, than in those parts which, for example, provide software as a service.
9
u/lgastako Mar 31 '19 edited Mar 31 '19
I am looking especially for guidelines which are generally applicable, regards of the programming language.
One problem you're going to run into is that cross-language guidelines are going to be fairly abstract, like "design for composition" but the concrete implementation of those guidelines is going to vary widely from language to language.
For example "designing for composition" in Clojure might mean always putting the data to be operated on as the first parameter and passing optional parameters second, so that you can build up natural compositions with threading operators like:
(-> value
foo
(bar "baz")
(bif [1 2 3]))whereas in Haskell "designing for composition" might mean putting the data to be operated on last with any optional arguments first so they can be supplied via partial application and the functions can be composed with the normal composition operator:
bif [1, 2, 3] . bar "baz" . foo $ valueClojure has the comp function which is equivalent to Haskell's composition operator (.) (and partial for partial application) but they are not used as frequently in idiomatic Clojure as the composition operator is in idiomatic Haskell.
3
u/watsreddit Mar 31 '19
We have a few in the Haskell world that could probably be applied to other functional (and indeed, non-functional) languages, such as "push side-effects to the edges of your application" and relatedly, the "functional core, imperative shell". The basic idea is that we can define interfaces that describe effectful actions and write functions parameterized on those interfaces, and only use the concrete implementations at the entry/exit points of your app. The core of the application doesn't use these interfaces at all, and just does pure data transformations.
Though I suppose maybe this idea is less useful without the Monad abstraction, I dunno.