Hello everyone, this is the table of contents of my speech today, divided into Java, JavaScript, ABAP three programming languages to tell.
Java
•JAD • Javap •Java Decompiler •Source Monitor •Visual VM •Refactor Menu in Eclipse
ABAP
•Code inspector
•Refactor feature in AIE
•Code coverage
JavaScript
•ESLint for Fiori Apps
•Check Jenkins build log
•JSlint for Sublime Text 2
•Code check in WebIDE
•Profile in Chrome
Is it correct to introduce a Boolean parameter control method behavior into a method?
See what it says on StackOverflow.
Best practices for defining constants in Java:
Developer.51cto.com/art/201509/…
Which of these two methods of defining constants in Java is better?
package one;
public interface Constants {
String NAME = "Sun Wukong";
int BP = 10000;
}
Copy the code
or
package two;
public class Constants {
public static final String NAME = Vegeta;
public static final int BP = 9000;
}
Copy the code
Why we shouldn’t use Array in our Java interface:
Eclipsesource.com/blogs/2014/…
One reason to avoid arrays: Arrays can cause performance problems if used improperly
One reason to avoid arrays: Arrays can cause performance problems if used improperly
Reason no. 2 to avoid Array: Array is a concept from the procedural programming world, using Java object-oriented collection classes such as List instead of Array
Here’s a concrete example:
String[] array = { "Steve Jobs"."Zhang Xiaolong" };
List list = Arrays.asList( array );
System.out.println( list );
// Print out [Steve Jobs, Zhang Xiaolong]
System.out.println( array );
// -> [Ljava.lang.String;@6f548414
list.equals( Arrays.asList( "Steve Jobs"."Zhang Xiaolong"))// -> true
array.equals( new String[] { "Steve Jobs"."Zhang Xiaolong"})// -> false
Copy the code
See the difference?
Arrays is not type-safe!
The following code will compile, but will run with an ArrayStoreException exception:
Number[] numbers = new Integer[10];
numbers[0] = Long.valueOf( 0 );
Copy the code
Using the JDK’s collection classes, such as List, can detect these errors at the compiler.
Interesting commas in Javascript
function a() {
console.log("I was called!");
return "Jerry";
}
var b = a(), a;
Copy the code
Then execute the following code:
console.log(b);
Copy the code
It prints out Jerry
Look at this code again:
var d = (function c(){
returna(),a; }) ();console.log(d);
Copy the code
Will print:
I was called!
function a() {
console.log("I was called!");
return "Jerry";
}
Copy the code
What about this code?
(function() {
var e = f = 1; }) ();Copy the code
Uncaught ReferenceError: f is not defined
Interesting semicolons in JavaScript
var b = function(para) {
return {
doSomething: function() {
console.log("hello: " + para);
returnpara; }}}var a = 1, x = 3, y = 4, s
s = a + b
(x + y).doSomething() // Print hello: 7
console.log(s) // Print out 8
function test(i){
var result = i++;
return
result
}
console.log("test: " + test(3)) // Print undefined
Copy the code
Let’s go ahead and look at this code
s = function(x){ console.log("called: " + x ); return x}
(1 + 2).toString()
s = function(x){ console.log("called: " + x ); return x}(1 + 2).toString()
// Print called: 3
Copy the code
Tip – How do you embed your own enhanced logic into Legacy code
var bigFunction = function() {
// big logic
console.log("big logic"); // This sentence simulates how we embed our new logic in a very long piece of legacy code
}
// The following solution is elegant and does not directly modify the legacy function itself
var _old = bigFunction;
bigFunction = function() {
if ( _old ) {
_old();
}
console.log("our own enhancement");
}
bigFunction();
// The third solution uses slice-oriented programming ideas and is more advanced
var bigFunction = function() {
// big logic
console.log("big logic");
}
bigFunction = ( bigFunction || function() {} ).after( function() {
console.log("our own logic");
});
bigFunction();
Copy the code
How to elegantly add utility code for performance test statistics to a function
var append_doms = function() {
var d = new Date(a);// 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();
}
Copy the code
Traditional solution: the implementation is ugly by forcing red standard tool code for collecting performance tests into a function body full of business logic:
Let’s look at a solution that takes a slice-oriented Programming approach: AOP-aspect Oriented Programming
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(a); }).after(function(){
console.log( log_name + ( new Date() - d)); }); }) (); };function test() {
log_time(append_doms, "consumed time: ") (); }Copy the code
How do I avoid a lot of if-else checking in my code
Before calling the real OData API, the system has a large number of IF ELSE checks the API’s input betelgeuse:
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}}Copy the code
A more elegant solution:
Encapsulate these different check rules in JavaScript functions as properties of a rule object:
var valid_rules = {
not_empty: function( value ) {
returnvalue.length ! = =' ';
},
max_length: function( value ) {
returnvalue.length <= MAX_LENGTH ; }}Copy the code
Implement a new check function that checks the properties of an object and performs the check logic:
var valid_check = function() {
for( var i in valid_rules ) {
if ( vali_rules[i].apply( this.arguments) = = =false ) {
return false; }}}Copy the code
OData calls are now very elegant:
var send = function( value ) {
if ( valid_check( value ) === false ) {
return;
}
// call OData API
}
Copy the code
That eliminates the IF ELSE.
Another way to eliminate the IF ELSE branch through the design pattern of the Chain of Responsibility code refactoring:
Let’s look at the traditional implementation:
// Priority: ActiveX > HTML5 > Flash > Form(default)
function isActiveXSupported(){
/ /...
return false;
}
function isHTML5Supported(){
/ /...
return false;
}
function isFlashSupported(){
/ /...
return false;
}
Copy the code
A lot of 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);
Copy the code
Look again at the realization of the responsibility chain design pattern:
Chain of Responsibility
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; }}Copy the code
Clean and elegant code:
var uploadAPI = getActiveX.after(getHTML5).after(getFlash).after(getForm)();
console.log(uploadAPI);
Copy the code
The String in the Java
public class stringTest {
public static void main(String[] args) {
String userName = "Jerry";
String skill = "JS";
String job = "Developer";
Stringinfo = userName + skill + job; System.out.println(info); }}Copy the code
Use Javap to decompile the Hello World program above to learn:
For more of Jerry’s original articles, please follow the public account “Wang Zixi “: