翻譯|使用教程|編輯:鮑佳佳|2020-12-17 11:29:47.507|閱讀 434 次
概述:簡化代碼具有很多優勢,包括提高可讀性,解決技術難題以及管理不斷變化的需求。我們將在此博客中介紹三種重構類型:提取和內聯,更改簽名,重命名。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
IntelliJ IDEA 2020.3提供了許多實用的功能,例如調試時的交互式提示,Git暫存支持,對Java 15記錄和密封類的擴展支持等等。它簡化了端點,框架和事件探查器的日常工作。通過基于機器學習技術的更好的代碼完成,更直觀和有用的新的“Welcome”屏幕以及更好的拼寫和語法檢查,整個UX得到了改進。簡而言之,一切都更好!
這篇博客文章涵蓋了與視頻相同的內容,并包含一些其他提示和技巧。
簡化代碼具有很多優勢,包括提高可讀性,解決技術難題以及管理不斷變化的需求。我們將在此博客中介紹三種重構類型:
提取和內聯
簡化代碼的第一種方法是提取它。您可以在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上使用?B或在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 +您要提取的內容的首字母:
是的,我知道這在技術上是五合一的,但是我真的想向您展示快捷方式的對稱性,因為它有助于我更快地學習。在繼續進行第二種主要重構類型之前,如果您改變主意,并且要內聯先前提取的內容,該怎么辦?在下文中我將詳細講解這個問題!
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自: