Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question

closed as not constructive by Karl Bielefeldt, gnat, Walter, Jan Hudec, Ryathal Nov 14 '12 at 16:53

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

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. –  Steve 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

5 Answers 5

up vote 3 down vote accepted

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.

share|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 Rizos 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

Scala adds functional programming capabilities, although it isn't purely functional. If you're not writing functional code, it comes down to a matter of opinion on which syntax you prefer, so I think your question boils down to when functional programming is better.

Functional programming is generally superior when the relationships between data are more prominent than a sequence of states. It can get unwieldy in the opposite case. In other words, if you don't care what order a computation is done in as long as you get the right answer, then functional programming will probably be more concise.

Examples include performing mathematical computations, especially on lists. Another example is problems dealing with real world relationships, like the "six degrees of separation" problem or a genealogical database.

Counter examples include software with heavy user interaction, like word processing. The user hits a series of keys and the code makes a corresponding sequence of changes of state to the document. A good functional programmer can still make very readable code in cases like that, but it would rarely be more concise and readable than the non-functional version, and is very easy to make worse.

share|improve this answer
    
In a strict functional language like Scala the order of evaluation is well-defined (expressions are evaluated one after the other, in the order in which they appear, so this is basically imperative execution), so as far as I understand the example with user interaction does not apply to Scala (and OCaml, Common Lisp, Clojure, Scheme, SML, etc). Also, Haskell can sequence the evaluation of expressions through monads (e.g. if you want to print a string you must first evaluate it) so, even though I know little about Haskell, your counterexample might not be applicable to Haskell as well. –  Giorgio Nov 14 '12 at 10:15
1  
I'm not saying functional languages can't have a deterministic order, I'm saying their strengths mostly shine when you don't need one. –  Karl Bielefeldt Feb 10 '13 at 17:13

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.

share|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. –  Heather 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? –  Heather 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.

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

share|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 Rizos 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.