mbien opened a new pull request #3689:
URL: https://github.com/apache/netbeans/pull/3689


   some project wide, map/collection related optimizations (low hanging fruits).
   
    Now that `referencedIn` is fixed in NB 13, we might as well use the 
opportunity to improve the codebase a bit with the help of jackpot :)
   
   I picked some rules from 
[this](https://github.com/mbien/jackpot-inspections/) hint collection.
   
   first commit:
   If the right iterator view is used (keySet/entrySet/values), the loop body 
does not have to call map.get(key) on every iteration.
   ```java
   // avoid inefficient map traversal
   for ($K $k : $map.keySet()) {
       $preamble$;
       $V $v = $map.get($k);
       $body$;
   } :: $map instanceof java.util.Map && !referencedIn($k, $preamble$)
   =>
   for ($V $v : $map.values()) {
       $preamble$;
       $body$;
   }
   :: !referencedIn($k, $body$)
   =>
   for (java.util.Map.Entry<$K, $V> entry : $map.entrySet()) {
       $preamble$;
       $K $k = entry.getKey();
       $V $v = entry.getValue();
       $body$;
   }
   :: otherwise
   ;;
   ```
   
   second commit - no need to use contains() before remove()
   ```java
   // contains + remove in succession
   if ($col.contains($v)) {
       $col.remove($v);
   } :: $col instanceof java.util.Collection
   =>
   $col.remove($v);
   ;;
   
   if ($map.containsKey($k)) {
       $map.remove($k);
   } :: $map instanceof java.util.Map
   =>
   $map.remove($k);
   ;;
   ```
   
   third commit (not project wide, only two selected places to make review 
easier)
   This one is risky, so I only applied it selectively.
   ```java
   // contains + get in succession (dangerous! values can be null)
   if ($map.containsKey($key)) {
       $preamble$;
       $V $v = $map.get($key);
       $body$;
   } :: $map instanceof java.util.Map && !referencedIn($key, $preamble$)
   =>
   $V $v = $map.get($key);
   if ($v != null) {
       $preamble$;
       $body$;
   }
   ;;
   ```
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to