翻譯|使用教程|編輯:鮑佳佳|2020-12-17 11:29:47.507|閱讀 434 次
概述:簡(jiǎn)化代碼具有很多優(yōu)勢(shì),包括提高可讀性,解決技術(shù)難題以及管理不斷變化的需求。我們將在此博客中介紹三種重構(gòu)類型:提取和內(nèi)聯(lián),更改簽名,重命名。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
IntelliJ IDEA 2020.3提供了許多實(shí)用的功能,例如調(diào)試時(shí)的交互式提示,Git暫存支持,對(duì)Java 15記錄和密封類的擴(kuò)展支持等等。它簡(jiǎn)化了端點(diǎn),框架和事件探查器的日常工作。通過(guò)基于機(jī)器學(xué)習(xí)技術(shù)的更好的代碼完成,更直觀和有用的新的“Welcome”屏幕以及更好的拼寫(xiě)和語(yǔ)法檢查,整個(gè)UX得到了改進(jìn)。簡(jiǎn)而言之,一切都更好!
這篇博客文章涵蓋了與視頻相同的內(nèi)容,并包含一些其他提示和技巧。
簡(jiǎn)化代碼具有很多優(yōu)勢(shì),包括提高可讀性,解決技術(shù)難題以及管理不斷變化的需求。我們將在此博客中介紹三種重構(gòu)類型:
提取和內(nèi)聯(lián)
簡(jiǎn)化代碼的第一種方法是提取它。您可以在IntelliJ IDEA中執(zhí)行五種類型的提取重構(gòu):
提取方法
此方法中的switch語(yǔ)句與該方法的其余部分不一致。
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中,此過(guò)程已簡(jiǎn)化。您需要選擇要提取的完整代碼塊,然后可以在macOS上使用??M,在Windows和Linux上使用Ctrl + Alt + M來(lái)提取方法。
我們可以給新方法起一個(gè)名字,例如,getWeather()并且IntelliJ IDEA將用對(duì)我們新方法的調(diào)用替換原始邏輯:
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"); } } }
通過(guò)再次使用相同的快捷方式,您可以獲取“提取方法”的其他選項(xiàng)。
提示:您可以通過(guò)在macOS上使用?B或在Windows和Linux上使用Ctrl + B導(dǎo)航到方法的聲明或用法。
提取常數(shù)
我們可以在此代碼行中將數(shù)字365提取為常數(shù),因?yàn)榈厍蚴冀K需要大約365天才能完成繞太陽(yáng)的自轉(zhuǎn):
System.out.println("Number of times the planet rotates around the sun is " + 365);
我們可以選擇數(shù)字,然后在macOS上使用??C,在Windows和Linux上使用Ctrl + Alt + C將其提取為常數(shù)。我們可以給它起一個(gè)名字,例如 NUMBER_OF_DAYS_IN_A_YEAR。IntelliJ IDEA在課程開(kāi)始時(shí)創(chuàng)建一個(gè)新的公共靜態(tài)最終常量:
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);
提取字段
在我們提取的方法中,短語(yǔ)“天氣是”重復(fù)了四次,因此讓我們將其提取到字段中:
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將其提取到字段中。在“介紹字段”對(duì)話框中,我們可以選擇在“字段聲明”中初始化該字段,為其指定一個(gè)名稱,例如,theWeatherIs然后選擇替換代碼中所有四個(gè)出現(xiàn)的字段。
當(dāng)我們按OK時(shí),IntelliJ IDEA在班級(jí)頂部創(chuàng)建一個(gè)新字段:
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"); } }
提取變量
這樣的鏈接方法可能很難理解,因此讓我們將其重構(gòu)為一個(gè)單獨(dú)的變量:
int planetNameLength = myPlanet.getName().length();
您可以將光標(biāo)放在鏈接的方法調(diào)用中,然后使用箭頭鍵選擇要提取到新變量的數(shù)量。
讓我們提取myPlanet.getName().length()部分。我們可以在macOS上使用??V,在Windows和Linux上使用Ctrl + Alt + V將其提取為變量并命名 planetNameLength。現(xiàn)在我們有了一個(gè)用于此計(jì)算的局部變量:
System.out.println("Planet name is " + myPlanet.getName().length());
IntelliJ IDEA還創(chuàng)建了我們的新變量:
int planetNameLength = myPlanet.getName().length();
…并用新變量替換了我們的代碼:
System.out.println("Number of characters in planet name = " + planetNameLength);
提取參數(shù)
讓我們UK在此代碼中提取國(guó)家/地區(qū),并將其作為參數(shù)傳遞給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。我們可以給它起一個(gè)名字,例如country。我將要求IntelliJ IDEA替換所有四個(gè)實(shí)例,然后選擇“重構(gòu)”。IntelliJ IDEA更新了我們的方法簽名參數(shù):
getWeather("UK");
…并替換其在文本中的所有四個(gè)出現(xiàn):
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); } }
摘錄摘要
提取重構(gòu)的快捷方式具有對(duì)稱性,可以幫助您記住它們。
對(duì)于macOS,它們是?? +您要提取的內(nèi)容的首字母,對(duì)于Windows和Linux,它們是Ctrl + Alt +您要提取的內(nèi)容的首字母:
是的,我知道這在技術(shù)上是五合一的,但是我真的想向您展示快捷方式的對(duì)稱性,因?yàn)樗兄谖腋斓貙W(xué)習(xí)。在繼續(xù)進(jìn)行第二種主要重構(gòu)類型之前,如果您改變主意,并且要內(nèi)聯(lián)先前提取的內(nèi)容,該怎么辦?在下文中我將詳細(xì)講解這個(gè)問(wèn)題!
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自: