r/programming • • May 25 '17

Readable Clojure

http://tonsky.me/blog/readable-clojure/
42 Upvotes

35 comments sorted by

View all comments

12

u/kindofdynamite May 25 '17

As a newcomer to FP, one of the biggest hurdles has been trying to decipher one letter variables, imports without a namespace and general terse code.

It seems to me that there is less of a focus on code readability ("plain english") in FP languages.

Why is terse code so common in Clojure/Haskell/et all? Is there something I'm missing that will cause me to "see the light"?

14

u/[deleted] May 25 '17

Frequently there simply isn't a good name as FP tends to heavily rely on generic algorithms. For instance, consider a definition of map (in say Haskell):

map _ [] = []
map f (x:xs) = f x : map f xs

f is a normal variable name in math meaning a function. x is a normal variable in math meaning an argument. xs is therefore just more arguments we're going to apply f to. In this case, the conventions are straightforward.

There seems to be very few people who find the following easier to read (note there are obviously names somewhere in between so this is sort of a strawman):

map _ [] = []
map function (someThing:restOfList) = function someThing : map function restOfList

Unfortunately, the habit of short names leaks into situations where there actually are decent names to be had.

4

u/ksion May 25 '17

The other reason is that at least in a heavily typed languages like Haskell, the type signature is often very indicative of what the function actually does.

For generic functions it may indeed be (a -> b -> c) -> a -> b -> c which translates to just f, x, y because there is nothing more concrete to be said about those arguments. But even when it's something more specific such as parseArgv :: [Text] -> Parser a -> Config, you don't really need more than one-letter argument names to figure out what's happening.