IntelliJ IDEA 2020.3使用教程:重构代码的3种方法

简化代码具有很多优势,包括提高可读性,解决技术难题以及管理不断变化的需求。我们将在此博客中介绍三种重构类型:提取和内联,更改签名,重命名。

这篇博客文章涵盖了与视频相同的内容,并包含一些其他提示和技巧。点击查看视频>>

简化代码具有很多优势,包括提高可读性,解决技术难题以及管理不断变化的需求。我们将在此博客中介绍三种重构类型:

  • 提取和内联
  • 更改签名
  • 重命名

提取和内联

简化代码的第一种方法是提取它。您可以在IntelliJ IDEA中执行五种类型的提取重构:

  • 提取方法
  • 提取常数
  • 提取字段
  • 提取变量
  • 提取参数

提取方法

此方法中的switch语句与该方法的其余部分不一致。

public class PlanetExtractions {    Planet myPlanet = new Planet("earth");    // I'm using PlanetExtractions to get the facts for my country    // I'm using planetextractions to get the facts for my country    private void printPlanetFacts(final String country) {        System.out.println("Planet name is " + myPlanet.getName());        System.out.println("Current season is " + myPlanet.getCountryWeather());        System.out.println("Number of times the planet rotates around the sun is " + 365);        System.out.println("Number of characters in planet name = " + myPlanet.getName().length());        switch (myPlanet.getCountryWeather()) {            case "Spring" -> System.out.println("The weather is warm in the UK");            case "Summer" -> System.out.println("The weather is hot in the UK");            case "Autumn" -> System.out.println("The weather is cool in the UK");            default -> System.out.println("The weather is cold in the UK");        }    }}

在2020.3中,此过程已简化。您需要选择要提取的完整代码块,然后可以在macOS上使用M,在Windows和Linux上使用Ctrl + Alt + M来提取方法。

提取方法选择

我们可以给新方法起一个名字,例如,getWeather()并且IntelliJ IDEA将用对我们新方法的调用替换原始逻辑:

public class PlanetExtractions {    public static final int NUMBER_OF_DAYS_IN_A_YEAR = 365;    Planet myPlanet = new Planet("earth");    private String theWeatherIs = "The weather is";    // I'm using PlanetExtractions to get the facts for my country    // I'm using planetextractions to get the facts for my country    private void printPlanetFacts(final String country) {        System.out.println("Planet name is " + myPlanet.getName());        System.out.println("Current season is " + myPlanet.getCountryWeather());        System.out.println("Number of times the planet rotates around the sun is " + NUMBER_OF_DAYS_IN_A_YEAR);        System.out.println("Number of characters in planet name = " + myPlanet.getName().length());        getWeather();    }    private void getWeather() {        switch (myPlanet.getCountryWeather()) {            case "Spring" -> System.out.println("The weather is warm in the UK");            case "Summer" -> System.out.println("The weather is hot in the UK");            case "Autumn" -> System.out.println("The weather is cool in the UK");            default -> System.out.println("The weather is cold in the UK");        }    }}

通过再次使用相同的快捷方式,您可以获取“提取方法”的其他选项。

提取方法选项

提示:您可以通过在macOS上使用或在Windows和Linux上使用Ctrl + B导航到方法的声明或用法。

查看视频中的步骤

提取常数

我们可以在此代码行中将数字365提取为常数,因为地球始终需要大约365天才能完成绕太阳的自转:

System.out.println("Number of times the planet rotates around the sun is " + 365);

我们可以选择数字,然后在macOS上使用C,在Windows和Linux上使用Ctrl + Alt + C将其提取为常数。我们可以给它起一个名字,例如 NUMBER_OF_DAYS_IN_A_YEAR。IntelliJ IDEA在课程开始时创建一个新的公共静态最终常量:

public static final int NUMBER_OF_DAYS_IN_A_YEAR = 365;

IntelliJ IDEA还用新的常量替换了原始代码行:

System.out.println(“Number of times the planet rotates around the sun is ” + NUMBER_OF_DAYS_IN_A_YEAR);

查看视频中的步骤

提取字段

在我们提取的方法中,短语“天气是”重复了四次,因此让我们将其提取到字段中:

private void getWeather() {        switch (myPlanet.getCountryWeather()) {            case "Spring" -> System.out.println("The weather is warm in the UK");            case "Summer" -> System.out.println("The weather is hot in the UK");            case "Autumn" -> System.out.println("The weather is cool in the UK");            default -> System.out.println("The weather is cold in the UK");        }    }

您需要选择The weather is,然后可以在macOS上使用F,或在Windows和Linux上使用Ctrl + Alt + F将其提取到字段中。在“介绍字段”对话框中,我们可以选择在“字段声明”中初始化该字段,为其指定一个名称,例如,theWeatherIs然后选择替换代码中所有四个出现的字段。

介绍字段对话框

当我们按OK时,IntelliJ IDEA在班级顶部创建一个新字段:

String theWeatherIs = “The weather is”;

IntelliJ IDEA还使用新字段更新了该方法:

private void getWeather() {        switch (myPlanet.getCountryWeather()) {            case "Spring" -> System.out.println(theWeatherIs + " warm in the UK");            case "Summer" -> System.out.println(theWeatherIs + " hot in the UK");            case "Autumn" -> System.out.println(theWeatherIs + " cool in the UK");            default -> System.out.println(theWeatherIs + " cold in the UK");        }    }

查看视频中的步骤

提取变量

这样的链接方法可能很难理解,因此让我们将其重构为一个单独的变量:

int planetNameLength = myPlanet.getName().length();

您可以将光标放在链接的方法调用中,然后使用箭头键选择要提取到新变量的数量。

提取变量下拉列表

让我们提取myPlanet.getName().length()部分。我们可以在macOS上使用V,在Windows和Linux上使用Ctrl + Alt + V将其提取为变量并命名 planetNameLength。现在我们有了一个用于此计算的局部变量:

System.out.println(“Planet name is ” + myPlanet.getName().length());

IntelliJ IDEA还创建了我们的新变量:

int planetNameLength = myPlanet.getName().length();

…并用新变量替换了我们的代码:

System.out.println(“Number of characters in planet name = ” + planetNameLength);

查看视频中的步骤

提取参数

让我们UK在此代码中提取国家/地区,并将其作为参数传递给country:

  private void getWeather() {        switch (myPlanet.getCountryWeather()) {            case "Spring" -> System.out.println(theWeatherIs + " warm in the UK");            case "Summer" -> System.out.println(theWeatherIs + " hot in the UK");            case "Autumn" -> System.out.println(theWeatherIs + " cool in the UK");            default -> System.out.println(theWeatherIs + " cold in the UK");        }    }

为此,让我们选择UK并在macOS上使用P,在Windows和Linux上使用Ctrl + Alt + P。我们可以给它起一个名字,例如country。我将要求IntelliJ IDEA替换所有四个实例,然后选择“重构”。IntelliJ IDEA更新了我们的方法签名参数:

getWeather(“UK”);

…并替换其在文本中的所有四个出现:

 private void getWeather(String country) {        switch (myPlanet.getCountryWeather()) {            case "Spring" -> System.out.println(theWeatherIs + " warm in the " + country);            case "Summer" -> System.out.println(theWeatherIs + " hot in the " + country);            case "Autumn" -> System.out.println(theWeatherIs + " cool in the " + country);            default -> System.out.println(theWeatherIs + " cold in the " + country);        }    }

查看视频中的步骤

摘录摘要

提取重构的快捷方式具有对称性,可以帮助您记住它们。

对于macOS,它们是 +您要提取的内容的首字母,对于Windows和Linux,它们是Ctrl + Alt +您要提取的内容的首字母:

  • 中号编制方法
  • /strong> onstants
  • ields
  • V ariables
  • P arameters

是的,我知道这在技术上是五合一的,但是我真的想向您展示快捷方式的对称性,因为它有助于我更快地学习。在继续进行第二种主要重构类型之前,如果您改变主意,并且要内联先前提取的内容,该怎么办下文中我将详细讲解这个问题!

标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年11月13日
下一篇 2020年11月13日

相关推荐

发表回复

登录后才能评论