reading scala (functional programming) code
Based on some recent scala work and coming from imperative programming background, reading functional programming code is hard for me.
Why?
First, the code is rather terse and dense. The terseness derives from the ability to use operator-like characters as function names. Java does not allow this so its usually easier to read the methods. Java's method name conventions generally make the code easy to read. This comes at the expense of verbosity. Scala can use normal function names as well and most libraries provide both the function name as well as a terse equivalent.
Second, its not always clear what design pattern is being implemented. Java has a few generally accepted approaches to writing code. Iterating over a list can only take a few forms. Creating some IO classes generally takes on only a few forms as well. The singleton pattern is implemented only in a few ways. If you are unfamiliar with functional programming, it takes awhile to recognize these types of patterns. There is alot noise in the functional programming community around monads. Monads have some special properties that make them useful in functional languages. I won't cover them here but there is a benefit for using monads. If you know something is a monad, then you expect it to behave a certain way and have certain types of methods. Putting aside details around their usage, monads help you understand functional code more quickly.
Third, functional languages are geared towards taking very small slices of code and building on them, layer after layer (or should I say, function after function). This type of fine-grained layering is much harder to understand than the coarseness of the java world. There are benefits to fine layering of course. However, a side effect is that it takes longer to get used to reading more finely-layered code.
I'm not saying that functional programming is hard or that's its not worth the effort to convert. It's not hard if you grew up with it. Converting is harder if you are coming from an imperative background (fortunately its more easy for me since I have a lisp background).
Comments
Post a Comment