当前位置:网站首页>重构--Inline
重构--Inline
2022-06-10 03:11:00 【李桥s2008100262】
目录
1、Inline(内联)概念
对于Java语言中,我们的class(类) 、method(方法)、local variable(本地局部变更),这些编程元素是有声明与内容的,对于我们的客户端代码通过这些符号引用这些元素的内容/本体完成功能的实现,这时候这种方式不是很合适,而需要让客户端代码直接引用编程元素的本体,重现编程人员最初的思路与业务流程,重新梳理、提练方法,进行重构。
2、Inline-Local Variable
内联本地局部变量:用本地局部变量的表达式获取逻辑来替换对本地局部变量的引用。
2.1 、动机
场景1:
有时候,在我们的方法中定义了大量的本地局部变量,这些本地局部变量的存在影响了Extract Method的重构,所以我们需要内联局部变量。
场景2:
有时候,在我们的方法中定义了大量的本地局部变量,这些本地局部变量,基本上是属于定义之后,只引用1次,基本没有定义的必要,这种情况下,我们也可以进行内联。
2.2 、实操

对于“tableNameArray”这个本地局部变量,其作用域在声明之后,仅仅使用了一次,这个局部变量就没有存在性的必要了,可以进行内联。


内联本地局部变量之后效果:
3、Inline Method
内联方法:用方法的本体来替换对方法的引用。
3.1 、动机
场景1:有时候,我们对一个方法的提炼Extract Method有得不是很好,并没有达到复用的目的,这个时候,我们就需要将方法进行Inline,等日后,有必要进行Extract Method,我们再进行提炼。
场景2:有时候,我们对一个方法的提炼Extract Method有得不是很好,提炼的粒度不够,这个时候,我们需要对方法进行Inline恢复原样,再重新进行提炼,以达到提炼的大粒度。如下所示:
原代码:
方法本体
{
//对代码块1进行提炼之后的方法调用
this.method1();
//代码块2
{
此处省略代码
}
}第1步: 我们发现对method1()的提炼粒度不够,不是很合适,希望将代码块1与代码块2一起提炼到一个方法中,这个时候,我们就需要使用Inline Method来使代码恢复原状再进行提炼,恢复之后的代码如下:
方法本体
{
//代码块1
{
此处省略代码
}
//代码块2
{
此处省略代码
}
}第2步:对代码块1与代码块2进行提炼方法
方法本体
{
//将代码块1与代码块2进行提供成新方法method();
this.method();
}3.2 、实操



Inline方法之后,原引用方法的地方,变成直接引用方法本体,如下图所示:
边栏推荐
- Why is the denominator of sample variance n-1 (unbiased estimation)?
- Tidb experience sharing 02
- C#/VB. Net to generate directory bookmarks when word is converted to PDF
- 19 determinant formula, algebraic cofactor
- 零拷贝原理详解
- 新号起步狂揽近200万粉,“老年”账号也能成为推广黑马
- Detailed explanation of zero copy principle
- jupyter notebook配置虚拟环境
- Matplotlib visual data analysis chart
- Determinant and its properties
猜你喜欢

Sword finger offer 09 Implementing queues with two stacks

基本数据类型与sizeof理解

剑指 Offer 09. 用两个栈实现队列

TiDB经验分享02

Promise introduction and Implementation

多线程并发

Vscade C language code ctrl+ left key cannot jump to definition

Anaconda 修改文件保存路径

Microgrid digital twin 𞓜 intelligent era, deploy the integrated control platform of source network load and storage

Huawei Hubble will add another IPO, and Maxon will rush to the scientific innovation board after more than ten years of dormancy
随机推荐
ModuleNotFoundError: No module named ‘pip‘
Xmake v2.6.6 發布,分布式編譯和緩存支持
修改谷歌浏览器Google Chrome缓存位置
Educational Codeforces Round 129 (Rated for Div. 2)(A-D)
Distributed data object: HyperTerminal 'global variable'
Drawing of common charts
Arduino and processing serial port communication (split function)
How to distinguish the attributes of the object itself from those on the prototype
Tensorflow.js入门之mobilenet
Knight Moves
21.动态规划整理
Degraded judge (European expansion + enumeration)
21. dynamic planning and arrangement
The playback capacity of a single video is more than 8000w, so hard core cooking is so superior
In the new year of hybrid, does BYD really want to reverse its rating?
m3u8文件中的 m3u8标签与属性说明
新号起步狂揽近200万粉,“老年”账号也能成为推广黑马
数据的存储(整型,浮点型超详细)
剑指 Offer 24. 反转链表
指针的进阶(C语言超详细指针介绍)