Eclipse: how to create Quick-Assist for if-else to switch

In Eclipse Juno, we have a Quick-Assist to convert Switch to If-else . Is there a way to add a Quick-Fix or similar shortcut for the opposite action: If-else to Switch conversion?

For example, convert:

 if (kind == 1) { return -1; } else if (kind == 2) { return -2; } else { return 0; } 

To:

 switch (kind) { case 1: return -1; case 2: return -2; default: return 0; } 
+4
source share
1 answer

Interest Ask. If you can add a quick fix for converting if-else to switch , how would Assist if-else convert if-else with a boolean expression that contain more than one variable in the switch expression?

Say we want to convert this if-else to switch :

 if (x == 1 && y == 1) { //do something } else if (x == 1 && y == 3) { //do something else } else if (x == 2 && y == 1) { //and so on. } 

This will lead to:

 switch (x) { case 1 : { switch (y) { case 1: // do something case 3: // and so on } break; } case 2 : { switch (y) { case 1: //do something else } break; } } 

Personally, I would not want to convert if-else with more complex boolean switch to switch , because the generated code can be very difficult to read and (maybe) will not follow some good practice conventions. In simpler cases, like the one you published, it is not very difficult and does not require so much time and effort for you to convert it.

0
source

Source: https://habr.com/ru/post/1480080/


All Articles