The Artima Developer Community
Sponsored Link

Weblogs Forum
Is Scala really more complicated than Java?

54 replies on 4 pages. Most recent reply: Nov 11, 2009 2:45 PM by Danny Lagrouw

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 54 replies on 4 pages [ « | 1 2 3 4 | » ]
Christian Maslen

Posts: 1
Nickname: cmaslen
Registered: Sep, 2009

Re: Is Scala really more complicated than Java? Posted: Sep 20, 2009 7:37 PM
Reply to this message Reply
Advertisement
Maybe it's me but I find both are equally difficult to read because they are formatted as one liners with little spacing. But comparing
for (int i = 0; i < 4; i++) {
    System.out.println("Happy Birthday " + (i == 2 ? "Dear XXX" : "To You")); 
}

to
(1 to 4).map { 
    i => "Happy Birthday %s".format(if (i == 3) "Dear XXX" else "To You") 
}.foreach { 
    println(_) 
}


for me at least (with very little Scala background), the Scala looks good.

Christian

Daniel Sobral

Posts: 80
Nickname: 57409
Registered: Aug, 2008

Re: Is Scala really more complicated than Java? Posted: Sep 20, 2009 7:43 PM
Reply to this message Reply
> Interestingly the result type isn't quite
> a list of strings - the repl gives me
>
>
> res0: RandomAccessSeq.Projection[String] = RangeM(Happy
> Birthday To You, Happy Birthday To You, Happy Birthday
> Dear XXX, Happy Birthday To You)
>

>
> I still can't help but think about these intermediate data
> structures that are being thrown around. I don't think
> Scala aggressively optimises this stuff.

Well, that comment, specially regarding that data structure, is wonderfully misguided! :-) Please, don't take offense by this comment, but let me explain that structure, and you'll see it precisely proves the opposite of what you said! :-)

Let's think about RandomAccessSeq.Projection[String]. First thing, it was generated from a Range -- it's the kind of signature you get when you call "map" on a Range in Scala 2.7. On Scala 2.8 you'll get a different class, that does about the same.

So, what does this structure contain? One thing you might be surprised to learn is that it doesn't contain any String! What it does have, actually, is a reference to the Range, and a reference to the function that was passed to it.

What you do a "map" on a Range, it doesn't compute anything. Only when you actually make access to an element it will be computed -- if you don't access it, it won't be computed.

The downside is that it gets computed every time you do access it. So, once you did all your maps, filters, drops, takes and what-have-you, you might transform it into a List or something, if you are going to repeatedly access those elements.

Steven Shaw

Posts: 15
Nickname: sshaw
Registered: Apr, 2003

Re: Is Scala really more complicated than Java? Posted: Sep 20, 2009 8:16 PM
Reply to this message Reply
Daniel, no offense taken. I didn't know that the Projection was lazily evaluated when applying map etc. What I was getting at about the data structures etc is that none of those appear in the original Java version. However, I took a casual look at the performance and it seems that the IO swamps anything else (except for calling format which must be quite slow).

art src

Posts: 33
Nickname: articulate
Registered: Sep, 2005

Re: Is Scala really more complicated than Java? Posted: Sep 21, 2009 12:45 AM
Reply to this message Reply
> I think we overstretched the example a bit and forgot the
> initial goal Dick had in mind- to prove that Scala can be
> *readable*.

I have the impression Scala is complicated, not unreadable. Specifically: type inferencing enhances readability; but makes the language more complicated.

> val (d,t)=("Dear XXX","To you")
> List(t,t,d,t) map ("Happy Birthday " + _) foreach println

Very nice. In Java too:

String t = "To You";
String d = "Dear XXX";
for (String end : new String[] {t, t, d, t}) {
System.out.println("Happy Birthday " + end);
}

art src

Posts: 33
Nickname: articulate
Registered: Sep, 2005

Re: Is Scala really more complicated than Java? Posted: Sep 21, 2009 12:47 AM
Reply to this message Reply
Seems simple and concise. I think mixing the IO and the logic makes it simpler.

Carson Gross

Posts: 153
Nickname: cgross
Registered: Oct, 2006

Re: Is Scala really more complicated than Java? Posted: Sep 23, 2009 1:51 PM
Reply to this message Reply
Just 'cause, here's the Gosu version:

var to = "To You";
var dear = "Dear XXX";
for( end in {to, to, dear, to} ){
print( "Happy Birthday ${end}" )
}

or, if you must:

var to = "To You";
var dear = "Dear XXX";
{to, to, dear, to}.each( \ end -> print( "Happy Birthday ${end}" ) )

That doesn't suck. (Statically typed, booya!)

Cheers,
Carson

Fred Garvin

Posts: 52
Nickname: fredgarvin
Registered: Jan, 2008

Re: Is Scala really more complicated than Java? Posted: Sep 24, 2009 12:20 PM
Reply to this message Reply
complicated [kom-pli-key-tid]
–adjective
1. composed of elaborately interconnected parts; complex
2. difficult to analyze, understand, explain, etc.:

val (d,t)=("Dear XXX","To you")
List(t,t,d,t) map ("Happy Birthday " + _) foreach println

Stephen Denne

Posts: 1
Nickname: spdenne
Registered: Feb, 2005

Re: Is Scala really more complicated than Java? Posted: Sep 24, 2009 7:32 PM
Reply to this message Reply
(0 to 3) would be a closer match to the java version.

For this case, (1 to 4) is clearer, since lines of a songs are referred to as the first line, second line, etc.

Do you think that there will there be a new breed of developers, blisfully unaware that memory accesses of the form location of structure + index * item size leads to counting from zero?

Ayman Saleem

Posts: 4
Nickname: ayman
Registered: Dec, 2005

Re: Is Scala really more complicated than Java? Posted: Sep 25, 2009 1:35 AM
Reply to this message Reply
> Which is what you would see in BASIC. Java has more than an it's fair share of "lore", it's just we seem to forget about that.


I agree that familiarity would play a role in how we understand things. However, while I'm a Java programmer, I would say it for ever that BASIC's loop is more readable than Java or C loops (i.e. familiarity is not be used in reasoning which is more complicated.


> Before we go on, I would like to point out that I also consider the ternary operator in Scala superior in readability to Java. Just compare:
>
>
> i == 3 ? "A" : "B"
>
> Kind of looks like line noise until you are used to it,
>

Somehow, I think your comparison is contradicted :-) that's because replacing for-from-to with 1 <- and i=> sounds more readable to you while using ternary operator ( ? and : ) looks noisy and you think it would be better to use if-then-else instead! I'm not sure whether you support using more or less English-like key words?



> I think the following would be the most
> compact and best signal to noise ratio:
>
>
> String[] birthdayArray = new String[4];
>
>
> for (int i = 0; i < 4; i++) { birthdayArray[i] = "Happy Birthday " + (i==2 ? "Dear XXX" : "To You"); }
>
>
> List<String> birthdaySong = Arrays.asList(birthdayArray);
>
>

I'm not sure how this could be the most compact version of Java code for popoulating a list with strings! Then, does compact mean more readble or less complicated??




> We could put all of these statements onto one line of course, but I think readability would suffer more.

Wouldn't you suffer when debugging similar Scala lines too? What about run-time errors in similar but dynamic single-lines?!



> MessageFormat.format("Happy Birthday {0} ", i == 2 ? "Dear XXX" : "To You");
>
>
> Scala:
>
> "Happy Birthday %s".format(if (i==3) "Dear XXX" else "To You")
>
>
> not that different, but the importing of MessageFormat and the use of a static method is orthogonal to the functionality we actually want. It adds nothing to the readability of the code, it's just more noise.


Why to import MessageFormat if you can use String.format? Then, if there is a purpose (which is printing in this context), then why not to use use out.format or out.printf??



> birthdaySong.foreach { println (_) }
>
> The _ is just shorthand for "the thing" (the only value available when iterating over the list). We could also have done:
>
> birthdaySong.foreach { line => println (line) }
>
> Perhaps the second is a little more readable, perhaps not after you get just a little more comfortable with the syntax.



I think using "line" as placeholder supports high level of readability and not just "little". That's because I think readability is not about being comfortable with the code. (for example, if I'm comfortable with assembly language, then I don't think I can tell others that it supports high readability). In all cases, this take us back to questioning your point of the ternary operator.



> In Java, even using String array as an intermediate, we get:

> String[] birthdayArray = new String[4];

> for (int i = 0; i < 4; i++) { birthdayArray[i] = MessageFormat.format("Happy Birthday {0}", i==2 ? "Dear XXX" : "To You"); }
>
> for (String line : birthdayArray) { System.out.println(line); }
>

Would the following be equivalent:


String[] birthdayArray = new String[4];
for (int i = 0; i < 4; i++) { System.out.println(birthdayArray[i] = String.format("Happy Birthday %s", i==2 ? "Dear XXX" : "To You")); }


>
> Can you still say Scala is more complex based on these examples? Is it not possible that it is just different?
>

I think using more symbols rather than English-like words degrades readability (just like the ternary operator). If we use these examples as a base for comparing Scala's code-complexity to Java, I think Java is a little bit more readable.
However, I tend to say they are just different because I don't think that two great languages can be compared using single and compacted lines (which are not recommended anyhow) especially when we recall that a scientific comparison would consider the coding context and purpose which play key roles in addressing all coding factors (productivity, maintainability, ...etc) and the impact on functional and nonfunctional requirements at run-time (we cannot leave everything to the garbage collector...etc).

Robert Evans

Posts: 11
Nickname: bobevans
Registered: Jun, 2003

Re: Is Scala really more complicated than Java? Posted: Sep 25, 2009 10:56 AM
Reply to this message Reply
I get and appreciate the point the article makes. However, I also can't help but think that as for complicated-ness, both lose in comparison to either of these two versions:

# print inline
4.times { |i| puts "Happy Birthday #{ i == 2 ? 'Dear XXX' : 'to you'}" }

# or, build up a list, then print it
puts (0..3).map { |i| "Happy Birthday #{ i == 2 ? 'Dear XXX' : 'to you'}" }

Vassil Dichev

Posts: 6
Nickname: 53870
Registered: Feb, 2008

Re: Is Scala really more complicated than Java? Posted: Sep 26, 2009 7:54 AM
Reply to this message Reply
So complicated means "composed of elaborately interconnected parts"? Well, I don't want to sound inflammatory, but any piece of software beyound "Hello world" will match this definition. So if we need the interconnected parts, at least we can hide them so that they are not visible at all times.

Ayman Saleem says "I think using more symbols rather than English-like words degrades readability", whereas Robert Evans gives a Ruby example, which is more concise at the cost of using more symbols. The regular Java guy will be at a loss what the #{} expansion does. It seems, though, that most people agree that complicated code will not look readable.

So I'll ask again, what does complicated mean:

* to the beginner: You could write code that closely resembles your Java coding style, and that's what Martin's example tries to show. Many of the operators have alternative readable method names, e.g. instead of "/:" and ":\" you can use foldLeft and foldRight; you can use explicit variable binding instead of the underscore character for closures, etc. Not only that, but you can skip much of the boilerplate:


def main(args: Array[String]) {
println("Hello, Scala!")
}


compared to the Java version:

    public static void main(String... args) {
        System.out.println("Goodbye, Java!");
    }


Here's some accidental complexity you don't need to care about. This is what the beginner will think: Why do I need to type public? What is void? What the heck is static used for? And where did System.out come from?

That's just one example where the details are unnecesarily exposed, and don't get me started on JavaBeans and properties.

* to the expert: this is where Scala shines. Using powerful abstractions, you can write libraries, which produce very concise and readable code (http://www.artima.com/weblogs/viewpost.jsp?thread=252702). Note that the beginner might not understand why the code works, but it's still fairly clear what it does and how to use it. An unnecessarily verbose version is more complicated than the short one. One example is the specs BDD framework:


List("hello") must not have size(2)


Let me ask: do you understand what this does? How many people understand how it works, though? I guess many more people will answer affirmatively to the first question than the second. Scala hides the complexity in a layer, where the user does not have to deal with it over and over and over again. Here's another example usage of lift-mapper by Apache ESME (disclaimer: I'm a committer):


Message.findAll(By(Message.author, user),
MaxRows(20),
OrderBy(Message.id, Descending)).


Can you tell what this does? Does it seem complicated? Best of all, a typo will not break your SQL query in ways which are hard to debug during run-time.

* to the maintainer: since Scala is more flexible than Java, it's easier to adapt your existing code so that it matches new requirements. One example is how Lift was able to replace the Scala actor library with its own implementation:

http://markmail.org/message/d77hls7ffwghrxgq

Another concurrency-related example is a parallel version of map. In the "Happy birthday" example, can you write a Java version, which executes in parallel with as little modification of the original code as possible? With Scala, you can mostly substitute List.map with Mapper.pmap in this 10-line Scala class:

http://debasishg.blogspot.com/2008/06/playing-around-with-parallel-maps-in.html

With Java, the best you can do is use the ExecutorService (since Java 5). Now try to rewrite your examples in Scala and Java so that they use Google's MapReduce:

http://scala-blogs.org/2008/09/scalable-language-and-scalable.html

Try to imagine the Java version, which can handle different concurrent implementations and ask yourself: will it look complicated?

So back to the definition of complexity. In the famous words of Larry Wall, a programming language should make "easy things easy and difficult things possible". I'll add one more thing: you should make your software also easy to evolve and maintain. That's what I think the complexity of software development really is all about. With Scala, you're able to hide complexity from the beginner without making it more complicated for the expert to create a flexible implementation.

Joshua Suereth

Posts: 1
Nickname: 57840
Registered: Aug, 2008

Re: Is Scala really more complicated than Java? Posted: Sep 27, 2009 8:58 AM
Reply to this message Reply
> Yes.
>
> Like it or not functional programming, no matter how
> wonderfully versatile you think closures are, or how foldl
> makes you feel warm and cuddly, is more complicated.
>
> It's more complicated because
> 1) It can use many, many ways to do the same thing, and
> all of them have the same affordability mostly.
> 2) Shifts the emphasis to data structure manipulation
> instead of primitive (or user made) types manipulation.
> 3) It compresses meaning so even the shortest n-grams can
> alter a program significantly (not that this couldn't be
> made with c or java for that matter, but you would need to
> really try, weather with scala and haskell it seems almost
> accidental). So you have to have a laser steel tipped
> intellect just so you are sure what a piece of code of
> doing.

I actually find most of your reasons beneifts, especially number 2. Except, you're not just mucking with data structures, you are using user types.

Honestly, the whole "It's complicated" thing is easy to say when looking at a language for the first time. Especially if it doesn't look like something you're familiar with. Just compare Perl to Haskell to Java to Scala. Complicated should be normalized by knowing the language *first* before declaring its complicatedness.

Infernoz Infernoz

Posts: 6
Nickname: infernoz
Registered: Nov, 2005

Re: Is Scala really more complicated than Java? Posted: Sep 28, 2009 6:10 PM
Reply to this message Reply
I'd joyfully mock this thread, with this Java variant:

String name = "me"
String[] a = {"Happy Birthday ","To You","Dear "+name};
for (int i : new int[] {1,1,2,1}) {
System.out.println(a[0] + a);
}

IMHO, The Scala version looks a pointless over abstraction, it makes a simple task more complicated, because of the article authors hubris.

It doesn't matter how much the article author thinks Scala is better for him, what really matters is which language the other developer can currently be most productive in. Learning curve can be a really sharp sand beach, especially if there are hidden holes in the documentation; as I discovered with ZK, Spring Security, and JSecurity

Infernoz Infernoz

Posts: 6
Nickname: infernoz
Registered: Nov, 2005

Re: Is Scala really more complicated than Java? Posted: Sep 28, 2009 6:17 PM
Reply to this message Reply
My mock code less the stupid formatting glitches:

String name = "me";
String[] a = {"Happy Birthday ","To You","Dear "+name};
for (int i : new int[] {1,1,2,1}) {
   System.out.println(a[0] + a[i]);
}


I hope.

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Is Scala really more complicated than Java? Posted: Sep 29, 2009 5:24 AM
Reply to this message Reply
An interesting article. Being experienced in Java and new to Scala, I'd say that it looks like Scala has a lot more ways of doing the same thing; some of which are more readable and some of which are more powerful. The trick is to find the right one with, hopefully, both properties and balance the need to optimise the code for both human readability (and maintainability) and computer efficiency.

> for (i <- 1 to 4) { }
>
>(I tend to read <- as "from") so: for i from 1 to 4 -
> - pretty readable and understandable, I would say moreso
> than the Java one, if you were cold on both languages.
> However, that's not the form I used, I instead used the
> more universal.
>
> (1 to 4).map { }

One minor question: What do you mean by referring to the latter example, above, as more "universal"?

Vince.

Flat View: This topic has 54 replies on 4 pages [ « | 1  2  3  4 | » ]
Topic: What are Your JUnit Pain Points, Really? Previous Topic   Next Topic Topic: Abstract Type Members versus Generic Type Parameters in Scala


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us