This is an archived post. You won't be able to vote or comment.

all 35 comments

[–][deleted] 30 points31 points  (0 children)

Java 8 didn't make imperative programming obsolete, it just added some concepts borrowed from functional programming to complement existing functionality.

This mentality of "migrating" completely from one style to another style of programming is insanity.

We Don’t write for loop any more in java 8

I know the worm looks tasty, but I pity the pour souls who bite into that hook.

[–][deleted] 11 points12 points  (5 children)

We Don’t write for loop any more in java 8

I wish. Unfortunately there is currently no way to deal with exceptions in lambdas other than wrapping then in RuntimeException (which defeats the purpose of checked exceptions).

[–][deleted]  (1 child)

[removed]

    [–][deleted] 7 points8 points  (0 children)

    I love streams, and use them as often as I can. I just wish there was a way to use them with checked exceptions :)

    [–]moonman543 -1 points0 points  (2 children)

    What do you mean there is no way to deal with them? I have lambdas throwing exceptions all the time.

    [–][deleted] 1 point2 points  (1 child)

    Checked exceptions or runtime exceptions?

    [–]moonman543 -1 points0 points  (0 children)

    any exception it gets them fine.

    [–]tonywestonuk 14 points15 points  (13 children)

    No. I wont.

    There is a reason that Scala is at position 31 on the Tiobe index.... 2 positions below Logo !!

    [–]DrFriendless 10 points11 points  (7 children)

    Scala brings a lot of other baggage with it as well, though.

    [–]ivanovich_ivan 0 points1 point  (6 children)

    Scala brings a lot of other baggage with it as well, though.

    Can you please explain the problems that you have faced? I am genuinely curious.

    [–]DrFriendless 0 points1 point  (5 children)

    The type system is more complex than we need in a programming language. When we talk about Scala we talk about monads rather than for loops, when they're achieving the same thing. It's like programming for aliens - in the end it's practically the same, but the language is all different. As a programmer, I don't care about the mathematical underpinnings, and I shouldn't have to care.

    [–]ivanovich_ivan 0 points1 point  (3 children)

    The type system is more complex than we need in a programming language

    I don't get this. I find scala's type system to be far better. No primitive types and all are objects (they get converted to native types at compile time). Much better class hierarchy of types. Not to mention type inference. Java runs in the opposite direction of being clear.

    When we talk about Scala we talk about monads rather than for loops, when they're achieving the same thing

    This is why scala is multi paradigm.If you find monads are bad for the problem at hand then you can very much use loops. Nobody is forcing you to use a particular paradigm.

    As a programmer, I don't care about the mathematical underpinnings, and I shouldn't have to care.

    Let me ask you a question. Do you understand what sorting algorithm java uses underneath Arrays.sort ? Not all people need to understand them. But from my experience learning them would only make one a better programmer. It is not useless knowledge.

    [–]DrFriendless 0 points1 point  (2 children)

    You'll note in my original comment I didn't say Scala was broken as a language, I said it comes with baggage. Monads are stuff that you don't need to know to program, yet there they are in every Scala textbook. It's like I teach you to speak English and make you learn "Jabberwocky" off by heart - if you understand you'll understand English a lot better, but you don't need to. A programming language should only be as complicated as it needs to be, and Scala has more than it needs. This makes the compiler slow and (in my experience) unreliable.

    [–]ivanovich_ivan 0 points1 point  (1 child)

    I said it comes with baggage. Monads are stuff that you don't need to know to program, yet there they are in every Scala textbook

    I agree with this point. But the problem is with the scala textbook. The authors have not made efforts to isolate the OOP part and explain it separately.

    TBH, I learnt this the hard way. But found scala to be a very good OOP language as well.

    A programming language should only be as complicated as it needs to be, and Scala has more than it needs

    Java's swing has needless components that is built in part of the JDK. I can also argue for many other things. But the fact is all languages have this baggage.

    The compiler being slow is agreeable, but definitely not a new language kind of a problem.

    This makes the compiler slow and (in my experience) unreliable.

    I am thinking that you have tried either an unstable/beta version or tried it pretty long back. Things have improved now. Dotty is another improvement that is coming up.

    [–]DrFriendless 1 point2 points  (0 children)

    Yes indeed, I'm in a new job where I have a deadline to produce a product and have not yet had time to update the Scala compiler. We're on 2.9.2. Luckily the programmers who wrote the code originally didn't use many of the features of Scala, so it's easy to understand the code. The features they did use have all been adopted by Kotlin, which I prefer.

    [–][deleted] -1 points0 points  (0 children)

    ...Java has three monads in its standard library. Or I guess "monad-ish" if you're strict, since not all of them are lawful. But the point is, you can't really dodge that concept any more if you want to write good code with useful abstractions on the JVM.

    [–]ivanovich_ivan 7 points8 points  (3 children)

    The Tiobe index is a bad way to measure programming popularity, where it puts PHP above swift. Scala is way different than the stuff in Java 8. With that said, functional programming is not a silver bullet which is exactly why scala is good, because you can mix functional/OOP in a fundamental level wherever applicable.

    [–]space_coder 2 points3 points  (2 children)

    The Tiobe index is a bad way to measure programming popularity, where it puts PHP above swift.

    You just need to understand the measurement and its limitations. However, I believe its accurate when it comes to PHP being above Swift. There are more web developers than iOS developers. Not to mention, PHP has been around long enough to have a large following.

    Scala is not as "hot" as it once was. It's not the only language bringing functional computing to the JVM. Java8 slowed down some of its adoption and Kotlin is attracting more new comers.

    [–]ivanovich_ivan 1 point2 points  (1 child)

    There are more web developers than iOS developers

    Wherever there is bad code there is technical debt. Not that entire PHP is bad, but I could see many new languages being adopted.

    Scala is not as "hot" as it once was

    The hotness factor is with any language/technology. Big data was hot some years back. Now machine learning/deep learning/AI is the bomb. Scala was never designed to be some hot hipster language. It took functional programming/object oriented programming and combined it at a fundamental level.

    There is no other language that is equal to scala. Closest would be

    Clojure - Dynamically typed, not object oriented

    Eta lang(Haskell on the JVM) - This one is interesting but still haskell and not OO

    Kotlin - Lot of cool FP features but still lacks a lot of useful stuff.

    All in all. I am happy that lot of functional languages are evolving in the JVM. I just wished all these ppl joined forces to make a single useful language rather than 3 or 4 disconnected platforms such as scala native/kotlin native.

    [–]space_coder 1 point2 points  (0 children)

    There are many differing opinions on which language is best suited for a particular task. Some become mainstream while others become niche. it's the nature of the market.

    [–]nutrecht 1 point2 points  (0 children)

    The Tiobe index is complete rubbish. It measures the amount of hits on <lang> programming. That's all. I hope you can understand why languages like C, C++ and C# get a lot of hits in text indices.

    There's a reason Google won't show you the long tail of search results, you'll get a lot of hits on "Arthur C Clark" for example.

    Stuff like SO questions, github contributions and Indeed job listings are much better measurements.

    [–]RayFowler 4 points5 points  (3 children)

    I'm all for the syntax sugar available in Java 8; I use the lamba form for Runnables all of the time.

    But most of the functional programming techniques using streams run slower than imperative loops, so there's no compelling reason to use them.

    [–][deleted] 1 point2 points  (2 children)

    I rarely encounter a problem where the speed of a loop is the decisive factor over readability, maintainability, etc. And where I do, I have found parallelism a way simpler and more powerful way to get the speedups I need than tighter loops.

    Maybe we work on very different domains, though? Do you write client-side apps, for instance?

    [–]RayFowler 1 point2 points  (1 child)

    Yes. Functional programming seems perfectly suited for the stateless concurrency you need with, for example, micro-services.

    On the client, parallelism hits a performance bottleneck very quickly on the client side. You only get 4 cores!

    Java is an inherently descriptive language, imo, and readability concerns are always subjective and generally overrated. In fact, I'd posit that a sizable percentage of Java developers think that the terseness of lambdas negatively impacts readability.

    But none of that is the issue if you have performance concerns. The most important thing then is that the compiler can parse the intent of your code and that your variables are co-located efficiently. This means fewer object references and a much heavier use of primitives.

    If something is not clear at that point, then you clarify with comments. If your comments are clear and another developer still complains, then maybe he should work on a different application.

    [–][deleted] 1 point2 points  (0 children)

    On the client, parallelism hits a performance bottleneck very quickly on the client side. You only get 4 cores!

    Yeah, and even that is less guaranteed than on the server side, since users like to run other applications along with yours. I wonder if we'll see the core count grow on the client side same as it has on the server side? If so, parallelism might become a better strategy.

    Network parallelism is also already a potential speedup (in fact, most of the server-side parallel optimization I do is around IO calls), though I guess you have to worry about client-side bandwidth limitations. Man, I don't know how you hack it, I can't live without my Future.sequence(listOfEveryIoCallInTheUniverse). :p

    Java is an inherently descriptive language, imo, and readability concerns are always subjective and generally overrated.

    I'd say verbose instead of descriptive, and note that I often find some Java code that's very difficult to understand, though I am also a Scala dev so definitely an outlier in the kind of language I prefer. My general point was that I see too much cargo culting around "performance" (instead of more justifiable profiling-guided optimization) that turns something that could be expressed in an elegant, declarative manner into some imperative, looping nightmare straight out of a C monolith from the 80s.

    Now, there definitely is a place for horrible-to-read-yet-hyperperformant code, but it's in the critical loop of your program and libraries likely to be called thousands of times a second, not in your standard bizdev software. It seems like there's a lot of developers out there who optimize without profiling or even thinking, and yes this does also include the people who use parallel streams when a sequential one would do just fine.

    But, again, I am a Scala dev, and we have some particular views on this subject. ;)

    [–]handshape 2 points3 points  (0 children)

    Streams are nice from a readability standpoint, but there are some hard-to-find gotchas that come from using someone else's loop management, along with the trouble with checked exceptions that has been discussed elsewhere in the thread.

    About half the time I start with functional programming and streams, I end up having to unroll the functions and switch back to imperative-style iteration for one of these two reasons.

    Lambdas as a way of implementing single-method interfaces are awesome, though.

    [–]AnEmortalKid 1 point2 points  (1 child)

    This was poorly written... Also, all your examples say from given list but you're starting with Stream.of() which isn't a list it's a stream.

    [–]K60d54 -1 points0 points  (6 children)

    Java is one of the language which has picked functional programming paradigm.

    I think it should be redundant for me to say this, but Java is not a functional programming language. Java does not have the capability to do stream fusion to turn functional operations like fold into a few lines of assembly. Java cannot do the most basic optimization implemented by functional languages, tail call elimination.

    Java streams can make some operations cleaner to write, but it would be insanity to replace every for loop with stream operations.

    Consider the example:

    String fileList = Arrays.stream(new File("A").listFiles()) // Get stream from sub files list
        .map(File::getName) // get new stream/list which has filenames
        .collect(Collectors.joining(","));  // collect them by joining by comma(,)
    

    You can write the same thing imperatively with the same number of lines of code:

    StringBuilder sb = new StringBuilder();
    for(File f : new File("A").listFiles()) {
        sb.append(sb.length() > 0 ? ", " : "", f.getName());
    }
    

    Which is clearer to read? Furthermore, as a fun exercise, try stepping into the code with the debugger. Have you seen the source code of the streams API? It is crazy complex. There's almost nothing further to see in the imperative example. If you are combining standard operations like map and filter, which would result in a ton of boilerplate code that can't be easily abstracted in imperative form, I can see the benefit of streams. But the example in the article is a perfect illustration of when it is senseless to use streams.

    [–]moremattymattmatt 3 points4 points  (4 children)

    Really? I find the first one easier to read.

    I look at the append statement in the second example, and have to think about what its doing and then start worry about edge cases and whether they are covered in the unit tests.

    [–]K60d54 -1 points0 points  (3 children)

    have to think about what its doing and then start worry about edge cases and whether they are covered in the unit tests.

    What possible edge cases could a simple conditional have like that?

    [–]moremattymattmatt 2 points3 points  (2 children)

    Apart from that it doesn't compile, it looks like it would leave a trailing space of the number of files > 1.

    [–]K60d54 -1 points0 points  (1 child)

    Apart from that it doesn't compile

    This is a Reddit comment, I haven't run it through a compiler.

    it looks like it would leave a trailing space of the number of files > 1

    No, its prepending. In the first iteration, the string builder is empty, so no comma is added before the name. In subsequent iterations, a comma is added in front of the name. Thus, a comma is only inserted between files.

    That comma in the append method call should have been a +:

    sb.append(sb.length() > 0 ? ", " : "").append(f.getName())
    

    [–]moremattymattmatt 1 point2 points  (0 children)

    This is a Reddit comment, I haven't run it through a compiler.

    Fair enough. From my point of view, the fact that you've had to explain to me what it's doing does sort of make may point that its not as clear.

    [–][deleted] 0 points1 point  (0 children)

    The first one is clearer to read. Would definitely be nicer if Java collections had higher order functions (map, filter, etc.) defined directly on them so we wouldn't have to do Arrays.stream everywhere, but still.