Presentation is loading. Please wait.

Presentation is loading. Please wait.

Effective Java Gruppe Gul Items vi vil uddybe: # 15Minimize Mutability # 23 Don’t use raw types in new code # 47Know and use Libraries # 63Include failure-capture.

Similar presentations


Presentation on theme: "Effective Java Gruppe Gul Items vi vil uddybe: # 15Minimize Mutability # 23 Don’t use raw types in new code # 47Know and use Libraries # 63Include failure-capture."— Presentation transcript:

1 Effective Java Gruppe Gul Items vi vil uddybe: # 15Minimize Mutability # 23 Don’t use raw types in new code # 47Know and use Libraries # 63Include failure-capture information in detail mesages # A short note on items 7, 31, 39, and 55

2 Item 15 Minimize Mutability How to make immutable classes 1.Do not provide any methods that modify the object’s state 2.Ensure that the class cannot be extended 3.Make all fields final 4.Make all fields private 5.Ensure exclusive access to any mutable components

3 ProsCons Constant state throughout lifecycle. Requires separate objects for each distinct value. Thread safe. Easy maintenance. Item 15 Minimize Mutability If absolute immutability is not possible, limit mutability as much as possible

4 Item 15 Minimize Mutability public final class MyImmutable { public final Køretøj fiat = new Køretøj(); public Køretøj getKøretøj(){ return fiat; } private final int max; private final int min; public MyImmutable(int min, int max){ this.min = min; if(max > min) this.max = max; else{ String msg = “error:max <= min, max = " + max +" & min =" + min; throw new IllegalArgumentException(msg); } public int getMax() { return max; } public static MyImmutable getImmutable(int min, int max){ return new MyImmutable(min, max); } Code Example A The class is final Do not do this! No accessor and no public types Use static methods and private constructors

5 Effective Java Gruppe Gul Plan # 15Minimize Mutability # 23 Don’t use raw types in new code # 47Know and use Libraries # 63Include failure-capture information in detail mesages # A short note on items 7, 31, 39, and 55

6 Item 23 Don’t use raw types in new code - A raw type is a generic type without an actual type parameter e.g.: private Collection cars =... - When using raw types one loses all type safety e.g. the following is allowed: cars.add(new Airplane())

7 Item 23 Issues when using raw types - When getting elements from raw types one must cast them e.g.: Car someCar = (car) cars.get(0) - This example would throw a ClassCastException at runtime as the element at rank 0 is an airplane!

8 Item 23 Unbounded types and Wildcards - Do not know / care about the element type of a collection? - Raw types might seem like the answer, but are not type safe - Unbounded wildcards achieve the same functionality while remaining both flexible and type safe. e.g.: public void collMethod( Collection coll){…}

9 Item 23 Old code and Eclipse - As generics were not added to Java prior to the 5th edition one may encounter raw types in older code segments - Eclipse issues a warning when raw types are used in the source code

10 Effective Java Gruppe Gul Plan # 15Minimize Mutability # 23 Don’t use raw types in new code # 47Know and use Libraries # 63Include failure-capture information in detail mesages # A short note on items 7, 31, 39, and 55

11 Item 47 Know and use libraries  Use the knowledge of experts and other developers  Don’t waste time on ad hoc solutions  The library is being opdated and optimized continously  The code becomes more mainstream, easier to read, and maintain

12 Item 47 Know and use libraries Therefore If you have any doubts concerning the content of the libraries – Look it up in the API! If it does not exist – Code it! Attention! Beware of updates in the libraries

13 Effective Java Gruppe Gul Plan # 15Minimize Mutability # 23 Don’t use raw types in new code # 47Know and use Libraries # 63Include failure-capture information in detail mesages # A short note on items 7, 31, 39, and 55

14 Item 63 Include failure-capture information in detail messages ProsCons Easier to debugRequires additional coding Easy to maintain Looks more professional

15 Item 63 Include failure-capture information in detail messages public class MyImmutable2 { private final int min; private final int max; public MyImmutable2(int min, int max) { this.min = min; if(max <= min) { String msg = "reason-: max <= min, max = " + max +" & min =" + min; throw new IllegalArgumentException(msg); } else this.max = max; } public static void main(String[] args) { try{ MyImmutable test = new MyImmutable(2,2); } catch(Exception e){ System.out.println(e); } } //OUTPUT: java.lang.IllegalArgumentException: reason-: max <= min, max = 2 & min =2 } Code Example D

16 Effective Java Gruppe Gul Plan # 15Minimize Mutability # 23 Don’t use raw types in new code # 47Know and use Libraries # 63Include failure-capture information in detail mesages # A short note on items 7, 31, 39, and 55

17 Items 7, 31, 39, 55 A short note on items 7, 31, 39, and 55 Item #7 – Finalizers: - Java takes care of garbage collection itself - Equal to C++’s destructors Item #31 – Use Instance Fields instead of Ordianls: - When you use the fields you control the value - The Enum can be expanded without breaking the code

18 Items 7, 31, 39, 55 A short note on items 7, 31, 39, and 55 Item #39 – Make Defensive copies when needed: - Protects your code from malicious clients - Futhers encapsulation Item #55 – Optimize Judiciously: - Optimization often gets in the way of making working code - Improving your choice of algorithms often is the best way to optimize your program

19 Effective Java Gruppe Gul Items vi vil uddybe: # 15Minimize Mutability # 23 Don’t use raw types in new code # 47Know and use Libraries # 63Include failure-capture information in detail mesages # A short note on items 7, 31, 39, and 55


Download ppt "Effective Java Gruppe Gul Items vi vil uddybe: # 15Minimize Mutability # 23 Don’t use raw types in new code # 47Know and use Libraries # 63Include failure-capture."

Similar presentations


Ads by Google