You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@netbeans.apache.org by John <jb...@gmail.com> on 2022/11/28 14:00:47 UTC

NetBeans `Convert to for (Iterator...) {}' suggestion

Hi,



Quick one. I have just installed a vanilla Netbeans 15 and when I write a line
such as



for (Word word : this.dictionary) {...}



Netbeans offers me a Warning / Recommendation



`Convert to for (Iterator...) {}'



That, if chosen, converts my syntax to



for (Iterator<Word> it = this.dictionary.iterator(); it.hasNext();) {...}



This is a lot less readable to me and I therefore wanted to turn off this
hint.



However, The <alt><ret> doesn’t offer the option to disable this hint and I
couldn’t find it in the Tools > Options > Editor > Hints (Java) list, although
I found lots of other nice hints that I have turned on!



I found this on Stack Overflow



<https://stackoverflow.com/questions/24263479/netbeans-convert-to-for-
iterator-suggestion>



But that was to do with which one is more efficient and I suspect that the
warning is due to the fact that the more verbose version is quicker if the
number of elements in the iterator is very large.



Am I stuck with this, or is it that I just overlooked where the hint is in the
long list?





John



\--------------------------------------------------------------------- To
unsubscribe, e-mail: users-unsubscribe@netbeans.apache.org For additional
commands, e-mail: users-help@netbeans.apache.org For further information about
the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


Re: NetBeans `Convert to for (Iterator...) {}' suggestion

Posted by Michael Bien <mb...@gmail.com>.
Hi John,

suggestions which are only active at the current line where your cursor 
is, don't necessary produce better code, they are just there to 
transform parts of it in case you want to change something. Internally 
they are hints of a kind called actions.

The doc describes it well:

"The hint (with kind ACTION) represents an offer to the user to 
automatically alter the code. The transformation is not intended to 
improve the code, only allow the user to do some kind of code 
transformation quickly. The only meaningful severity for suggestions if 
Severity.CURRENT_LINE_WARNING."

You should notice that if you change the loop to the iterator version, 
but not actually use the iterator except calling next() it will also add 
a hint called "convert to for-loop" which does the opposite. This hint 
will always appear, even if you move the cursor, since unless you 
actually need the iterator the simple for loop is more readable and 
should be preferred as you said.

the for-each loop uses a hidden iterator if it iterates over iterables 
anyway.

I don't know why you can't disable this particular suggestion via the 
context menu.

regards,
michael


On 28.11.22 15:00, John wrote:
>
> Hi,
>
> Quick one. I have just installed a vanilla Netbeans 15 and when I 
> write a line such as
>
> for (Word word : this.dictionary) {...}
>
> Netbeans offers me a Warning / Recommendation
>
> `Convert to for (Iterator...) {}'
>
> That, if chosen, converts my syntax to
>
> for (Iterator<Word> it = this.dictionary.iterator(); it.hasNext();) {...}
>
> This is a lot less readable to me and I therefore wanted to turn off 
> this hint.
>
> However, The <alt><ret> doesn’t offer the option to disable this hint 
> and I couldn’t find it in the Tools > Options > Editor > Hints (Java) 
> list, although I found lots of other nice hints that I have turned on!
>
> I found this on Stack Overflow
>
> https://stackoverflow.com/questions/24263479/netbeans-convert-to-for-iterator-suggestion
>
> But that was to do with which one is more efficient and I suspect that 
> the warning is due to the fact that the more verbose version is 
> quicker if the number of elements in the iterator is very large.
>
> Am I stuck with this, or is it that I just overlooked where the hint 
> is in the long list?
>
> John
>
> --------------------------------------------------------------------- 
> To unsubscribe, e-mail: users-unsubscribe@netbeans.apache.org For 
> additional commands, e-mail: users-help@netbeans.apache.org For 
> further information about the NetBeans mailing lists, visit: 
> https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists 


RE: NetBeans `Convert to for (Iterator...) {}' suggestion

Posted by John <jb...@gmail.com>.
Scott,



Thanks for the advice, and indeed, turning off Suggestions > Expand Enhanced
For Loop, removes the warning 😊. I don’t then get a ‘Can use functional
operations’ hint instead but that might be due to the context the for loop
exists in.



Do you happen to know where to find hint ‘Can use functional operations’ in
the Options?



I can’t locate something similar when looking through the lists. I prefer to
use functional operations when there isn’t exception handling needed (feels
messy then and so prefer the older syntax).



Michael,



  * suggestions which are only active at the current line where your cursor is, don't necessary produce better code, they are just there to transform parts of it in case you want to change something. Internally they are hints of a kind called actions.



Thanks for the clarification, I was wondering why some markers only appeared
when the line had focus. I suppose my issue with this is probably just the
icon which has an “!” implying that something is not right rather than use a
different icon (e.g. ‘?’ type icon) to hint that it is offering suggestions. I
couldn’t find anywhere where you could use your own icons for the hints
otherwise I would amend the ‘Warning on Current Line’ one to something a
little softer.



  * You should notice that if you change the loop to the iterator version, but not actually use the iterator except calling next() it will also add a hint called "convert to for-loop" which does the opposite. 



I did notice that and, for me, that made it more confusing, even
contradictory. But then I think that is more to do with my interpretation of
the icon as I read that as ‘use best practice’ and so it was like Netbeans
couldn’t make up its mind which syntax was best 🤔.



John





 **From:**[Scott Palmer](mailto:swpalmer@gmail.com)  
 **Sent:** 28 November 2022 17:51  
 **To:**[John](mailto:jbarrow104@gmail.com)  
 **Cc:**[NetBeans Mailing List](mailto:users@netbeans.apache.org)  
 **Subject:** Re: NetBeans `Convert to for (Iterator...) {}' suggestion



I agree that this isn’t usually a good suggestion.  It is available as a
refactoring for when you might need an iterator, but by no means should the
IDE “warn” about it.  I think that is the current behaviour though.  The line
is not marked with a hint lightbulb unless I put the cursor on that line. So
it’s not like it shows in the gutter as a warning like you would get for an
unused private function or similar.



It appears to be the hint “Expand Enhanced For Loop” that you will find under
“Suggestions” in the editor Hints panel.



When I turn that off the”expand” hint, the suggestion changes to  “Can use
functional operations” which coverts the loop to this.dictionart.forEach(word
-> {...});



Scott

  
  

> On Nov 28, 2022, at 9:00 AM, John <jb...@gmail.com> wrote:
>
>  
>
> Hi,
>
>  
>
> Quick one. I have just installed a vanilla Netbeans 15 and when I write a
> line such as
>
>  
>
> for (Word word : this.dictionary) {..}
>
>  
>
> Netbeans offers me a Warning / Recommendation
>
>  
>
> `Convert to for (Iterator...) {}'
>
>  
>
> That, if chosen, converts my syntax to
>
>  
>
> for (Iterator<Word> it = this.dictionary.iterator(); it.hasNext();) {...}
>
>  
>
> This is a lot less readable to me and I therefore wanted to turn off this
> hint.
>
>  
>
> However, The <alt><ret> doesn’t offer the option to disable this hint and I
> couldn’t find it in the Tools > Options > Editor > Hints (Java) list,
> although I found lots of other nice hints that I have turned on!  
>
>  
>
> I found this on Stack Overflow
>
>  
>
> [https://stackoverflow.com/questions/24263479/netbeans-convert-to-for-
> iterator-suggestion](https://stackoverflow.com/questions/24263479/netbeans-
> convert-to-for-iterator-suggestion)
>
>  
>
> But that was to do with which one is more efficient and I suspect that the
> warning is due to the fact that the more verbose version is quicker if the
> number of elements in the iterator is very large.
>
>  
>
> Am I stuck with this, or is it that I just overlooked where the hint is in
> the long list?
>
>  
>
>  

John



\--------------------------------------------------------------------- To
unsubscribe, e-mail: users-unsubscribe@netbeans.apache.org For additional
commands, e-mail: users-help@netbeans.apache.org For further information about
the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists


Re: NetBeans `Convert to for (Iterator...) {}' suggestion

Posted by Scott Palmer <sw...@gmail.com>.
I agree that this isn’t usually a good suggestion.  It is available as a refactoring for when you might need an iterator, but by no means should the IDE “warn” about it.  I think that is the current behaviour though.  The line is not marked with a hint lightbulb unless I put the cursor on that line. So it’s not like it shows in the gutter as a warning like you would get for an unused private function or similar.

It appears to be the hint “Expand Enhanced For Loop” that you will find under “Suggestions” in the editor Hints panel.

When I turn that off the”expand” hint, the suggestion changes to  “Can use functional operations” which coverts the loop to this.dictionart.forEach(word -> {...});

Scott

> On Nov 28, 2022, at 9:00 AM, John <jb...@gmail.com> wrote:
> 
> Hi,
>  
> Quick one. I have just installed a vanilla Netbeans 15 and when I write a line such as
>  
> for (Word word : this.dictionary) {...}
>  
> Netbeans offers me a Warning / Recommendation 
>  
> `Convert to for (Iterator...) {}'
>  
> That, if chosen, converts my syntax to
>  
> for (Iterator<Word> it = this.dictionary.iterator(); it.hasNext();) {...}
>  
> This is a lot less readable to me and I therefore wanted to turn off this hint. 
>  
> However, The <alt><ret> doesn’t offer the option to disable this hint and I couldn’t find it in the Tools > Options > Editor > Hints (Java) list, although I found lots of other nice hints that I have turned on!  
>  
> I found this on Stack Overflow
>  
> https://stackoverflow.com/questions/24263479/netbeans-convert-to-for-iterator-suggestion
>  
> But that was to do with which one is more efficient and I suspect that the warning is due to the fact that the more verbose version is quicker if the number of elements in the iterator is very large.
>  
> Am I stuck with this, or is it that I just overlooked where the hint is in the long list?
>  
>  
> John