Reputation: 8158
I'm using Java with Eclipse.
In my application, one code snippet is there which is the long series of multiple if statements
.
Recently I had to work upon that code & I found that these multiple if statements
can be converted to if-else-if
ladder.
Now converting this to if-else-if
ladder manually is somewhat cumbersome.
Is there some short cut in Eclipse that I can use to do so?
Upvotes: 0
Views: 1579
Reputation: 8158
After a long search I finally found the short cut in Eclipse.
I selected all the if statements
, pressed Ctrl+1 (Right Click & selected Quick Fix)
.
It gave me an option "Join if sequence in if-else-if
", which does exactly what I want.
Upvotes: 4
Reputation: 9599
Use Find/Replace Dialog.
Replace all }\s*if
with }else if
.
Enable "Regular Expressions".
You can select the lines you want and enable "Selected lines".
Upvotes: 4
Reputation: 4506
Since it might not be the same logic, I don't think that Eclipse has that feature. I do know that IntelliJ have Inspections covering those kind of issues, so maybe a plugin doing code inspections can help you refactor.
Secondly, if it can be converted to if else if
, then it might also be convertable to switch case
depending on what variable you are looking at, and if it's the same. Also available on strings since java 7. It might also be refactorable to do switch on enums
.
Upvotes: 1
Reputation: 9023
Find & Replace all "if" with "else if", then fix the compiler error on the first one by deleting the "else". Make sure you're not accidentally changing anything else that you shouldn't!
Upvotes: 1