9

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.

Usman Ismail
  • 314
  • 2
  • 9
  • 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 06:15
  • 1
    The project is in Pre-alpha stage and we have a mandate to experiment. – Usman Ismail Nov 14 '12 at 06: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 06: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 06:43
  • 1
    If your application uses concurrency and communication heavily, you might want to look at http://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 06:54
  • @Giorgio: Of all the options for working with legacy code, I see mixing languages in the same project to be one of the worst possible choices. Wrap up the legacy code in an assembly and call it from new code if necessary. – Steven Evers Nov 14 '12 at 16:16
  • @SnOrfus: I think we have a similar idea: in Java, this would mean to have a Java API as an interface to the Java legacy code and write the new code in Scala. I did not mean to write the new code in a mix of Scala and Java. – Giorgio Nov 14 '12 at 19:25
  • @Usman Ismail: I am a big fan of Scala, but I agree with SnOrfus that it is not a good idea to have a mixed project in Java / Scala only to convince your colleagues to take a look at some Scala snippets. If you want the project to switch to Scala you should follow a more structured approach: attend a course, experiment with small side projects and tools, discuss and plan the transition with the rest of the team after you have gotten some experience. But learning the language on a mixed Java / Scala alpha release of your product does not seem a good approach to me. – Giorgio Nov 15 '12 at 22:25

4 Answers4

4

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)
        names.add(customer.name);

    Collections.sort(names);

    return names;
}

Scala version:

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

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

NOTE

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.

Giorgio
  • 19,646
  • 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
3

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.

cnd
  • 1,874
  • 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 07: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 07: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 07: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 07: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
  • @Giorgio that is fine case, I say nothing against that, I'm saying that develop in both languages everytime chosing on which one will be better is bad pattern for a project. – cnd Nov 14 '12 at 10:26
  • @Ash: Yes, I definitely agree, also considering that from an OOP point of view, Scala covers most of Java's features, so any Java program can be mapped quite faithfully to Scala. – Giorgio Nov 14 '12 at 10:47
2

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.

Jan Hudec
  • 18,340
-2

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 ;)

ggambetta
  • 1,214
  • 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. http://www.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 :) – ggambetta 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
  • @Giorgio, I was [half] joking, don't read too much into my answer. I know every paradigm has its legitimate uses. I respect that. What I don't respect are the posers and fad-followers, and FP seems to attract a disproportionately high number of these, for whatever reason (again, in my experience). I know a couple of people who are FP die hards who endlessly mentally masturbate thinking of monads and bask in their superiority, but don't actually do anything. Come on, you must know some (and yes, I am aware that idiots come in any programming language, not limited to FP) – ggambetta Nov 16 '12 at 07:15