• IDEA代码重构技巧--抽取+内联


    IDEA代码重构技巧--目录页

    1. 抽取

    在做代码重构时,可能发现我们需要将变量,参数做抽取,或者某个方法过长,需要将这个方法中相同逻辑的代码块抽取出一个独立的函数,这时候就需要使用抽取,抽取有三类:

    1. 抽变量,IDEA快捷键 CTRL+ALT+V
    2. 抽参数,IDEA快捷键 CTRL+ALT+P
    3. 抽函数,IDEA快捷键 CTRL+ALT+M

    示例代码:

    1. package com.coline.extract;
    2. /**
    3. * @author: Coline
    4. * @ClassName: ReconsitutionParam
    5. * @Date: 2022/8/20 10:34
    6. * @Description: 抽取验证
    7. * 抽变量 CTRL+ALT+V
    8. * 抽参数 CTRL+ALT+P
    9. * 抽函数 CTRL+ALT+M
    10. */
    11. public class ReconsitutionParam {
    12. private int testInt;
    13. private String testStr;
    14. /**
    15. * @Description: 抽取变量,选中this.testInt,键入CTRL+ALT+V
    16. */
    17. public void extractVariable() {
    18. System.out.println("testInt: " + this.testInt + "testStr: " + this.testStr);
    19. }
    20. /**
    21. * @Description: 抽取参数,选中函数第一行的变量testStr,键入CTRL+ALT+P
    22. */
    23. public void extractParam() {
    24. String testStr = this.testStr;
    25. System.out.println("testStr: " + testStr);
    26. }
    27. /**
    28. * @Description: 抽取函数,选中函数第一行的String testStr = this.testStr; ,键入CTRL+ALT+M
    29. */
    30. public void extractMethod() {
    31. int testInt = this.testInt;
    32. System.out.println("testInt: " + testInt + "testStr: " + testStr);
    33. }
    34. }

    1.1. 抽变量

        选中可以被抽取的变量,例如示例函数extractVariable中的this.testInt,键入CTRL+ALT+V

    1.2. 抽参数

        选中需要被抽取的参数,例如示例函数extractParam中的变量testStr,键入CTRL+ALT+P,就会将变量抽取到方法参数中

    1.3. 抽函数

      选中需要被抽取的代码块,键入CTRL+ALT+M,就会将变量抽取到方法参数中

    2. 内联

        在做代码重构时有时会发现有的变量或者函数没必要被独立的抽取出来,增加了代码的阅读成本,同时也不利于后续的优化,此时我们需要使用内联简化代码
        例如:变量只使用了一次,但是被独立的抽取出来了,这时可以使用内联将变量替换到调用的位置。函数内容很短,逻辑很简单,只被调用了一次,而且预期未来不会有别的地方调用,这时可以使用内联将函数内容替换到调用位置。

    1. /**
    2. * @author: Coline
    3. * @ClassName: ReconsitutionInline
    4. * @Date: 2022/8/20 12:07
    5. * @Description: 重构-内联
    6. * CTRL+ALT+N
    7. */
    8. public class ReconsitutionInline {
    9. private int testInt;
    10. private String testStr;
    11. /**
    12. * 验证内联变量
    13. * 选中变量,键入CTRL+ALT+N
    14. */
    15. public void inlineVariable() {
    16. int testInt = this.testInt;
    17. System.out.println("testInt: " + testInt + "testStr: " + this.testStr);
    18. }
    19. /**
    20. * 验证内联函数
    21. * 选中待内联函数,键入CTRL+ALT+N
    22. * @return
    23. */
    24. public String inlineMeth() {
    25. return inlineMethTmp();
    26. }
    27. public String inlineMethTmp() {
    28. return "I am inline Meth";
    29. }
    30. }

    2.1. 内联变量

        选中需要被内联的变量,键入CTRL+ALT+N

     2.2. 内联函数

        选中需要被内联的函数,键入CTRL+ALT+N

  • 相关阅读:
    Android Studio Gradle插件版本与Gradle 版本对应关系
    ArcGIS || ENVI:如何将彩色影像拆分为R、G、B以及H、S、I(B/V)影像?
    GBase 8c V3.0.0数据类型——HLL函数和操作符(内置函数)
    Ubuntu本地快速搭建web小游戏网站,公网用户远程访问
    关于Flask高级_RequestParser中的add_argument方法参数详解
    WebDAV之葫芦儿·派盘+PassStore
    实现图的遍历
    基于springboot+vue的毕业生实习与就业管理系统
    人工智能基础 作业6
    uniapp 高度铺满全屏
  • 原文地址:https://blog.csdn.net/u011294519/article/details/126437604