美文网首页
通用程序设计

通用程序设计

作者: 李军_6eaa | 来源:发表于2018-02-19 15:30 被阅读0次
最小化local variables的范围
  • 最强有力的方式是:在该local variables第一次使用的地方declare it
  • 优先使用for loops, 而不是while loops
  • keep methods small and focused,一个方法只执行一个activity
for 循环和while循环相比,有哪些好处?
  • 能最小化local variables的范围
for (Iterator<Element> i = c.iterator(); i.hasNext(); ) {
    Element e = i.next();
    ... // Do something with e and i
}
...

// Compile-time error - cannot find symbol i
for (Iterator<Element> i2 = c2.iterator(); i.hasNext(); ) {
    Element e2 = i2.next();
    ... // Do something with e2 and i2
} 
Iterator<Element> i = c.iterator();
while (i.hasNext()) {
    doSomething(i.next());
}
...
Iterator<Element> i2 = c2.iterator();
while (i.hasNext()) {             // BUG!
    doSomethingElse(i2.next());
}
  • for循环更简洁,能增强可读性
for (int i = 0, n = expensiveComputation(); i < n; i++) {
    ... // Do something with i;
}
优先使用for-each loops,而不是传统的for loops
  • 在对collections and arrays进行迭代时,优先使用for-each loops
  • 如果想使用for-each loops迭代any object,只需要该object实现Iterable接口
懂得使用the libraries
  • 不要自造轮子,代价太大
// Common but deeply flawed!
static Random rnd = new Random();
static int random(int n) {
    return Math.abs(rnd.nextInt()) % n;
}
// just use
Random.nextInt(int)
// for Java 7
use ThreadLocalRandom, not Random
// for fork join pools and parallel streams
use SplittableRandom 
  • 通过使用standard library,你可以使用写这些库的专家的知识以及已经使用过的这些库的那些人的经验
  • 通过使用standard library,如果有flaws被发现,那么它们会在 the next release 中fixed掉
  • 通过使用standard library,你可以专注于自己的应用,而不用关心某些不太紧要的细节
  • standard library,随着时间的推移,性能会越来越高,而且不需要你维护
  • standard library,随着时间的推移,功能越来越丰富,健全
  • 通过使用standard library,你的代码会更易读,可维护性更强,因为你的代码属于mainstream
  • 为了便利的使用standard library,你需要关注 every major release,不断学习这些新添加的特性
  • 每一个程序员都应该熟悉 java.lang, java.util, java.io, 以及它们的 subpackages
  • 如果Java platform libraries 不能满足你,接下来应该先去查找高质量的第三方库,比如:Guava,如果你实在找不到满足你需要的库,再去自己编码实现它
如果需要exact answer,请不要使用float 和 double类型
  • float 和 double类型尤其不适合货币计算
  • 应该使用BigDecimal, int, long进行货币计算
  • 如果数量级不超过9位十进制数,则用int;如果不超过18位,则用long;如果超过18位或者含有小数,则用BigDecimal
说说Java的两种类型系统
  • primitives 和 reference types
  • primitives 包括 比如 int, double, and boolean;reference types 包括比如String 和 List
  • primitive type对应的reference type称为boxed primitive
说说primitives 和 boxed primitives的区别
  • primitives仅仅拥有values,boxed primitives 除此以外,还拥有不同于values的identities
  • boxed primitives有一个nonfunctional value,即null
  • primitives 更省时间,更省空间
  • 对boxed primitives,使用== operator,一般总是错误的,因为该操作符会比较identities,最后造成不期望的结果
// error! 由于==符号,(new Integer(42), new Integer(42))比较,会返回1
Comparator<Integer> naturalOrder =
    (i, j) -> (i < j) ? -1 : (i == j ? 0 : 1);
//right way
Comparator<Integer> naturalOrder = (iBoxed, jBoxed) -> {
“int i = iBoxed, j = jBoxed; // Auto-unboxing
    return i < j ? -1 : (i == j ? 0 : 1);
};
  • 在一个操作中,混合使用 primitives 和 boxed primitives,会使boxed primitive 自动拆箱,下面的代码会报NullPointerException的错误
public class Unbelievable {
    static Integer i;

    public static void main(String[] args) {
        if (i == 42)  //NullPointerException
            System.out.println("Unbelievable");
    }
}
public static void main(String[] args) {
    Long sum = 0L;
    for (long i = 0; i < Integer.MAX_VALUE; i++) {
        sum += i; //重复的拆箱和装箱,影响性能
    }
    System.out.println(sum);
}
什么时候必须使用boxed primitives
  • 充当collections中的元素
  • 激活反射方法
当其他类型更适合时,请不要使用strings类型
  • Strings不适合替代其他的value types
  • Strings不适合替代enum types
  • Strings不适合替代aggregate types(复合类型),应该提供一个私有的静态成员类
// Inappropriate use of string as aggregate type
String compoundKey = className + "#" + i.next(); 
小心String concatenation operator (+)的性能
  • 不要使用String 的+操作符来 combine more than a few strings,应该使用StringBuilder 的 append 方法
使用interfaces来引用objects
  • 如果有适合的接口存在,则 parameters, return values, variables, 以及 fields 都应该由interface types宣称
// Good - uses interface as type
Set<Son> sonSet = new LinkedHashSet<>();
// Bad - uses class as type!
LinkedHashSet<Son> sonSet = new LinkedHashSet<>();
有哪些情况适合使用具体类来引用objects
  • value classes, 比如 String 和 BigInteger
  • class-based framework,比如 大多数 java.io 类,如OutputStream
  • 该interface中没有提供某些方法,比如PriorityQueue中有个comparator方法,该方法没有在Queue interface中提供
优先使用 interfaces 而不是 reflection
  • java.lang.reflect 提供获取一个类的 Constructor, Method, Field,你可以使用它们 construct instances, invoke methods, 以及 access fields of the class
reflection的缺点
  • 失去了编译期类型检查的所有好处
  • 执行reflective access的代码笨拙又冗长
  • 性能差
怎么结合使用 interfaces 和 reflection
  • 仅仅使用反射 create instances,使用interfaces和superclass access them
  • 小例子
// Reflective instantiation with interface access
public static void main(String[] args) {
    // Translate the class name into a Class object
    Class<? extends Set<String>> cl = null;
    try {
        cl = (Class<? extends  Set<String>>)  // Unchecked cast!
                Class.forName(args[0]);
    } catch (ClassNotFoundException e) {
        fatalError("Class not found.");
    }
    // Get the constructor
    Constructor<? extends Set<String>> cons = null;
    try {
        cons = cl.getDeclaredConstructor();
    } catch (NoSuchMethodException e) {
        fatalError("No parameterless constructor");
    }
    // Instantiate the set
    Set<String> s = null;
    try {
        s = cons.newInstance();
    } catch (IllegalAccessException e) {
        fatalError("Constructor not accessible");
    } catch (InstantiationException e) {
        fatalError("Class not instantiable.");
    } catch (InvocationTargetException e) {
        fatalError("Constructor threw " + e.getCause());
    } catch (ClassCastException e) {
        fatalError("Class doesn't implement Set");
    }
    // Exercise the set
    s.addAll(Arrays.asList(args).subList(1, args.length));
    System.out.println(s);
}
private static void fatalError(String msg) {
    System.err.println(msg);
    System.exit(1);
}
使用native methods,要谨慎
  • native methods,即使用C或C++写的方法,Java 允许使用Java Native Interface (JNI) 来调用native methods
  • native methods 使用的胶水语言难以阅读和书写,难以debug,可移植性差,易造成内存污染,不利于GC。所以应该尽可能少的使用native methods
代码优化,要谨慎
  • 过早的优化是万恶之源
  • 不要为了性能而牺牲好的程序结构
  • 致力于写好的代码,而不是快的代码
  • Good API design 会带来 Good performance
  • programs spend 90 percent of their time in 10 percent of their code,必要时,可以借助profiler或jmh来搜寻那10%
坚持公认的命名惯例

相关文章

  • C++模板template用法

    引言 模板(Template)指C++程序设计设计语言中采用类型作为参数的程序设计,支持通用程序设计。C++ 的标...

  • 通用程序设计

    最小化local variables的范围 最强有力的方式是:在该local variables第一次使用的地方d...

  • 通用程序设计

    该篇文章是《Effective Java 2nd》的读书总结,关于此书自然不必多言。如果有需要pdf中文&英文的,...

  • 通用程序设计

    本章主要讨论java的实现细节。讨论了局部变量的处理,控制结构,类库的用法,各种数据类型的用法,以及两种不是由语言...

  • 七、通用程序设计

    47.了解和使用类库 书中通过一个产生随机数的例子来告诉我们使用类库的几个好处: 通过使用标准类库,可以充分利用这...

  • Python --- 序列类型及操作

    笔记主要由来于 慕课网 Python 语言程序设计 序列类型定义序列类型通用操作符序列类型通用函数和方法元组类型...

  • 优化函数式编程:向 PHP 移植 Clojure 函数

    许多通用程序设计语言试图兼容大多数编程范式,PHP 就属于其中之一。不论你想要成熟的面向对象的程序设计,还是程序式...

  • Effective Java - 第8章 通用程序设计

    《Effective Java(第2版)》第8章 通用程序设计,整理的思维导图,文字版见:https://mubu...

  • Effective Java-通用程序设计

    本章内容导图: 1.将局部变量的作用域最小化 同“使类和成员的可访问性最小化”一样,将局部变量的作用域最小化,可以...

  • Effective Java-通用程序设计

    将局部变量的作用域最小化 要使局部变量最小化,最有力的方法就是在第一次使用它的地方声明 几乎每个局部变量的声明都应...

网友评论

      本文标题:通用程序设计

      本文链接:https://www.haomeiwen.com/subject/cbgitftx.html