tags:

views:

1492

answers:

11

I am a student. I learned java during the 2nd year. Now i am in fourth year. I got bored with java and i started to learn Scala. As i learn it, i find it being very complex (although i love it). My question may apply to all new complex language.

Why scala is complex?
is it because we need to create complex softwares?
or i am the only one who thinks it is complex?

+3  A: 

It is as complex as Java (or any modern programming language) - I can only assume you have not seen large programs in Java.

It is not just Object Oriented but also Functional, which is a different programming paradigm.

Don't confuse your difficulty with Functional Programming with the language being hard. Functional Programming can be very confusing to those used to Object Oriented (or Procedural) programming.

Oded
+1 to counterbalance the uproar you may cause by saying Scala is not OO! :)
Daniel Earwicker
-1: it's absolutely not true that all modern languages are equally complex, nor that the complexity of large programs has anything to do with the complexity of the language they're written in.
Michael Borgwardt
±1: A program to perform a particular task has a particular characteristic complexity that is inherent to the task in question. The fitness of a language for that task is determined by the amount of *additional* complexity that is added over that inherent level. The manner in which that complexity is expressed is less important, as is whether that is part of the language or part of the explicitly written program. It's Turing Machines, all the way down.
Donal Fellows
+8  A: 

There are two questions here:

  1. Is it more difficult to learn Scala than Java?
  2. Is code written in Scala tends to be more complicated than code written in Java?

The first question is easier to answer: The Scala language is richer than Java. In particular, its type system is more expressive than Java which means that one can express more logical errors as compile time errors. However, in order to exploit these capabilities one needs to be acquainted with the different constructs of the language (dependent types, case classes, variance annotations, views, to name a few). Mastering these takes time and that's why Scala is more complicated to learn than Java.

The second question is trickier. Scala advocates claim that these new constructs make it easier to write correct programs and that the resulting code is simpler. Others are saying that Scala's additional power does not outweigh the complexity of understanding the sematnics of its constructs (For example, take a look at this talk. Search for "Scala"). This is a manifestation of broader dispute: that of statically vs. dynamically typed languages.

Itay
"the different constructs of the language (dependent types,[...]" Since when does Scala have dependent types?
sepp2k
http://programming-scala.labs.oreilly.com/ch12.html#PathDependentTypes
Itay
@Itay: I'd think that using "dependent types" to mean "path-dependent types" would be confusing to most people (at least it was to me).
sepp2k
The answer to the first question is dead on. Think of it as analogous to understanding hidden invariants in any medium-sized piece of code. A very simple analogy is Java 1.4, where you had to use comments and crawl the code in order to understand what type of elements could be inserted into a collection. Java 1.5 is quite a bit more complex, but when used appropriately, can make your code more robust.
lindydonna
+6  A: 

Scala is complex because it gives you flexibility. Time after time, not having enough flexibility makes it difficult to do some tasks, yet too much flexibility is like too much money, it empowers you to make the really big mistakes.

Scala is both Object Oriented and Functional. Both of these language types were once considered quite complex apart (although object oriented is now more mainstream) but putting them together opens all sorts of new doors. Some of those doors look like short cuts to "mission accomplished"! Some of those doors have lions behind them. Functional programming gives you all the rope to get the job done, hang yourself, and tie your neighbourhood up in knots for years. It's up to you to not injure yourself in a functional programming language.

The key to successful Scala is to recognize that you should be a successful object oriented programmer, a successful functional programmer, and then learn how to mix the two together in ways that get you to your goal. That's a lot of work. Perhaps in the future, people will know what is the "best" approach to learning Scala, but for now, the only approach known is to be good in two different approaches to progamming, AND be good in mixing them together.

Edwin Buck
Every paradigm provides rope "enough". The ways to hang yourself just varies.
Thorbjørn Ravn Andersen
+4  A: 

What is complex about Scala is the type system. It is very flexible, but unfortunately exposes this flexibility in complex type signatures.

Or it may be that you are finding the paradigm shift to using higher order functions complex.

Either way, I recommend you stick with it. It offers a degree of power over Java-the-language which cannot be passed up.

Synesso
+2  A: 

Scala provides the power to make libraries that can be used in a very terse way. Java libraries often have a verbose, cumbersome interface. This sounds like nothing but a plus for Scala, but not necessarily. In Scala, two functions can work completely different and yet appear very similar in usage - syntax is no guide to behaviour. Whereas in Java certain things are always made obvious by the syntax.

Suppose you have a function foo in Java and it is called like this:

foo(v > 5);

So we are apparently passing it a boolean value. Not so fast! Suppose Java had a feature that allowed the function foo to capture the expression and evaluate it later (perhaps on a different thread). So instead of accepting a boolean value, foo accepts a function that takes no parameters and returns a boolean. You can't tell what's happening by just looking at the call site; you have to know the details of how foo works in order to know when the v > 5 expression will be evaluated. Whereas in Java today, the expression will always be evaluated before foo is executed.

To Java programmers this would probably seem quite unnerving. But Scala has this exact feature.

This breaking of the link between syntax and behaviour is something that makes Scala more liable to confuse the unwary. On the other hand, it allows embedded domain specific languages to be created, where the whole point is that there is a terse, efficient notation suited to a specific problem domain.

Daniel Earwicker
+16  A: 

@Anantha For the last ten years most universities have been teaching their students Java as first language. I've heard of a strikingly high number of cases where it even remains the only language students get to learn while at college - unless they pick up something else on their own, that is.

Purely from a language viewpoint, Java's three most characterizing features are

  1. it's imperative
  2. it's object oriented
  3. it's garbage collected

Features 1 & 2 make it very similar to a wide array of languages from the Algol/C and C++ family. All of these languages either share similarities in their paradigm or even utilize exactly the same.

C# for instance, despite it's syntactic differences, Windows as main target OS and .NET framework as "class library", is very easy to pick up for a Java programmer. That is due to both languages sharing the same programming paradigm.

Scala on the other hand - despite running on the JVM, providing easy interoperability with Java APIs - is what is commonly referred to as a multi-paradigm language. The language provides deep syntactic integration of functional programming language features, yet structures code in an object oriented fashion.

The concept of functional programming - especially once you get into code beyond trivial tutorials - proves to be hard for devs who are only experienced with imperative, OO languages. My personal experience with helping fellow developers get up to speed on Scala et al is that it greatly helps to teach them Scheme first ;) It's a nice, small, clean Lisp dialect. It helps with conveying advanced functional concepts. If you decide to give it a shot, I recommend you have a look at "The Little Schemer" and "The Seasoned Schemer". Once you're through with both books, I bet you'll have an easier time looking right through Scala's syntax and seeing it's concepts more clearly.

In a nutshell: IMHO it's not Scala that is hard to learn, the functional programming paradigm is the reason for most devs, who've only been exposed to imperative languages, having a hard time getting up to speed.

codesurgeon
i wish my college should teach Scheme instead of J2EE.
Anantha Kumaran
Maybe your college has to balance teaching a language which students will feel that there is a "point" in learning (i.e. they can see that there are a ton of jobs out there which require said language) and hence will be attracted to the college, and teaching a language to make people *better* programmers. Basically, it's all the students' fault :-)
oxbow_lakes
I don't think it's bad for a college to focus on the current set of *working man's languages* - read: Java and C# - at all. No matter how much any of us prog lang geeks fan out over any other language, the success of imperative, OO and GC languages shows, that despite their shortcomings, a lot is right with them. Thankfully it's not as if Java and probably even more so C# would be standing still. Plus: both languages have a vibrant eco-system, which is *very* important. Colleges should teach many more languages and offer a mandatory compiler and/or language design lecture in their curricula.
codesurgeon
@codesurgeon Thanks for suggesting the little schemer book. I just finished the book and it was amazing. It helped me understand many concepts.
Anantha Kumaran
@anantha-kumaran Thanks for letting me know. I agree, it's a very different kind of programming book ... in the best sense. Plus, it's a lot of fun.
codesurgeon
+5  A: 

Scala is complex for several reasons:

It brings functional programming paradigms. The Scala Collection library is much reacher than Java's because of these.

It allows creating modular and fluent APIs. For example, in Scala, the method #map is implemented in TraversableLike, (one of the root classes in the collection library) yet, its result type is the type of the most appropriate concrete collection (for BitSet, it will return a BitSet if the mapping converts an Int to an Int and a Set otherwise). This is made possible by the language, not compiler trickery.

It is statically typed. It is easier to create a dynamic language that has the above features, but static typing gives you IDE support and performance.

IMHO all these features are very important and worth the additional complexity.

I was once at the point where you are. When I encountered Scala in 2006 I abandoned it after a while of trying to learn it, for its complexity. I had to learn it for a project I'm doing and am very happy that I did so. I consider not learning it in 2006 one of the biggest mistakes in my professional life.

IttayD
+6  A: 

Am I the only one who doesn't think it is complex? You can write complex stuff with it, sure. But the alternative would be not being able to write complex stuff, which isn't exactly an improvement. But I find the language itself very simple.

What I do think you are going through is the shock of learning a second language. You'd probably find C with its pointer arithmetics very complex.

Daniel
A: 

One of the most popular Scala questions pn Stack Overflow right now is about the complexity of the Scala 2.8 libraries. I suggest you read it.

Ken Bloom
+2  A: 

I'd suggest that you don't look at Scala as complex, just advanced. Scala represents a broad and surprisingly coherent advance on just about every aspect of conventional imperative programming languages, however each of these advances is easily absorbed and applied. Trying to adopt too many of Scala's improvements at once will probably result in confusion and damaged confidence.

Scala deserves mainstream adoption but seems to suffer from it's own intoxicating effects. Trying to be content with applying small improvements is very difficult in such a rich environment but don't be put off.

Don Mackenzie
A: 

I tis more complex because it needs to be to have all the power it has. Java is simpler, but Java also doesn't have the same amount of capabilities as Scala.

Java:

  • +less to learn
  • -closures
  • -type inference
  • -traits
  • -case statements
Alex Baranosky