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 ?
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.
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.
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 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 ?
posted 4 months ago
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?
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.
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.
All things are lawful, but not all things are profitable.
posted 4 months ago
. . . 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.