Less Code == [Less Bugs,Better Readability,Less programmers to hire, Less organizational communication costs, Less maitain cost]
Less Code != [Higher Productivity,Better Performance]
1, import static
remove duplicated namespace
what does it do?
package com.toolbox.lang;
public class ClassWithStaticMember {
public static final String name="value";
public static String say() {
return "111";
}
public static String say(String msg) {
return msg;
}
}
package com.toolbox.lang;
import static com.toolbox.lang.ClassWithStaticMember.say;
import static com.toolbox.lang.ClassWithStaticMember.name;
public class Test {
public static void main(String[] args) {
System.out.println(say());
System.out.println(say(name));
}
}
import同名的member
意味着不同方法签名的member都会import, e.g. say() and say(String msg)
const #45 = Method #46.#48; // com/toolbox/lang/ClassWithStaticMember.say:()Ljava/lang/String;
const #46 = class #47; // com/toolbox/lang/ClassWithStaticMember
const #47 = Asciz com/toolbox/lang/ClassWithStaticMember;
const #48 = NameAndType #49:#38;// say:()Ljava/lang/String;
const #49 = Asciz say;
const #62 = Method #46.#63; // com/toolbox/lang/ClassWithStaticMember.say:(Ljava/lang/String;)Ljava/lang/String;
const #63 = NameAndType #49:#64;// say:(Ljava/lang/String;)Ljava/lang/String;
copy primitive type value
意味着编译后,Test class中static import的String, int 等是值的写入class中,不会在去引用ClassWithStaticMember
43: ldc #55; //String value
45: invokestatic #57; //Method com/toolbox/lang/ClassWithStaticMember.say:(Ljava/lang/String;)Ljava/lang/String;
48: invokevirtual #50; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
when to use it?
Do it when frequent access to static members from one or two classes
Not Do it when has naming conflict or complicated class body, otherwise, make you confuse with so many polluting namespace
2, apply lanuage feature
2.1 String contact
String s = "a" + "b";
compile into:
const #15 = Asciz ([Ljava/lang/String;)V;
const #16 = String #17; // ab
if String constance + String constance
直接编译成结果写入class
else
使用StringBuilder.append
So always use "+" unless you need to use StringBuffer or other specific class
3, inner class {{}}
List<String> list = new ArrayList<String>() {{add("data1");add("data2");}};
4, use PMD to find and remove duplicated code
分享到:
相关推荐
The Definitive Guide to Squarespace Learn to Deliver Custom, Professional Web Experiences for Yourself and Your Clients 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息...
This might not be your particular target language or platform, but you can use you can use what you learn here to engineer or re-evaluate your own code translation system to dramatically increase the...
less code and deliver more! And like all of our .NET components, the XtraNavBar is available with FULL SOURCE CODE. Total Control, Flexibility and Power The No Limits Navigation Bar for .NET
Whether you’re developing cutting-edge code for a new start-up, or maintaining an unruly old system, this guide will help you deliver exactly what you need: quality code. • Simplify unit testing ...
deliver high-quality solutions on time and within budget. I'll focus on those features of J2EE that have proven most useful for solving the commonest problems in enterprise software development. In ...
deliver high-quality solutions on time and within budget. I'll focus on those features of J2EE that have proven most useful for solving the commonest problems in enterprise software development. In ...
演讲出彩的三种方式three ways to deliver Effective Presentations
This book will help you to use the right tools, techniques, and approaches to deliver working solutions for today's modern infrastructure challenges. Style and approach This is a recipe-based book ...
Obi is an advanced cloth simulator for Unity that uses particle physics to deliver high-quality, flexible and easy to use cloth simulation. Available for Windows, Mac, iOS and Android. Features: - ...
As a result we still continue to see high failure rates in new product development, be it inside organizations or startups. Unfortunately, or rather fortunately, these failures are largely avoidable....
You’ll have thought about working in a team to deliver a product to a written schedule. You’ll begin to understand project management, know some metrics and how to review work products, and ...
Identify problem code and use faster alternatives to accomplish the same task Improve scripts by learning how JavaScript stores and accesses data Implement JavaScript code so that it doesn’t slow ...
Obi is an advanced cloth simulator for Unity that uses particle physics to deliver high-quality, flexible and easy to use cloth simulation. 不支持2019
This book will give you the knowledge and tools to measure your mobile website or webapp performance, how to find bottlenecks and the tips to deliver the fastest possible experience to end-users. ...
This book explains JUnit concepts and best practices applied to the test first approach, a foundation for high quality Java components delivered in time and budget. From the beginning you'll be ...
Obi is an advanced cloth simulator for Unity that uses particle physics to deliver high-quality, flexible and easy to use cloth simulation. Available for Windows, Mac, iOS and Android.