In a current project we have setup the build so that we could mix Java and Scala. I would like to use more Scala in our code base to make the code more readable and concise. In the process also learn the language by handing over real features.

So I plan to use Scala for some classes to showcase its benefits and convince other devs to look into using Scala too.

For a rest based web server or a program in general what kind of code structures lend themselves to Scala's functional programming style.

  • 7
    -1: I'm all for learning new languages (especially languages that are more functional these days)... but looking for a reason to mix in a different language and paradigm into production code? That's a no no. Go with a personal project, or a separate app/utility/tool that management signs off on... at most. – Steven Evers Nov 14 '12 at 6:15
  • 1
    The project is in Pre-alpha stage and we have a mandate to experiment. – Usman Ismail Nov 14 '12 at 6:34
  • 2
    @SnOrfus: "That's a no no.": I see it as a good way to work with legacy code (instead of waiting for the next backward compatible revision of the language in which the legacy code is written). – Giorgio Nov 14 '12 at 6:35
  • 2
    Asking for a list of things (like classes and use cases) is generally not a good fit for this site. I think you have a good question in there, though. You might want to try rephrasing it. – Karl Bielefeldt Nov 14 '12 at 6:43
  • 1
    If your application uses concurrency and communication heavily, you might want to look at akka.io. Otherwise, try to write an example in which you make heavy use of collections with complex transformations like filtering, folding, mapping. For this, Scala can be much more concise than Java. – Giorgio Nov 14 '12 at 6:54

A typical example if you want to illustrate conciseness of functional code could use collections and their operations. E.g. consider a class Customer and a method creating a sorted list of customer names from a list of customers.

Here is the Java implementation:

class Customer
    String name;

    ... // Other attributes.

List<String> sortedCustomerNames(List<Customer> customers)
    List<String> names = new ArrayList<String>();

    for (Customer customer : customers)


    return names;

Scala version:

class Customer(val name: String, /* ... other attributes */)

def sortedCustomerNames(customers: List[Customer]): List[String] =
  customers.map(c => c.name).sorted


Conciseness in terms of keystrokes is not always a good measure of code quality and maintainability.

Also, in general I would not advise to switch from Java to Scala just because Scala is newer. If most of the team members are more familiar with Java, the team will be much more productive with Java than with Scala (or any other programming language they don't know well).

I have been learning Scala in my free time for about two years now, and only recently (after attending an online course) I got the feeling I can be more productive in Scala than I am in Java. Before reaching this point, I would not have considered using Scala for production.

| improve this answer | |
  • 1
    Conciseness in terms of keystrokes is not always a good measure of code quality and maintainability Especially when coding in Perl. – yannis Nov 14 '12 at 10:53
  • This is a good example of what I was looking for, I don't want to switch everything to scala at once but small utility classes which do obvious things would be a good place to start. thx – Usman Ismail Nov 14 '12 at 11:00
  • 1
    @Usman Ismail: Another thing you could look at is Scala collections. I was recently very impressed by what one can do with Stream's: you can perform complex breadth first searches in a solution space until you hit a solution (like find a path from a certain object to another one, with certain constraints). I do not know if this is relevant for you. – Giorgio Nov 14 '12 at 11:11

For all, everywhere where it is possible or for nothing, just use Java. This way this will work sane. I were making such experiments long time ago but in later times you rewrite everything to one or two (to you keep some things if that could not be done in another language) languages just for clarity.

If you can declare class shorter in 2 lines of code, that is not advantage you must not use language for this "feature". For example also you can write beautiful functional code in xxx lines and spend yyy time and maybe in the same time you can write it in OOP style in xxx+150 lines in yyy-2hours time with perfomance benefit (who knows). What I want to say is that is not possible to say "this part will be better on Scala" and "this part on Java". In development you must not discover features, in my opinion, you must just develop without mixing and think about general logic instead of implementation in language X or Y that's why, in my opinion, for clarity and productivity you must pick language for project instead of for feature.

| improve this answer | |
  • I would disagree a little there because there are now 150 fewer lines of code to test and to have potential bugs in. – Usman Ismail Nov 14 '12 at 7:36
  • 2
    Lines of code makes no sense at all. if perl code could be 5x smaller than python it doesn't mean it's easier to test and contains lesser potential bugs. Yes, there are some project with mixed perl and python but I don't really see it as cool feature. – cnd Nov 14 '12 at 7:39
  • Your point is valid, however take a look at Project Lombok, as an example of how you can elegantly reduce Java's verbosity. I was wondering if there was a similar benefit with Scala's functional features. Scala is designed to be mixed with Java unlike other functional languages such as erlang which would probably be a better option if we went full functional. – Usman Ismail Nov 14 '12 at 7:44
  • 1
    I still don't understand that mixing. If Scala is that good for you then just use it everywhere you can in your project. That will be sane and I understand it but using it for some features in some cases is wrong way in my opinion. Is there reason to not just switch project to Scala? – cnd Nov 14 '12 at 7:57
  • @Ash: Maybe they need to interface to legacy code? In this case the main code will be in Scala and the legacy code (and possibly some interfaces or wrappers) in Java. – Giorgio Nov 14 '12 at 10:18

I suspect this is a matter of how good the programmers on the team are and how much they are willing to learn new things.

Scala is much more concise and expressive, but equally more complex. Any programmer who can make a good use of the high level abstractions it offers will like it and be more productive in it and will never want to go back to the oversimplified level of Java. But a programmer who is not good at high level abstractions or simply does not want to learn will never get it.

Also I don't think you should use any cases tailored specifically to Scala. Just start writing the prototype in it and you'll see whether it's more productive or not. It should be more productive for everything to be worth switching.

And I definitely wouldn't mix the languages except for using libraries written in the other. Start prototyping in Scala, see how well it goes and choose one or the other.

| improve this answer | |

In my (admittedly limited) experience, the use case I've seen most is to use it to get a sense of superiority and smugness over your colleagues who actually ship things, and/or hide your limitations by blaming the "antiquated and inelegant" language you're "forced" to use instead of the new hotness ;)

| improve this answer | |
  • 1
    -1: Functional programming is much older than Java. – Giorgio Nov 14 '12 at 10:19
  • 2
    Hey, that's more or less the same argument I used when Java first showed up! But I was 17 at the time... – yannis Nov 14 '12 at 10:57
  • 2
    BTW, functional programming techniques are about shipping things (more quickly), not about boasting (see e.g. paulgraham.com/avg.html). – Giorgio Nov 14 '12 at 12:13
  • -3? Come on, you can do better. @Giorgio, I didn't mention Java at all, IMO it also sucks :) – ggambett Nov 15 '12 at 21:13
  • @ggambett: You did refer to Java indirectly: The question asks about introducing Scala (and its functional style) in an environment using Java. Your answer compares an "antiquated ... language you're forced to use" (Java?) with the "new hotness" (Scala - functional programming?) whose most common use case is "to get a sense of superiority ... over your colleagues". Or what was the exact meaning of your answer? – Giorgio Nov 15 '12 at 22:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.