AutoRefactor
AutoRefactor copied to clipboard
New refactoring - Simplify loops which only retain last iteration result
Given
The following condition:
String result1 = null;
for (String s : sortedSet) {
result1 = s.toString();
}
String result2 = null;
for (String s : list) {
result2 = s.toString();
}
When
... automatic refactorings are applied ...
Then
code is refactored to:
String result1 = !sortedSet.isEmpty() ? sortedSet.last().toString() : null;
String result2 = !list.isEmpty() ? list.get(list.size() - 1).toString() : null;
A similar change can be applied to Maps.
(Edited) (Watch out. This refactoring must not be applied if the set or the list is empty. In this case, result stays as null.) Previous edited comment referred to old description, current phrasing is good:
String result1 = !sortedSet.isEmpty() ? sortedSet.last().toString() : null;
String result2 = !list.isEmpty() ? list.get(list.size() - 1).toString() : null;
On 16 July 2015 at 00:45, Jean-Noël Rouvignac [email protected] wrote:
Given
The following condition:
String result1 = null;for (String s : sortedSet) { result1 = s.toString(); } String result2 = null;for (String s : list) { result2 = s.toString(); }
When
... automatic refactorings are applied ... Then
code is refactored to:
String result1 = sortedSet.last().toString();String result2 = list.get(list.size() - 1).toString();
— Reply to this email directly or view it on GitHub https://github.com/JnRouvignac/AutoRefactor/issues/140.
Yes, I saw I made a mistake and corrected it quickly. Thanks for watching ;)