Justin du Coeur (jducoeur) wrote,
Justin du Coeur

Paradigm shifts as fixing mental bugs

[Purely for the serious programmers.]

Amusing. I just realized the following:

The shift from "old-fashioned" programming (BASIC, Fortran, assembly) to modern styles was largely accomplished by internalizing that using "goto" was a design bug. The languages allowed it for a while, but it slowly went away, because it was never really worth the pain imposed by using it.

Likewise, the shift from imperative to functional programming is largely accomplished by internalizing that using "var" is a design bug. Like goto, it's still sometimes useful, especially when working in a hybrid language like Scala. But one of the ways in which I realize that I'm making the mental leap is that little twitch I get every time I type var, which is leading me to go that extra mile to eliminate it.

Yes, there is *vastly* more to functional than simply replacing var with val. But it's surprisingly central, simply because avoiding vars *motivates* a lot of functional techniques. I suspect I am slowly reinventing a lot of functional wheels as I figure out how to avoid vars in various circumstances. (For example, yesterday afternoon's exercise was puzzling out how to efficiently mutate a node buried in a tree.)

It's nothing more than a guideline, but I recommend it to anyone trying to learn functional-programming style: every time you reach for a var, try to figure out how to accomplish your goals with a val instead. It's possible without great pain most of the time, and you learn a lot from the exercise.

At this point, I think I'm only using vars in a few small, localized algorithms, and a few state vars nestled safely inside Actors. I continue to be surprised that I've been able to build something as big and complex as Querki in nearly pure-functional style, without terribly excessive pain. And the payoff is enormous: by combining Actors (in the form of Akka) and functional style, I wind up with a system that is massively scalable and thread-safe more or less automatically. For the most part, I don't even have to *think* about threading, which is a refreshing change of pace...
Tags: programming
  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded