4 ms·
> - keep the complexity of the functions as low as possible. This seems particularly critical. This style works well in Haskell where it's extremely common to
by JoshTriplett 10y ago
> - keep the complexity of the functions as low as possible.
This seems particularly critical. This style works well in Haskell where it's extremely common to have short functions.
I've found that Haskell code often has two kinds of functions: 1-2 line functions (or function cases) that do actual work and computation (often combining other such functions), and 50+ line monadic functions that implement simple sequential routines with very simple control flow (calling other functions for any non-trivial computation). Point-free style works quite well for many of the former.
- krakensden 10y agoI don't do Haskell, but I have worked on projects with lots of small functions, and one of the problems with that is that can be... too many functions. You have to go look up three functions every time you read a block of code. Worse, people start writing the same functions over and over again. There's probably a bigger picture problem and fix in that kind of situation, but it is something I think about.
- JoshTriplett 10y ago> You have to go look up three functions every time you read a block of code. When you write small, single-purpose functions, you can make them fairly self-documenting with the name alone. If you have to look up a one-line function, it probably doesn't have a very clear function or name. > Worse, people start writing the same functions over and over again. That one I've definitely run into. > There's probably a bigger picture problem and fix in that kind of situation, but it is something I think about. Mechanisms to search functions by type ("find me a function that takes an X and returns a Y") help quite a bit.
- mrkgnao 10y agoConventions also help. For instance, the lazy readTVar, modifyTVar, etc. functions have strict analogs called readTVar' and so on. mapM, foldM, replicateM, and traverse return a monadic value, while mapM_, foldM_, replicateM_, traverse_ and friends discard the return value (giving an m () instead of an m a).
- Silhouette 10y agoWhen you write small, single-purpose functions, you can make them fairly self-documenting with the name alone. Sometimes, although with languages like Haskell there can be a related problem that accurate names for these small functions wind up being almost as long, or even longer, than their definitions.
- astrobe_ 10y agoPharo/Squeak (and probably other Smalltalk systems) have a utility that let you find a function given a set of input/output examples.
- krakensden 10y agoDo you have a name/link? That sounds fascinating.