Grasping immutability

I work in a Scala shop. We are a small start-up. When I arrived, the CTO was the only developer on the back-end and  we were inheriting a large ill-written ruby codebase. Fortunately, the CTO told me that the first “task” was to rewrite everything in Scala. So we did, and now we are an Scala shop.

Our lifes are fulfilled with happiness; but that’s not true for the new members of the team. The company is in South Florida, a place were only old people and spring-break-teens want to move in. So we pick developers that are smart and eager to learn, even if they don’t have any experience with Scala or functional programming in general, which they never have.

The new members learn as they go, since we can’t wait for them to be comfortable with Scala. This has gave me the opportunity of witnessing all kinds of code: from the carefully crafted that our CTO produces, to the crude, and often pragmatic, that our less experienced developers write.

One concern of theirs is that they might not be writing a “worthy” code. I also had that problem when I started. I have come to consider that craving for the “right’ code, is one of the worst dangers when learning Scala. There are too many ways of doing it right, the doubt can be paralyzing.

One of the developers asked me if I was OK with his code having vars. I told him I didn’t really care, but he should be careful about passing those vars around, they should have a limited scope; then I gave him a little talk about immutability and its benefits. This happened a few times. Of course, what he understood each time was that vars were OK, and he just kept going on with his work. Eventually he hit a wall. His code had a bug, one of those hard to grasp. His code worked well when testing under controlled conditions, but always failed when the concurrent reality was added into the mix.

Then something happened that made all those little talks to worth their while: I suggested him to look for shared state somewhere. He found the bug, and I could see right there, when he was telling me about his findings, that he finally understood the benefits of immutability.

 

 

Advertisements

3 thoughts on “Grasping immutability

  1. Very good advice regarding the approach to green developers. Many functional programming purists site this as a weakness of Scala, but I can attest to its contributions to its success in my organization. I joined a team with only two other developers, both fresh out of college. They knew a dab of JavaScript and a good bit of Java thanks to a GWT project. While they were initially apprehensive to make the leap to Scala, we’ve successfully struck out on a project with Scala on the back end (Lift and Akka) and CoffeeScript and Angular on the front end. They’re use of vars is almost eliminated and they both much prefer to write the server-side code. There’s no way in hell we could have been productive enough if we set sail with Clojure or the quintessential Haskell. I’d still be lecturing on monads instead of building useful software. I was able to coach them to productivity quickly, and over time they continue to refine their skills to produce more idiomatic Scala. In my experience, it has truly been the scalable language.

    • @barnesjd
      “In my experience, it has truly been the scalable language.”

      … and that can’t be said enough. There are far too many people who see Scala as a Haskell or as a Java. Is hard to make them understand that Scala is somewhere in the middle, and for a good reason: It works.

  2. It's been hilarious watching the self-anointed saberkidz complain how dumb it is to slave an award to transparent counting statistics like 'home runs' and 'RBI' by advocating that we…instead slave these awards to an opaque black-box statistic that virtually no one stirring up this ginned-up 'controversy' is capable of replicating!Yes, that's so much &#m.9s3arter;9Make no mistake, Trout had a very nice Rickey Henderson-like year. By my count this 'WAR' stat gave him some 4-5 'wins' for achievements related to his running speed. It's okay to be a skeptic of this. Or at least, it should be.My vote goes to Cabrera.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s