Eclipse:如何为If-else创建快速辅助切换[复制]

时间:2022-09-02 14:48:24

This question already has an answer here:

这个问题在这里已有答案:

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

在Eclipse Juno中,我们有一个Quick-Assist用于将Switch转换为If-else。有没有办法为相反的操作添加快速修复或类似的快捷方式:将If-else转换为Switch?

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;
}

1 个解决方案

#1


0  

Interesting question. If it's possible to add a quick-fix for converting if-else to switch, then how would the Assist convert if-else statements with boolean expression that contain more than one variable to switch statement ?

有趣的问题。如果可以添加一个快速修复来转换if-else来切换,那么Assist如何将包含多个变量的boolean表达式的if-else语句转换为switch语句?

Let's say, we want to convert this if-else statement to switch:

假设我们要将此if-else语句转换为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 would result in:

这将导致:

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 wouldn't like to convert if-else with more complex boolean statements to switch, because the generated code could be very difficult to read and (maybe) wouldn't meet some Good Practices conventions. In simplier cases, like the one you've posted, it's not very difficult and wouldn't take so much time and effort for you to convert it.

就个人而言,我不想将if-else转换为更复杂的布尔语句来切换,因为生成的代码可能非常难以阅读,并且(可能)不符合某些良好实践惯例。在简单的情况下,就像你发布的那样,它并不是很困难,也不会花费太多的时间和精力来转换它。

#1


0  

Interesting question. If it's possible to add a quick-fix for converting if-else to switch, then how would the Assist convert if-else statements with boolean expression that contain more than one variable to switch statement ?

有趣的问题。如果可以添加一个快速修复来转换if-else来切换,那么Assist如何将包含多个变量的boolean表达式的if-else语句转换为switch语句?

Let's say, we want to convert this if-else statement to switch:

假设我们要将此if-else语句转换为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 would result in:

这将导致:

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 wouldn't like to convert if-else with more complex boolean statements to switch, because the generated code could be very difficult to read and (maybe) wouldn't meet some Good Practices conventions. In simplier cases, like the one you've posted, it's not very difficult and wouldn't take so much time and effort for you to convert it.

就个人而言,我不想将if-else转换为更复杂的布尔语句来切换,因为生成的代码可能非常难以阅读,并且(可能)不符合某些良好实践惯例。在简单的情况下,就像你发布的那样,它并不是很困难,也不会花费太多的时间和精力来转换它。