1 / 19

Effective Java Gruppe Gul

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

nessa
Download Presentation

Effective Java Gruppe Gul

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. Item 15Minimize Mutability

  4. The class is final Code Example A Item 15Minimize Mutability Do not do this! No accessor and no public types public final class MyImmutable { public final Køretøj fiat = new Køretøj(); public KøretøjgetKø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 MyImmutablegetImmutable(int min, int max){ return new MyImmutable(min , max); } Use static methods and private constructors

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

  6. Item 23Don’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 23Issues when using raw types - Whengetting elements from raw types one must cast them e.g.: CarsomeCar = (car) cars.get(0) - Thisexamplewouldthrow a ClassCastException at runtime as the element at rank 0 is an airplane!

  8. Item 23Unbounded types and Wildcards - Do not know / careabout the element type of a collection? - Raw types mightseemlike the answer, but are not type safe - Unbounded wildcards achieve the same functionalitywhile remainingbothflexible and type safe. e.g.: public voidcollMethod( Collection<?> coll){…}

  9. Item 23Old code and Eclipse - As genericswere not added to Java prior to the 5th edition onemayencounterraw types in oldercode segments - Eclipseissues a warningwhenraw types areused in the sourcecode

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

  11. Item 47Know 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 optimizedcontinously The code becomes more mainstream, easier to read, and maintain

  12. Item 47Know and use libraries Therefore If you have anydoubts 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 JavaGruppe Gul Plan # 15 Minimize Mutability # 23 Don’tuse raw types in new code # 47 Know and use Libraries # 63 Include failure-capture information in detailmesages # A short note on items 7, 31, 39, and 55

  14. Item 63Include failure-capture information in detail messages

  15. Code Example D Item 63Include 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 }

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

  17. Items 7, 31, 39, 55A short note on items 7, 31, 39, and 55 Item #7 – Finalizers: - Java takescare of garbagecollectionitself - Equal to C++’s destructors Item #31 – UseInstance Fields instead of Ordianls: - Whenyouuse the fieldsyoucontrol the value - The Enumcanbeexpandedwithoutbreaking the code

  18. Items 7, 31, 39, 55A short note on items 7, 31, 39, and 55 Item #39 – Make Defensive copieswhenneeded: - Protectsyourcode from maliciousclients - Futhersencapsulation Item #55 – OptimizeJudiciously: - Optimizationoftengets in the way of makingworkingcode - Improvingyourchoice of algorithmsoften is the bestway to optimizeyour program

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

More Related