您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“Java/JavaScript/ABAP代碼重構(gòu)實(shí)例分析”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
看看stackoverflow上是怎么說的。
Java里這兩種定義常量的方法,哪種更好?
package one;public interface Constants { String NAME = "孫悟空"; int BP = 10000; }
或
package two;public class Constants { public static final String NAME = "貝吉塔"; public static final int BP = 9000; }
為什么我們不應(yīng)該在Java 接口中使用Array:
避免Array的原因之一:Array若使用不當(dāng),會(huì)造成性能問題
避免Array的原因之一:Array若使用不當(dāng),會(huì)造成性能問題
避免Array的原因之二:Array是面向過程編程領(lǐng)域的概念,使用Java面向?qū)ο蟮募项?,比如List,而不是Array
看個(gè)具體例子:
String[] array = { "喬布斯", "張小龍" }; List list = Arrays.asList( array ); System.out.println( list );// 打印輸出 [喬布斯, 張小龍]System.out.println( array );// -> [Ljava.lang.String;@6f548414list.equals( Arrays.asList( "喬布斯", "張小龍" ) )// -> truearray.equals( new String[] { "喬布斯", "張小龍" } )// -> false
看出差距了吧?
Arrays不是類型安全的!
下面的代碼能通過編譯,但是運(yùn)行時(shí)會(huì)報(bào)ArrayStoreException的異常:
Number[] numbers = new Integer[10]; numbers[0] = Long.valueOf( 0 );
而使用JDK的集合類比如List,就能在編譯器即檢測(cè)出這類錯(cuò)誤。
function a() { console.log("I was called!"); return "Jerry"; }var b = a(), a;
然后執(zhí)行下面的代碼:
console.log(b);
會(huì)打印出Jerry
再看這段代碼:
var d = (function c(){ return a(),a; })();console.log(d);
會(huì)打印出:
I was called!function a() { console.log("I was called!"); return "Jerry"; }
再看這段代碼呢?
(function() { var e = f = 1; })();
直接報(bào)錯(cuò):Uncaught ReferenceError: f is not defined
var b = function(para) { return { doSomething: function() { console.log("hello: " + para); return para; } } }var a = 1, x = 3, y = 4, s s = a + b (x + y).doSomething() // 打印出 hello: 7console.log(s) // 打印出 8function test(i){ var result = i++; return result }console.log("test: " + test(3)) // 打印出undefined
s = function(x){ console.log("called: " + x ); return x} (1 + 2).toString() s = function(x){ console.log("called: " + x ); return x}(1 + 2).toString()// 打印出 called: 3
var bigFunction = function() { // big logic console.log("big logic"); // 這句話模擬我們?cè)谝欢魏苋唛L(zhǎng)的遺留代碼里植入自己的新邏輯}// 下面這種解決方案不會(huì)直接修改遺留函數(shù)本身,顯得比較優(yōu)雅var _old = bigFunction; bigFunction = function() { if ( _old ) { _old(); } console.log("our own enhancement"); } bigFunction();// 第三種解決方案采用了面向切片編程思想,顯得更加高級(jí)var bigFunction = function() { // big logic console.log("big logic"); } bigFunction = ( bigFunction || function() {} ).after( function() { console.log("our own logic"); }); bigFunction();
var append_doms = function() { var d = new Date(); // dirty code - nothing to do with application logic!!! for( var i = 0; i < 100000; i++) { var div = document.createElement( "div"); document.body.appendChild(div); } // dirty code - nothing to do with application logic!!! console.log(" time consumed: " + ( new Date() - d)); };function test() { append_doms(); }
傳統(tǒng)方案:在充滿了業(yè)務(wù)邏輯的函數(shù)體里強(qiáng)行加入紅色標(biāo)準(zhǔn)的搜集性能測(cè)試的工具代碼,這個(gè)實(shí)現(xiàn)顯得很丑陋:
var append_doms = function() { for( var i = 0; i < 100000; i++) { var div = document.createElement( "div"); document.body.appendChild(div); } };var log_time = function( func, log_name) { return func = ( function() { var d; return func.before( function(){ d = new Date(); }).after( function(){ console.log( log_name + ( new Date() - d)); }); })(); };function test() { log_time(append_doms, "consumed time: ")(); }
在調(diào)用真正的OData API之前,系統(tǒng)有大量的IF ELSE對(duì)API的輸入?yún)⑺捱M(jìn)行檢查:
var send = function() { var value = input.value; if( value.length === '' ) { return false; } else if( value.length > MAX_LENGTH) { return false; } ... // lots of else else { // call OData API } }
更優(yōu)雅的解決方案:
把這些不同的檢查規(guī)則封裝到一個(gè)個(gè)JavaScript函數(shù)里,再把這些函數(shù)作為一個(gè)規(guī)則對(duì)象的屬性:
var valid_rules = { not_empty: function( value ) { return value.length !== ''; }, max_length: function( value ) { return value.length <= MAX_LENGTH ; } }
實(shí)現(xiàn)一個(gè)新的檢查函數(shù),變量檢查對(duì)象的屬性,執(zhí)行校驗(yàn)邏輯:
var valid_check = function() { for( var i in valid_rules ) { if ( vali_rules[i].apply( this, arguments) === false ) { return false; } } }
現(xiàn)在的OData調(diào)用函數(shù)非常優(yōu)雅了:
var send = function( value ) { if ( valid_check( value ) === false ) { return; } // call OData API}
通過這種方式消除了IF ELSE。
另一種通過職責(zé)鏈 Chain of Responsibility 的設(shè)計(jì)模式 design pattern消除IF ELSE分支的代碼重構(gòu)方式:
先看傳統(tǒng)方式的實(shí)現(xiàn):
// Priority: ActiveX > HTML5 > Flash > Form(default)function isActiveXSupported(){ //... return false; }function isHTML5Supported(){ //... return false; }function isFlashSupported(){ //... return false; }
好多的IF -ELSE?。?/p>
var uploadAPI;if ( isActiveXSupported()) { // lots of initialization work uploadAPI = { "name": "ActiveX"}; }else if( isHTML5Supported()) { // lots of initialization work uploadAPI = { "name": "HTML5"}; }else if( isFlashSupported()) { // lots of initialization work uploadAPI = { "name": "Flash"}; }else { // lots of initialization work uploadAPI = { "name": "Form"}; }console.log(uploadAPI);
再看職責(zé)鏈設(shè)計(jì)模式的實(shí)現(xiàn):
var getActiveX = function() { try { // lots of initialization work return { "name": "ActiveX"}; } catch (e) { return null; } }var getHTML5 = function() { try { // lots of initialization work return { "name": "HTML5"}; } catch (e) { return null; } }
代碼整潔優(yōu)雅:
var uploadAPI = getActiveX.after(getHTML5).after(getFlash).after(getForm)();console.log(uploadAPI);
public class stringTest { public static void main(String[] args) { String userName = "Jerry"; String skill = "JS"; String job = "Developer"; String info = userName + skill + job; System.out.println(info); } }
用javap將上面的Hello World程序反編譯出來學(xué)習(xí):
“Java/JavaScript/ABAP代碼重構(gòu)實(shí)例分析”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。