
Three thousand lines of code is not a huge program, but it is enough
to get a pretty good feel for a language. Now that I've completed my
first real Haskell program I feel that I've gotten over several
of the humps in the learning curve and am starting to get a good feel
for it.
Actually, I've written closer to five thousand lines, since there were
several big refactorings. One was when I stopped manually threading my
program state around and added a
StateT monad. I did know from the
beginning I would need one, but it seemed easier and a better learning
exercise to let the program start out with a vesigial tail and gills
before growing up into a modern Haskell program. (I suppose it's still
written in baby-Haskell, really..)
Another refactoring came when I realized I needed to use a custom data
type, not
String, to represent keys. That was a great experience in
type-based refactoring. Being able to keep typing ':make' and landing
on the next bit of code that needed fixing was great, and simply adding
that one type exposed several non-obvious bugs.
I found myself writing code that is much more solid and reusable than
normally comes easily. And yet it's also very malleable. Actually,
pulling out better data types and abstractions can get a bit addictive.
When I realized that I had a similar three-stage control flow being
used for each of
git-annex's subcommands, and factored that
control flow out into a function that used the 3 data types below, I felt
I'd gone down that rabbit hole perhaps far enough for now.
type SubCmdStart = String -> Annex (Maybe SubCmdPerform)
type SubCmdPerform = Annex (Maybe SubCmdCleanup)
type SubCmdCleanup = Annex Bool
(That will allow for some nice parallelism later though, and removed dozens
of lines of code, so was worth it.)
Since
git-annex is a very
Real World Haskell type program,
there is a lot of impure code in it. I could probably do better at
factoring out more pure code. I count 117 impure functions, and only
37 pure.
Anyhow, from my perspective of a long-time perl programmer, some
other random impressions..