Java Eclipse:最有用的重构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1804839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:49:53  来源:igfitidea点击:

Eclipse: Most useful refactorings

javaeclipserefactoring

提问by Adrian

Every now and again, I make use of the Eclipse refactoring feature. Some techniques are more obvious then others and some I never tried.

我时不时地使用 Eclipse 重构特性。有些技术比其他技术更明显,有些我从未尝试过。

What refactoring is most useful for you and why?

什么重构对你最有用,为什么?

Note: I find this presentation very useful, perhaps because it is example driven therefore easy to understand:

注意:我发现此演示文稿非常有用,也许是因为它是示例驱动的,因此易于理解:

"Refactoring for everyone - How and why to use Eclipse's automated refactoring features"

为每个人进行重构 - 如何以及为何使用 Eclipse 的自动重构功能

Edit: This article is useful as well (Thanks jitter)

编辑:这篇文章也很有用(感谢抖动)

Explore refactoring functions in Eclipse JDT

探索 Eclipse JDT 中的重构功能

采纳答案by zvikico

It is an interesting question. I know what works for me and it is interesting to see what others use.

这是一个有趣的问题。我知道什么对我有用,看看其他人使用什么很有趣。

I decided to take a more scientific approach to determine the most commonly used refactoring commands. Eclipse has a Usage Data Collector (UDC)feature built in. The data is publicly available. I took the data and extracted the following graph which shows the most commonly used editing commands (without navigation commands).

我决定采用更科学的方法来确定最常用的重构命令。Eclipse 具有内置的使用数据收集器 (UDC)功能。数据是公开可用的。我获取了数据并提取了下图,其中显示了最常用的编辑命令(没有导航命令)。

alt text http://img.skitch.com/20091207-bmcng36rjy837sqmcx58b85age.gif

替代文字 http://img.skitch.com/20091207-bmcng36rjy837sqmcx58b85age.gif

However, I am a strong believer in "Save Actions" for formatting and organizing imports (read my article about it), so I wouldn't count those. I would also remove the commenting actions. The picture looks like this: alt text http://img.skitch.com/20091207-ieas1mk5114fwitucqkqxyw6t.gif

但是,我坚信用于格式化和组织导入的“保存操作”(阅读我的文章),所以我不会计算这些。我也会删除评论操作。图片看起来是这样的:alt text http://img.skitch.com/20091207-ieas1mk5114fwitucqkqxyw6t.gif

回答by Andreas Dolk

My favorites:

我最喜欢的:

  1. Renaming
  2. Pull up/Push Down
  3. Extract Method
  1. 重命名
  2. 上拉/下推
  3. 提取方法

回答by Yishai

What I use the most is Rename, Extract Method and Change Method Signature, in that order.

我最常用的是按重命名、提取方法和更改方法签名的顺序。

回答by abyx

Rename- because giving things meaningful names is the best way to write self-documenting code. Shift+Alt+R

重命名- 因为给事物赋予有意义的名称是编写自文档化代码的最佳方式。Shift+ Alt+R

Extract method- whenever a method gets too long. Shift+Alt+M

提取方法- 每当方法变得太长时。Shift+ Alt+M

Extract constant- because magic numbers are bad. Shift+Alt+T(refactoring menu, there's no direct shortcut).

提取常量- 因为幻数不好。Shift+ Alt+ T(重构菜单,没有直接的快捷方式)。

Inline/introduce variable- to remove clutter from methods. Shift+Alt+I(inline), Shift+Alt+L(introduce)

内联/引入变量- 从方法中消除混乱。Shift+ Alt+ I(内置),Shift+ Alt+ L(介绍)

回答by Nettogrof

I use :

我用 :

1- Rename - to correct better method name

1- 重命名 - 更正更好的方法名称

2- Move - to organize my package in better ways, like when I start my project, it was so small so no need of a io package, but now yes.

2- 移动 - 以更好的方式组织我的包,比​​如当我开始我的项目时,它是如此之小,所以不需要 io 包,但现在是的。

3- Generate Comments -when I create a .class avoid me to re-copy the GPL license, etc

3- 生成评论 - 当我创建一个 .class 时避免我重新复制 GPL 许可证等

4- Correct Indentation - to keep my code readable.

4- 正确缩进 - 保持我的代码可读。

回答by Hardcoded

My favourites (in order of using):

我的最爱(按使用顺序):

  1. Rename (Alt-Shift-R, or Ctrl-1for faster in-file renaming)
    Good renaming variables,methods,etc. without side effects.
  2. Extract Variable (Ctrl-1, Alt-Shift-L)
    Good for splitting a quick-made-100-character-line to separate steps.
  3. Extract Method (Alt-Shift-M)
    Create a method out of some code without any side effects.
  4. Split Variable Declaration (Ctrl-1)
    Good when you initialize a variable at the declaration and now find out, that the initialisation needs to be in a try- or if-block.
  5. Change Method Signature (Alt-Shift-C)
    The handy Swiss Army Knife of method signature manipulation, including default values for new parameters.
  6. Pull Up/Push Down Pull methods and variables to a generic interface or superclass or push it down to a subclass
  7. Extract Interface/Superclass
    Extract an interface or a superclass out of the current class. Very handy.
  1. 重命名(Alt-Shift-R,或Ctrl-1为了更快的文件内重命名)
    良好的重命名变量、方法等。没有副作用。
  2. 提取变量 ( Ctrl-1, Alt-Shift-L)
    适用于将快速制作的 100 个字符的行拆分为单独的步骤。
  3. Extract Method ( Alt-Shift-M)
    从一些代码中创建一个没有任何副作用的方法。
  4. 拆分变量声明 ( Ctrl-1)
    当您在声明处初始化一个变量并且现在发现初始化需要在 try- 或 if- 块中时很好。
  5. 更改方法签名 ( Alt-Shift-C)
    方便的瑞士军刀方法签名操作,包括新参数的默认值。
  6. 上拉/下推 将方法和变量拉到通用接口或超类或下推到子类
  7. 提取接口/超
    类 从当前类中提取接口或超类。非常便利。

回答by jeff porter

CTRL + 1 on a red lined section, i.e. quick fix.

CTRL + 1 在红线部分,即快速修复。

回答by Paul Lammertsma

The most popular refactorings have been stated, and I entirely agree with them.

已经说明了最流行的重构,我完全同意它们。

Code formatter(Source, Formator CtrlShiftF) is one of the features of the IDE I use very often. True, it is not refactoring, but it improves code readability while maintaining your coding style: simply head to Preferences, Java, Code Style, Formatterand tell Eclipse how you want your code to look!

代码格式化程序SourceFormatCtrlShiftF)是我经常使用的 IDE 的功能之一。没错,它不是重构,而是在保持编码风格的同时提高了代码可读性:只需前往PreferencesJavaCode StyleFormatter并告诉 Eclipse 您希望代码的外观!

Generate Getters and Settersis also a feature I find to save time when writing Java beans.

生成 Getter 和 Setter也是我发现在编写 Java bean 时可以节省时间的一个特性。

回答by Spina

My favorites are:

我最喜欢的是:

1) Rename - It works on method names, variable names, class names, fields-- really anything with a name.
2) Convert Anonymous Class to Nested - Helps with debugging, lets you reuse logic (such as a comparator) that you only thought that you'd use in one place.
3) Convert Member Type to Top Level - Frequently after making an anonymous class into a nested class I discover that the class is useful elsewhere. This refactoring is perfect then.

1) 重命名 - 它适用于方法名称、变量名称、类名称、字段——实际上任何有名称的东西。
2) 将匿名类转换为嵌套类 - 有助于调试,让您可以重用您认为只在一个地方使用的逻辑(例如比较器)。
3) 将成员类型转换为顶级——经常在将匿名类变成嵌套类后,我发现该类在其他地方很有用。这种重构是完美的。

回答by Peter Lawrey

Eclipse has perhaps the least refactorings for all the popular IDEs. You might consider Netbeans or IntelliJ (Community edition is free). Conversely Eclipse has possibly the best debugger. ;)

Eclipse 可能对所有流行的 IDE 进行了最少的重构。您可以考虑 Netbeans 或 IntelliJ(社区版免费)。相反,Eclipse 可能是最好的调试器。;)

I use refactoring as I write the code (which I have found speeds up writing by about 15%) so IntelliJ's ability to refactor code which does not compile very useful for me. The other IDEs may support this now (does any one know?) I find IntelliJ's smart complete is a fair bit smarter as well.

我在编写代码时使用重构(我发现它使编写速度提高了大约 15%),因此 IntelliJ 重构不能编译的代码的能力对我来说非常有用。其他 IDE 现在可能支持这一点(有人知道吗?)我发现 IntelliJ 的智能完成也更智能一些。

I have tested retyping a files from printout (originally written in eclipse) and found I used 30% less keys and 50% less mouse movements when typing the file with IntelliJ (compared with Eclipse) I would estimate Netbeans to be somewhere in the middle.

我已经测试了从打印输出(最初是用 eclipse 编写的)重新输入文件,发现在使用 IntelliJ 输入文件时,我使用的键减少了 30%,鼠标移动减少了 50%(与 Eclipse 相比)我估计 Netbeans 处于中间位置。

回答by VonC

I like the Extract Method (Alt+Shift+M), and since 3.6M1, it now handles selections that contain continue statements.

我喜欢的提取方法(Alt+ Shift+ M),并自3.6M1,它现在处理包含continue语句的选择。

To preserve the semantics of the existing code, the selection needs to include the last statement of the loop. In the extracted method, continue statements are changed to return:

为了保留现有代码的语义,选择需要包括循环的最后一条语句。在提取的方法中,continue 语句改为 return:

Extract method refactoring with continue http://download.eclipse.org/eclipse/downloads/drops/S-3.6M1-200908061400/images/extract-method-continue.png

提取方法重构继续 http://download.eclipse.org/eclipse/downloads/drops/S-3.6M1-200908061400/images/extract-method-continue.png

For a selection that would need multiple return values in the extracted method, Eclipse now lists the conflicting variables in the error message:

对于在提取的方法中需要多个返回值的选择,Eclipse 现在会在错误消息中列出冲突变量:

Extract method refactoring with an ambiguous return value error http://download.eclipse.org/eclipse/downloads/drops/S-3.6M1-200908061400/images/extract-method-multiple-return-values.png

提取方法重构,返回值不明确错误 http://download.eclipse.org/eclipse/downloads/drops/S-3.6M1-200908061400/images/extract-method-multiple-return-values.png