开发者

Why is Scala very complex? [closed]

开发者 https://www.devze.com 2022-12-25 08:11 出处:网络
Closed. This question is opinion-based. It is not currently accepting answers. Closed 9 years ago. 开发者_如何学编程
Closed. This question is opinion-based. It is not currently accepting answers. Closed 9 years ago. 开发者_如何学编程 Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.

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?


@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.


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.


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.


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
**


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.


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.


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.


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.


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.


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.


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.


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


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消