Article

3 Reasons why You Shouldn’t Replace Your for-loops by Stream

Topic: Coach Training and CertificationPublished January 23, 2018
No ratings yet587 viewsSign in to rate
Java developers will need to practice, and come up with an intuitive understanding of when to use FP, and when to stick with OO/imperative. With the right amount of practice, combining both will help us improve our software.rn1. Performance – you will lose on itrnIn many cases, performance is not critical, and you shouldn’t do any premature optimisation – so you may claim that this argument is not really an argument per se. But I will counter this attitude in this case, saying that the overhead of Stream.forEach() compared to an ordinary for loop is so significant in general that using it by default will just pile up a lot of useless CPU cycles across all of your application. If we’re talking about 10%-20% more CPU consumption just based on the choice of loop style, then we did something fundamentally wrong. Yes – individual loops don’t matter, but the load on the overall system could have been avoided.rnAn entirely different aspect of scaling is about scaling performance, i.e. to make sure that an algorithm that works for 1 piece of information will also work well for 10 pieces, or 100 pieces, or millions. Whether this type of scaling is feasible is best described by Big O Notation. Latency is the killer when scaling performance. You want to do everything possible to keep all calculation on a single machine. This is often also referred to as scaling uprnHere’s Angelika’s benchmark result on an ordinary loop, finding the max value in a list of boxed ints:rnArrayList, for-loop : 6.55 msrnArrayList, seq. stream: 8.33 msrnIn other cases, when we’re performing relatively easy calculations on primitive data types, we absolutely SHOULD fall back to the classic for loop (and preferably to arrays, rather than collections).rnHere’s Angelika’s benchmark result on an ordinary loop, finding the max value in an array of primitive ints:rnint-array, for-loop : 0.36 msrnint-array, seq. stream: 5.35 msrnPremature optimisation is not good, but cargo-culting the avoidance of premature optimisation is even worse. It’s important to reflect on what context we’re in, and to make the right decisions in such a context. rn2. Readability – for most people, at leastrnWe’re software engineers. We’ll always discuss style of our code as if it really mattered. For instance, whitespace, or curly braces.rnThe reason why we do so is because maintenance of software is hard. Especially of code written by someone else. A long time ago. Who probably wrote only C code before switching to Java.rnThings start getting a bit more interesting and unusual. I’m not saying “worse”. It’s a matter of practice and of habit. And there isn’t a black/white answer to the problem. But if the rest of the code base is imperative (and it probably is), then nesting range declarations and forEach() calls, and lambdas is certainly unusual, generating cognitive friction in the team.rnYou can construct examples where an imperative approach really feels more awkward than the equivalent functional one, as exposed here:rn3. Maintainability Difficult-to-maintain source code is a big problem in software development today, leading to costly delays and defects. Read the first chapter from Building Maintainable Software, Java Edition, to learn exactly what maintainability means and how it has an impact on business.rnIf you have work in large organization e.g. Investment banks e.g. Barclays, Citibank, or big Insurance companies you will find how important is support and maintenance of production applications are. There are several more software engineers working in support role then programmers developing software. You have L1, L2, and L3 support and then dedicated support team for every application. This means support is really important. But, when programmers develop an application, they focus on features and speed of development e.g. how quickly a feature is ready for testing etc, and to achieve these many programmers including myself in past often ignore things which matter a lot in production. From a maintenance perspective, a functional programming style can be much harder than imperative programming – especially when you blindly mix the two styles in legacy code.rnConclusionrnThis is usually a pro-functional programming, pro-declarative programming blog. We love lambdas. We love SQL. And combined, they can produce miracles.rnBut when you migrate to Java 8 and contemplate using a more functional style in your code, beware that FP is not always better – for various reasons. In fact, it is never “better”, it is just different and allows us to reason about problems differently.

Further reading

Further Reading

4 total

Article

How Coaching Turns Knowledge Into Change Walk into any bookstore or scroll through your favorite podcast feed, and you’ll see the same thing: endless tips, strategies, and “life hacks” promising transformation.rnWe devour them. We highlight paragraphs. We even try to practice what we’ve learned. But somehow, the big shifts never stick. Weeks later, the old patterns creep back in.rnSo why does it happen? Why do so many smart, motivated people keep getting stuck — eve

October 6, 2025

Article

grinding and cutting a variety of materials across numerous industries. Understanding their construction, types and safety precautions is mandatory for anyone working with these powerful tools. This article will explore the basics of abrasive wheels, offering insights into their components, maintenance and legal requirements to ensure both effective and safe usage. Exploring the Basics of Abrasive Wheels Abrasive wheels are critical tools in various industrial applications, f

March 6, 2025

Article

The internet we use daily, known as the surface web, represents only a fraction of the entire digital landscape. Beneath this visible layer lies the deep web and the dark web, where anonymity, privacy, and unrestricted information exchange thrive. Among the most well-known directories for accessing dark web content is the Uncensored Hidden Wiki . This article delves into the history, significance, risks, and access methods of the Uncensored Hidden Wiki, providing a comprehens

January 31, 2025

Article

If you're searching for exceptional piano classes near you, there are several excellent options in Toronto to consider. For those eager to learn the piano, finding a school or instructor that offers expert guidance, comprehensive lesson plans, and a supportive environment is key. Catering to both beginners and advanced players, many schools in the Toronto area provide tailored lessons to help students achieve their musical aspirations. What to Look for in Piano Lessons When

January 1, 2025