• (nodebb)

    Wow. O(N^3) on the number of visitors. Excellent.

    Overall, it's O((N^3)*P*Q*R) where N is the number of visitors, P is the number of schedules, Q is the average number of days per schedule, and R is the average number of slots per day, and it could be frist, oops, O(N*P*Q*R).

  • comments.stream().findFirst().orElseThrow(new NotFristException()) (unregistered)

    To be fair to Clarence, streams are a bit confusing if you're not used to them and haven't been exposed to FP or similar concepts in e.g. JavaScript. Evaluate-on-demand in particular is something that people often trip over.

    Not that that excuses re-iterating the same collection multiple times for an O(n^3) WTF.

    This is why we have code reviews though. Everyone has brain farts now and then.

  • MiserableOldGit (unregistered) in reply to comments.stream().findFirst().orElseThrow(new NotFristException())
    Everyone has brain farts now and then.

    Oh indeed, twice daily in my case, but this is full on cerebral explosive diarrhoea.

  • Naomi (unregistered)

    It creates too many copies of our objects, so it’s terrible for memory, and it’s so much slower.

    Eeeeeeesh... maybe I shouldn't read TDWTF before my morning coffee, but this really got on all my nerves at once. It's so ignorant! Java doesn't have C++-style copy constructors - I mean, of course you can write a copy constructor (all the built-in collection implementations have them!), but it won't get invoked automatically, so that's one that's already way off. As for the last, streams can be more performant than loops, so that's two, and an extra bonus WTF for giving implementation-level performance advice without profiling.

    But the middle point is honestly the worst, because it's legitimate misinformation. The idea that creating temporary objects was Bad and To Be Avoided was good advice several JVM versions ago. These days, so much R&D has gone into memory management/the garbage collector/escape analysis (okay, escape analysis won't help here, but I'm mentioning it for completeness's sake).

    comments.stream().findFirst().orElseThrow(new NotFristException())

    Btw,orElseThrow takes a Supplier<X extends Throwable> so it can avoid filling in the stack trace for no reason. orElseThrow(() -> new NotFristException()).

  • JB (unregistered)

    TRWTF is using streams when all you need is a for loop. They're a powerful tool when you want e.g. a map()/filter()/collect() pipeline, but in this example they contribute exactly nothing (note that Java also supports someCollection.forEach(item -> { ... }) syntax).

    It's also funny how they've doubled up the forEach loops on each line to try to disguise how deeply nested the code really is :D

  • (nodebb) in reply to Naomi

    escape analysis won't help here

    It will if I run fast enough!

  • Airdrik (unregistered)

    Another bonus is that when they added streams to java, they also added a little-known forEach method to Iterable, which means that all of those calls to .stream() are superfluous anyway (as well as the isEmpty checks because looping over an empty iterable is already comparatively free/cheap).

  • Naomi (unregistered) in reply to dkf

    escape analysis won't help here It will if I run fast enough!

    You need to optimize the stream pipeline before it can run fast at all!

  • Naomi (unregistered)

    TRWTF remains the lack of a preview function, Remy!

    :P

  • Wizofaus (unregistered)

    When I first saw streams in use it looked like Java's answer to LINQ->SQL and we happily used it everywhere to avoid messy/hard-to-test-and-debug HSQL queries. Then we discovered what happened when used on a table with millions of rows :facepalm:

  • Bubba (unregistered)

    This site is dying a painful death

  • Functional (unregistered)

    Monads or applicatives?

    Monads process input to build a function. Applicatives build a function to process input.

    Sequence or transverse?

    Sequence will error out the entire array. Transverse will error out the individual items in the array.

    That's all the decisions you really need.

  • JJ (unregistered) in reply to dkf

    You can run, but you can't hide :-)

Leave a comment on “A Slow Moving Stream”

Log In or post as a guest

Replying to comment #516665:

« Return to Article