Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Case Analysis of Java/JavaScript/ABAP Code refactoring

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

本篇内容介绍了"Java/JavaScript/ABAP代码重构实例分析"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

在方法里引入一个布尔类型的参数控制方法的行为,这种做法正确吗?

看看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;}

为什么我们不应该在Java 接口中使用Array:

避免Array的原因之一:Array若使用不当,会造成性能问题

避免Array的原因之一:Array若使用不当,会造成性能问题

避免Array的原因之二:Array是面向过程编程领域的概念,使用Java面向对象的集合类,比如List,而不是Array

看个具体例子:

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不是类型安全的!

下面的代码能通过编译,但是运行时会报ArrayStoreException的异常:

Number[] numbers = new Integer[10];numbers[0] = Long.valueOf( 0 );

而使用JDK的集合类比如List,就能在编译器即检测出这类错误。

Javascript里有趣的逗号function a() { console.log("I was called!"); return "Jerry";}var b = a(), a;

然后执行下面的代码:

console.log(b);

会打印出Jerry

再看这段代码:

var d = (function c(){ return a(),a;})();console.log(d);

会打印出:

I was called!function a() { console.log("I was called!"); return "Jerry";}

再看这段代码呢?

(function() { var e = f = 1;})();

直接报错:Uncaught ReferenceError: f is not defined

JavaScript里有趣的分号var b = function(para) { return { doSomething: function() { console.log("hello: " + para); return para; } }}var a = 1, x = 3, y = 4, ss = 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小技巧 - 如何把您自己增强逻辑植入到legacy遗留代码中var bigFunction = function() { // big logic console.log("big logic"); // 这句话模拟我们在一段很冗长的遗留代码里植入自己的新逻辑}// 下面这种解决方案不会直接修改遗留函数本身,显得比较优雅var _old = bigFunction;bigFunction = function() { if ( _old ) { _old(); } console.log("our own enhancement");}bigFunction();// 第三种解决方案采用了面向切片编程思想,显得更加高级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();} 传统方案:在充满了业务逻辑的函数体里强行加入红色标准的搜集性能测试的工具代码,这个实现显得很丑陋: 再看看采用面向切片编程思路的解决方案:AOP - Aspect Oriented Programmingvar 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: ")();} 如何避免代码中大量的IF - ELSE 检查 在调用真正的OData API之前,系统有大量的IF ELSE对API的输入参宿进行检查: 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 }}

更优雅的解决方案:

把这些不同的检查规则封装到一个个JavaScript函数里,再把这些函数作为一个规则对象的属性:

var valid_rules = { not_empty: function( value ) { return value.length !== ''; }, max_length: function( value ) { return value.length HTML5 > Flash > Form(default)function isActiveXSupported(){ //... return false;}function isHTML5Supported(){ //... return false;}function isFlashSupported(){ //... return false;}

好多的IF -ELSE啊:

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);

再看职责链设计模式的实现:

Chain of Responsibilityvar 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; }}

代码整洁优雅:

var uploadAPI = getActiveX.after(getHTML5).after(getFlash).after(getForm)();console.log(uploadAPI);Java中的Stringpublic 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程序反编译出来学习:

"Java/JavaScript/ABAP代码重构实例分析"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report