• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java lambdas and streams - Should we make it mandatory to use them in projects ?

 
Ranch Hand
Posts: 143
6
IntelliJ IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assume that we have Java projects where Java version can be updated to support lambdas and streams. In all new code in such projects, should we make it mandatory to use lambdas and streams wherever it is advisable to do so ? If time and money are not a problem, then should we re-write old code to use lambdas wherever possible ?

 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tom Joe wrote:. . . mandatory to use lambdas and streams wherever it is advisable to do so ? . . .

Can you see there is a contradiction in that phrase?

Maybe I should ask you whether you are advising people that even if it ain't broke, they should fix it regardless?

Adding to our λs forum.
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Caution against any re-writes unless you have excellent regression tests for your project(s). And maybe even performance tests.

For making lambda/streams mandatory, I'd say your team would need a review process where developers can review each others code snippets and make recommendations and perhaps put together some documentation of examples. Again, testing needs to be a priority.
 
Rancher
Posts: 285
14
Eclipse IDE C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lambdas can make your code become either more readable or less readable. it could go either way. the biggest benefit for lambas that i've seen in my code is that it reduces event triggers from a block to a single line of code. i don't know if that improves the performance or not, but my javaFX controller class is far less cluttered that way.

i'm not one who can talk much about streams, but if you're doing multi-threading you'll want to use parallel streams, and you need to be careful about the order of execution. serial streams are leaving performance on the table.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe in the back of your mind is the thought "Our programmers are not keeping up with new (and useful) features"? Quite a reasonable thought, really. I feel that way about myself sometimes too.

So time and money are not a problem? Ha ha ha... but okay, let's go with that. What I would suggest is, you could have group training where the group digs up old code and rewrites it to use lambdas and streams. You could certainly focus on old code which won't bring down the wrath of the CEO on your head if rewriting it happens to break things. After a while it should become second nature to your programmers to use streams and lambdas in places where they make things better.

But you may find you have some programmers who don't find that streams and lambdas improve things at all. You would probably get carefully written explanations/BS about why they are the devil's spawn. I don't know what you would want to do about those programmers.
 
Tom Joe
Ranch Hand
Posts: 143
6
IntelliJ IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Tom Joe wrote:. . . mandatory to use lambdas and streams wherever it is advisable to do so ? . . .

Can you see there is a contradiction in that phrase?

Maybe I should ask you whether you are advising people that even if it ain't broke, they should fix it regardless?

Adding to our λs forum.



Yes, I see how that sounds contradictory. I guess I should have used feasible instead of advisable. Perhaps old code could be left untouched, but only the new code could use lambdas and streams when its advisable ?
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tom Joe wrote:. . . I should have used feasible instead of advisable.

That wouldn't remove the ambiguity.

Perhaps old code could be left untouched . . .

I think Paul's suggestion is far better. Experiment with old code, preferably on a trial server, and see what happens. See how you can get rid of loops, for example by replacing the code with loops in the discussion of histograms by Streams throughout.
How much do you know about λs and Streams yourself?
 
Tom Joe
Ranch Hand
Posts: 143
6
IntelliJ IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:. . .



Thanks for sharing the histogram link. I know very little about lambdas and streams. I learned a bit of the basics from a text book and small online articles.

PS - As an aside, I could not find any resource with plenty of exercises on lambdas and streams. Without that, I bet it would be impossible to learn and remember concepts (see related post). I could create my own exercises or find random ones on forums. But, I don't have time to search and filter out bad or made up questions.


 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On any Java exercise website (like codewars or exercism) you can sometimes code the solution with lambdas and streams.  I have gone through some of those sites and first coded them procedurally, then I went back and tried to use lambdas and streams.  Then if you can look at other people's solutions, they would often use lambdas and streams too.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and 90+% of exercises using loops can have Streams substituted for the loops. Try all the posts in this forum using loops and see how far you get.
 
Ranch Hand
Posts: 128
Hibernate Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For a quick review on Lambdas, you can checkout the following Java 8 Lamdba basics
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had a quick look at that tutorial and it seemed quite good.
 
Tom Joe
Ranch Hand
Posts: 143
6
IntelliJ IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joseph Mokenela wrote:For a quick review on Lambdas, you can checkout the following Java 8 Lamdba basics



That is an excellent tutorial. I had first learned lambdas from there. Currently, Kaushik Kothugal has not created any tutorials on streams and that is what I need.
 
reply
    Bookmark Topic Watch Topic
  • New Topic