Resources Software Development

Scala things to avoid

Scala has some things you should be aware of in order to not write code that is slower than molasses in Winter in Siberia or eat up every last drop of RAM your machine has.  Most of these things revolve around Collections/Arrays and how you work with them.

When I started digging into for comprehensions they sounded very inefficient so I did some digging and my suspicions were

#1 thing to avoid For comprehensions. Unless the collection/array/data you are operating on is small then avoid for comprehensions and use other methods. Reason being is for comprehensions underneath the covers create maps and flatmaps and those create objects for each element in your collection/data. If you stop for a moment you can imagine what a collection of 1,000 elements would do or larger. So you should probably use for comprehensions on only small collections like 10 or less. Of course you should play with your code and find out for yourself what is more efficient.

Also map and flatmap are not very efficient with large collections for the same reason as above.

I will continue to add to this article as I have time. Below are some excellent resources describing the above in much deeper detail.

You might also want to see my listing of Scala Best practice resources.

Benchmarking Scala collections – article by lihaoyi about the inefficiencies of and memory usage of various Scala Collection methods such as map and flatmap very valuable and useful information here for people that like to understand languages inside and out.

Warts of the Scala Programming language – Excellent article about some of the bad parts of Scala

Scala: comprehending the for-comprehension – this is a great article that covers the for comprehension in depth. This will help you understand more about the inefficiencies I am talking about.

Scala Wart: Convoluted de-sugaring of for-comprehensions – link to a discussion about the language issues including the creator of Scala. This give super in depth information as to what I am referring to above.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.