musings of a Software Developer

Seven languages in seven weeks, my solutions for scala day 3


In day 3 of the Scala chapter of the book seven languages in seven weeks we dive deeper in functional programming and parallelization with scala. I run into some problems doing the exercises, one of them being that the Source.fromUrl method, after putting in the encoding, there were still some problems when following links. So I had to put a try catch around it. Normally you might need a bit more robust solution, but for now this will do ;-)

The first exercise was kind of simple, you had to adjust a given “sizer” script to include a link count. There was already a foldLeft over the links so I only had to add an int var and a increment of this counter, and off course return the result.

At work a colleague once talked about the idea of tupels, and I wanted to introduce this also in my Java code. I was taken aback from the idea back then since you would only need such things if your methods do more than one thing or you are to lazy to introduce an object for the results that belong together. But because he learned about tuples in Scala (and I was lazy myself) I wanted to give it a try. So I adjusted the existing method that returned the size, to also return the link count.

I still haven’t made my mind up about this tuple idea. Off course it is very handy, but to me it still seems to be about being lazy instead of writing better code. Why not return an object with both values in it or make two separate methods? The only thing I can come up with is maybe an object or id and a count or other statistics. Well that being said, I decided to refactor a bit, here is my refactored solution:

Still not the most beautiful program in the world, but at least a bit better in my opinion. But it might be because I’m used to imperative OO progamming instead of functional programming of course.

The bonus exercise of this chapter is to follow the links in the given sites and calculate the total size of the page and its links. And this should off course be done in a sequential and a parallel way.

I started off with the sequential method, and really love the power of the leftFold (folding in general). It really saves you quite some code, and still is quite readable.

For the paralellization I stumbled upon the scala 2.9 feature of parallel collections. I really love the idea, it gives beautiful clean code. Apparently there are also some limitations and complaints about it, but for this exercise it worked just great.

All things considered I have to adjust my opinion on scala to a quite more favorable one.

I’m not very fond off the ugly _1 _2 syntax, and also the short leftFold notation is too compact and complex in my opinion. But scala gives you quite a lot of things that make coding and also reading code easier.

But one of the main problems I see is that a lot of people seem to thing that it is ok to make code shorter (and thus more complex) and that we should expect other programmers they understand. To some extend I can sympathize, but at the end I read more code than I will ever write. I even read my own code more often than I write it. The code should always clearly communicate what is happening. One more than one occasion I have seen Scala code where I just get the feeling people want to show off what they can do with Scala (which is a scary lot).

Making code more complex and harder to read just to shorten it to the max, usually with the justification of “programming just being a difficult job”, is a bad practice IMHO. Sometimes those extra lines make it easier for someone (like yourself) to understand what you were thinking a year or so ago ;)

Leave a Reply

Required fields are marked *.