This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 247772 - "Hints > General > Use Functional Operations" should not attempt to provide a solution
Summary: "Hints > General > Use Functional Operations" should not attempt to provide a...
Status: RESOLVED WONTFIX
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 8.0.1
Hardware: PC Windows 7
: P3 normal (vote)
Assignee: Svata Dedic
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-10-07 13:56 UTC by terje7601
Modified: 2016-06-21 21:06 UTC (History)
0 users

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description terje7601 2014-10-07 13:56:36 UTC
It's extremely hard for NetBeans (or any tool) to provide automatic conversion of for loops which results in optimal Streams API code. Sometimes it even results in broken code. So in my opinion, I think this hint should not attempt to provide a solution at all. Moreover, this hint will lead to users blindly converting all of their for-loops, complaining about the verbosity of the result, not realizing they ought to use ".collect(toList())" and the likes, introducing subtle concurrency bugs, ... in short: not learning to use the Streams API correctly.
In my opinion, the best NetBeans can do is to link to the relevant javadoc, e.g. http://docs.oracle.com/javase/8/docs/api/java/util/stream/Collectors.html or http://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html

For example, consider this snippet:

List<String> input = Arrays.asList("1", "2");
List<Integer> output = new ArrayList<>();
for(String i : input) {
    output.add(i.length());
}

NetBeans converts it to:

List<String> input = Arrays.asList("1", "2");
List<Integer> output = new ArrayList<>();
input.stream().forEach((i) -> {
    output.add(i.length());
});

While the optimal solution is:

List<String> input = Arrays.asList("1", "2");
List<Integer> output = input.stream().map(String::length).collect(toList());

Moreover, if someone later changes the NetBeans-converted code to use parallelStream() instead of stream(), the code is broken because multiple threads will modify "output" without synchronization.
Comment 1 Svata Dedic 2016-06-21 21:06:35 UTC
I agree that it is hard to transform the code right. The user may turn the 'Use functional operations' hint off.