17

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

Why scala is so complex?
Is it because we need to create complex software, or am I the only one who thinks it is complex?

Machavity
  • 30,841
  • 27
  • 92
  • 100
Anantha Kumaran
  • 10,101
  • 8
  • 33
  • 36

15 Answers15

37

@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
  • 688
  • 5
  • 8
  • 11
    i wish my college should teach Scheme instead of J2EE. – Anantha Kumaran Apr 04 '10 at 16:05
  • 1
    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 Apr 04 '10 at 16:30
  • 3
    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 Apr 05 '10 at 09:31
  • 1
    @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 Apr 18 '10 at 09:57
  • @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 Apr 19 '10 at 06:16
  • Its not hard to learn Scala because of FP. I repeat its not hard to learn because of FP. Its hard to learn Scala, Haskell or Ocaml because of their advanced type systems. – Adam Gent Apr 29 '11 at 21:27
13

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 Maman
  • 30,277
  • 10
  • 88
  • 118
  • 1
    "the different constructs of the language (dependent types,[...]" Since when does Scala have dependent types? – sepp2k Apr 04 '10 at 12:55
  • 1
    http://programming-scala.labs.oreilly.com/ch12.html#PathDependentTypes – Itay Maman Apr 04 '10 at 12:59
  • 7
    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 Apr 04 '10 at 13:22
  • 2
    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 Apr 05 '10 at 13:53
8

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
  • 69,361
  • 7
  • 100
  • 138
8

If you think the language is complex then you might consider learning it in stages.

This guide to breaking up Scala into different parts is what many people do instinctively and usually necessarily, but this guide is written by Martin (Scala's creator) so it has his unique perspective. http://www.scala-lang.org/node/8610

Notice that most of the constructs that freak people out are in the Library designer levels. The intermediate application developer can get a lot done without too many CS grad-school concepts.

**
            Level A1: Beginning application programmer
                Java-like statements and expressions: standard operators, 
                     method calls, conditionals, loops, try/catch
                class, object, def, val, var, import, package
                Infix notation for method calls
                Simple closures
                Collections with map, filter, etc
                for-expressions

            Level A2: Intermediate application programmer

                Pattern matching
                Trait composition
                Recursion, in particular tail recursion
                XML literals

            Level A3: Expert application programmer

                Folds, i.e. methods such as foldLeft, foldRight
                Streams and other lazy data structures
                Actors
                Combinator parsers

            Level L1: Junior library designer

                Type parameters
                Traits
                Lazy vals
                Control abstraction, currying
                By-name parameters

            Level L2: Senior library designer

                Variance annotations
                Existential types (e.g., to interface with Java wildcards)
                Self type annotations and the cake pattern for dependency injection
                Structural types (aka static duck typing)
                Defining map/flatmap/withFilter for new kinds of for-expressions
                Extractors

            Level L3: Expert library designer

                Early initializers
                Abstract types
                Implicit definitions
                Higher-kinded types
**
Jesse
  • 479
  • 1
  • 5
  • 13
7

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
  • 37,610
  • 35
  • 136
  • 207
7

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 C. Sobral
  • 295,120
  • 86
  • 501
  • 681
  • 5
    I agree. Scala is simple when you want to write simple stuff. C is probably a perfect poster child for the "opposite of Scala" - it's complex when you want to write simple stuff, making you scratch your head wondering about the difference between an ampersand in front of `const` and an ampersand after `const`. –  Dec 29 '10 at 02:16
6

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
  • 7,953
  • 7
  • 31
  • 32
5

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
  • 28,271
  • 28
  • 124
  • 178
4

Java is distinctive in strongly encouraging a specific style of writing. If you take code samples from two professional programmers, Java is perhaps the only language where you probably wouldn't be able to tell them apart. One reason for this is that Java is relatively inflexible, so for many problems there's only one way to do things.

Scala is far more flexible, but that results in many ways to do the same things. For example, to populate a Map from another Map you can iterate (Java, imperative style) or you can apply a closure (functional style). The latter is likely to be more compact, but more cryptic if you are used to imperative languages. To make matters worse, Scala has many shortcuts to make the functional style even more compact-- potentially more readable to experts, but more cryptic to non-experts.

To put it another way, Scala is a language for writing domain-specific languages. It gives you the tools to add keywords for your particular domain. You can, for example, easily turn Scala into a great language for accounting or particle physics. But then the accountants and the physicists will have trouble reading each others' programs.

Unfortunately, one such domain is collections. You can write Java-style Map, Set, and List code. Or you can use some more cryptic code which can be much more clear and concise-- once you've learned all the tricks.

I don't think any language can have it both ways.

David Leppik
  • 3,194
  • 29
  • 18
3

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
  • 489,969
  • 99
  • 883
  • 1,009
  • +1 to counterbalance the uproar you may cause by saying Scala is not OO! :) – Daniel Earwicker Apr 04 '10 at 08:35
  • 8
    -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 Apr 04 '10 at 08:38
  • 3
    ±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 Apr 04 '10 at 10:45
  • Scala is way more complicated than Java, you really can't be serious by saying the contrary. First, the object oriented model is more complex than Java's (controllable type variance, traits, multiple inheritance, abstract values...). Just take a look at the collection's type hierarchy in Scala, it's magnitudes more complex than Java's. Then you add advanced functional features: higher-order functions, higher-kinded types, currying, laziness, streams... Finally, you add compile-time tricks: macros, implicit resolution... The result is a language that is much more complex than Java is. – Dici Dec 10 '17 at 22:34
3

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
  • 114,894
  • 38
  • 205
  • 284
3

My kids can ride a bike, my mom can't

My mom can drive a car, my kids can't

Scala is not complex - it is different

Jack
  • 16,506
  • 19
  • 100
  • 167
1

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
  • 48,865
  • 44
  • 102
  • 150
0

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.

Community
  • 1
  • 1
Ken Bloom
  • 57,498
  • 14
  • 111
  • 168
0

As has been stated above, the reason Scala is so complex is because of it's type system. It's an impressive piece of engineering, but even expert Scala programmers get tripped up by it. I'd strongly recommend that anyone considering learning and using Scala take a long look at Clojure first. It offers pretty much all the features and benefits of Scala without the ongoing nightmare of having to fight with the type system.

bfancher
  • 469
  • 1
  • 4
  • 4
  • 1
    I'm not sure why this was voted down. Not having programmed in scheme, I believe what I've heard. That is it is a nice small language to learn and lends itself to learning functional programming. Probably, you cannot go wrong learning scheme. However, I agree with this answer [about learning Clojure], because being a JVM language, it puts Scala, Java, and Clojure at the same table as "cousin" languages. What you know about Java can and does help you in Clojure (and probably Scala, too). – octopusgrabbus Nov 04 '11 at 14:16
  • I guess the down-voting comes from the poor information-to-advertisement ratio, the uncalled advertisement itself and the unproven claim that “[Clojure] offers pretty much all the features and benefits of Scala [...]”. – soc Nov 26 '13 at 22:37