如何优化代码中大量的if/else

野性酷女 2023-10-04 11:58 111阅读 0赞

目录

一、如何优化代码中大量的if/else

1、判断条件取反,提前return

2、策略模式

3、使用Optional

4、表驱动法

5、反射

6、方法引用

7、其他


一、如何优化代码中大量的if/else

不是所有的if/else和switch/case都需要优化,当我们发现有“痛点”或者“闻到代码有坏味道”再来优化才是最好的,不然你可能会写了一个从不扩展的可扩展代码,所有的优化都是为了更好的迭代项目,更好的服务于业务,而不是为了优化而优化——深夜里的程序员

1、判断条件取反,提前return

  1. package com.zibo.ifelse;
  2. // 判断条件取反,提前return
  3. public class Method01 {
  4. public static void main(String[] args) {
  5. boolean good = true;
  6. // 原始写法
  7. System.out.println(doSth(good));
  8. // 优化写法:判断条件取反,提前return
  9. System.out.println(doSthNew(good));
  10. }
  11. // 原始写法
  12. private static String doSth(boolean good) {
  13. if (good) {
  14. // do something
  15. return "你是个好人!热烈欢迎!";
  16. } else {
  17. return null;
  18. }
  19. }
  20. // 优化写法:判断条件取反,提前return
  21. private static String doSthNew(boolean good) {
  22. if(!good){
  23. return null;
  24. }
  25. // do something
  26. return "你是个好人!热烈欢迎!";
  27. }
  28. }

2、策略模式

  1. package com.zibo.ifelse;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. // 策略模式
  5. public class Method02 {
  6. public static void main(String[] args) {
  7. String strategy = "大哥";
  8. // 原始写法
  9. doSth(strategy);
  10. // 多态写法
  11. doSthNew1(strategy);
  12. // 枚举写法
  13. doSthNew2(strategy);
  14. }
  15. // 原始写法
  16. private static void doSth(String strategy) {
  17. if(strategy.equals("大哥")){
  18. System.out.println("大哥威猛!……");
  19. }else if(strategy.equals("二哥")){
  20. System.out.println("二哥豪爽!……");
  21. }else if(strategy.equals("三哥")){
  22. System.out.println("二哥帅气!……");
  23. }
  24. }
  25. // 多态写法
  26. private static void doSthNew1(String strategy){
  27. Map<String,Strategy> map = new HashMap<>();
  28. map.put("大哥",new A());
  29. map.put("二哥",new B());
  30. map.put("三哥",new C());
  31. Strategy s = map.get(strategy);
  32. s.run();
  33. }
  34. // 枚举写法
  35. private static void doSthNew2(String strategy){
  36. EStrategy.valueOf(strategy).run();
  37. }
  38. }
  39. // 多态写法
  40. interface Strategy{
  41. void run();
  42. }
  43. class A implements Strategy{
  44. @Override
  45. public void run() {
  46. System.out.println("大哥威猛!……");
  47. }
  48. }
  49. class B implements Strategy{
  50. @Override
  51. public void run() {
  52. System.out.println("二哥豪爽!……");
  53. }
  54. }
  55. class C implements Strategy{
  56. @Override
  57. public void run() {
  58. System.out.println("二哥帅气!……");
  59. }
  60. }
  61. // 枚举写法
  62. enum EStrategy{
  63. 大哥 {
  64. @Override
  65. void run() {
  66. System.out.println("大哥威猛!……");
  67. }
  68. }, 二哥 {
  69. @Override
  70. void run() {
  71. System.out.println("二哥豪爽!……");
  72. }
  73. }, 三哥 {
  74. @Override
  75. void run() {
  76. System.out.println("二哥帅气!……");
  77. }
  78. };
  79. abstract void run();
  80. }

3、使用Optional

  1. package com.zibo.ifelse;
  2. import java.util.Optional;
  3. import java.util.function.Function;
  4. // 使用 Optional
  5. public class Method03 {
  6. public static void main(String[] args) {
  7. String str = null;
  8. // 原始写法
  9. doSth(str);
  10. // 使用 Optional
  11. doSthNew(str);
  12. }
  13. // 原始写法
  14. private static void doSth(String str) {
  15. if(str == null){
  16. System.out.println("你在耍我!");
  17. }else {
  18. System.out.println("好的,大哥!");
  19. }
  20. }
  21. // 使用 Optional
  22. private static void doSthNew(String str) {
  23. Optional<String> userOptional = Optional.ofNullable(str);
  24. Function<String, String> function = string -> "好的,大哥!";
  25. System.out.println(userOptional.map(function).orElse("你在耍我!"));
  26. }
  27. }

4、表驱动法

表驱动法:使用查询代替逻辑语句

  1. package com.zibo.ifelse;
  2. // 表驱动法:数组小技巧
  3. public class Method04 {
  4. public static void main(String[] args) {
  5. // 原始写法
  6. System.out.println(doSth(1));
  7. // 数组优化
  8. System.out.println(doSthNew(1));
  9. }
  10. // 原始写法
  11. private static int doSth(int month) {
  12. if (month == 1) return 31;
  13. if (month == 2) return 29;
  14. if (month == 3) return 31;
  15. if (month == 4) return 30;
  16. if (month == 5) return 31;
  17. if (month == 6) return 30;
  18. if (month == 7) return 31;
  19. if (month == 8) return 31;
  20. if (month == 9) return 30;
  21. if (month == 10) return 31;
  22. if (month == 11) return 30;
  23. if (month == 12) return 31;
  24. return 0;
  25. }
  26. // 数组优化
  27. private static int doSthNew(int month) {
  28. int[] monthDays = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  29. return monthDays[--month];
  30. }
  31. }

5、反射

  1. package com.zibo.ifelse;
  2. import java.lang.reflect.Method;
  3. // 反射
  4. public class Method05 {
  5. public static void main(String[] args) throws Exception {
  6. // 原始写法
  7. doSth("doOne");
  8. // 反射写法
  9. doSthNew("doOne");
  10. }
  11. // 原始写法
  12. private static void doSth(String ds) {
  13. if("doOne".equals(ds)){
  14. new DoSth().doOne();
  15. }else if("doTwo".equals(ds)){
  16. new DoSth().doTwo();
  17. }else if("doThree".equals(ds)){
  18. new DoSth().doThree();
  19. }else {
  20. System.out.println("啥也不干!");
  21. }
  22. }
  23. // 反射写法
  24. private static void doSthNew(String ds) throws Exception {
  25. Method method = DoSth.class.getDeclaredMethod(ds);
  26. DoSth doSth = DoSth.class.newInstance();
  27. method.invoke(doSth);
  28. }
  29. }
  30. class DoSth{
  31. public void doOne(){
  32. System.out.println("One");
  33. }
  34. public void doTwo(){
  35. System.out.println("Two");
  36. }
  37. public void doThree(){
  38. System.out.println("Three");
  39. }
  40. }

6、方法引用

  1. package com.zibo.ifelse;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import java.util.function.Consumer;
  5. // 方法引用
  6. public class Method05 {
  7. public static void main(String[] args) {
  8. // 原始写法
  9. doSth("doOne");
  10. // 方法引用
  11. doSthNew("doOne");
  12. }
  13. // 原始写法
  14. private static void doSth(String ds) {
  15. if("doOne".equals(ds)){
  16. new DoSth().doOne();
  17. }else if("doTwo".equals(ds)){
  18. new DoSth().doTwo();
  19. }else if("doThree".equals(ds)){
  20. new DoSth().doThree();
  21. }else {
  22. System.out.println("啥也不干!");
  23. }
  24. }
  25. // 方法引用
  26. private static void doSthNew(String ds){
  27. Map<String, Consumer<DoSth>> functionMap = new HashMap<>();
  28. functionMap.put("doOne", DoSth::doOne);
  29. functionMap.put("doTwo", DoSth::doTwo);
  30. functionMap.put("doThree", DoSth::doThree);
  31. functionMap.get(ds).accept(new DoSth());
  32. }
  33. }
  34. class DoSth{
  35. public void doOne(){
  36. System.out.println("One");
  37. }
  38. public void doTwo(){
  39. System.out.println("Two");
  40. }
  41. public void doThree(){
  42. System.out.println("Three");
  43. }
  44. }

7、其他

遇到大量if记住下面的口诀:

互斥条件表驱动,

嵌套条件校验链,

短路条件早return,

零散条件可组合。——知乎某大佬

发表评论

表情:
评论列表 (有 0 条评论,111人围观)

还没有评论,来说两句吧...

相关阅读