Refactoring #002 Inline Method
Example
private ServerSocket createServerSocket(final int port) throws IOException {
ServerSocket result = new ServerSocket(port);
return result;
}
public void startup() {
try {
ServerSocket serverSocket = createServerSocket(PORT);
printServerPrompt("The server is listening on " + PORT + " port ...");
startWorking(serverSocket);
} catch (IOException e) {
e.printStackTrace();
}
}
/
public void startup() {
try {
ServerSocket serverSocket = new ServerSocket(PORT);
printServerPrompt("The server is listening on " + PORT + " port ...");
startWorking(serverSocket);
} catch (IOException e) {
e.printStackTrace();
}
}
Note
为什么/何时要 Inline Method?同样是考虑代码的“代码复用”和“可读性”:
- 函数体本身已经有足够的表达能力、并且只被调用一次,就没必要绕多余的弯子(“间接性可能带来帮助,但非必要的间接性总让人不舒服。”)
- 如果你手上有一群组织不合理的函数,那么先把它们集中起来再重新进行
Extract Method应该是个很不错的主意。
Inline Method 的手段:和 Extract Method 恰好相反
Refactoring #002 Inline Method的更多相关文章
- 『重构--改善既有代码的设计』读书笔记----Inline Method
加入间接层确实是可以带来便利,但过多的间接层有时候会让我自己都觉得有点恐怖,有些时候,语句本身已经够清晰的同时就没必要再嵌一个函数来调用了,这样只会适得其反.比如 void test() { if ( ...
- 重构改善既有代码设计--重构手法02:Inline Method (内联函数)& 03: Inline Temp(内联临时变量)
Inline Method (内联函数) 一个函数调用的本体与名称同样清楚易懂.在函数调用点插入函数体,然后移除该函数. int GetRating() { return MoreThanfiveLa ...
- Refactoring #001 Extract Method
Example public void startup() { ServerSocket serverSocket = null; try { serverSocket = new ServerSoc ...
- BookNote: Refactoring - Improving the Design of Existing Code
BookNote: Refactoring - Improving the Design of Existing Code From "Refactoring - Improving the ...
- Refactoring in Coding
Make changes on existing code for subsequent and constant changes of requirement. Reference:http://w ...
- 『重构--改善既有代码的设计』读书笔记----Inline Temp
与Inline Method相同,有时候犹豫需要Extract Method,需要对一些临时变量进行内联,而这个往往是Replace Temp with Query的一部分.简单来说,当你看到这种 d ...
- 重构手法之Extrct Method(提炼函数)
返回总目录 本节包含3个手法: 1.Extract Method(提炼函数) 2.Inline Method(内联函数) 3.Inline Temp(内联临时变量) Extract Method(提炼 ...
- Hypervisor, computer system, and virtual processor scheduling method
A hypervisor calculates the total number of processor cycles (the number of processor cycles of one ...
- 重构 改善既有代码的设计 (Martin Fowler 著)
第1章 重构, 第一个案例 1.1 起点 1.2 重构的第一步 1.3 分解并重组 statement() 1.4 运用多态取代与价格相关的条件逻辑 1.5 结语 第2章 重构原则 2.1 何谓重构 ...
随机推荐
- Extjs6.2.0搭建项目框架
1.安装 首先你总要去官网下载ext-6.2.0-gpl.zip和安装Sencha CMD工具来管理ExtJs项目,ext-6.2.0-gpl.zip下载完成解压先放在一边,一会用到. Sencha ...
- 关于Windows下的批处理如何模拟Sleep
好好的批处理,居然没有正式的Sleep可供调用.有时候,确实感到很无趣. 1. 方法1: ping 1.1.1.1来模拟 好不容易从stackoverflow上找到一个答案(称之为答案,是因为它被人标 ...
- JS-制作留言提交系统(支持ctrl+回车)
弹出键值说明: //console.log(ev.keyCode)//回车:13//ctrl:17 <!DOCTYPE html> <html> <head> &l ...
- webpack中,require的五种用法
a.js: module.exports = function(x){ console.log(x); } 一,commonjs同步: var b = require('./a');b('你好')// ...
- 【Android】Android--Dialog
前言 对话框对于应用也是必不可少的一个组件,在Android中也不例外,对话框对于一些提示重要信息,或者一些需要用户额外交互的一些内容很有帮助.本篇博客就讲解一下Android下对话框的使用,在本篇博 ...
- java如何计算两个日期之间相差多少天?
java如何计算两个日期之间相差多少天? public static void main(String [] args) { Date now = new Date(); Calendar cal = ...
- 树链剖分+离散+扫描(HDU5044)
Tree Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Subm ...
- 基础概念 之 Spark on Yarn
先抛出问题:Spark on Yarn有cluster和client两种模式,它们有什么区别? 用Jupyter写Spark时,只能使用client模式,为什么? 写一篇文章,搞清楚 Spark on ...
- sqlserver字符串多行合并为一行
--创建测试表 CREATE TABLE [dbo].[TestRows2Columns]( [Id] [,) NOT NULL, [UserName] [nvarchar]() NULL, [Sub ...
- codeforces#505--C Plasticine Zebra
C. Plasticine zebra time limit per test 1 second memory limit per test 256 megabytes input standard ...