13
Java Best Practices Major Books and Concepts. - Effective Java - Concurrency in practice - Design Patterns - Design Principles - Many more……

Effective Java Session

Embed Size (px)

Citation preview

Page 1: Effective Java Session

Java Best Practices

• Major Books and Concepts.- Effective Java- Concurrency in practice- Design Patterns- Design Principles- Many more……

Page 2: Effective Java Session

Why ???

• JVM is Awesome but What about Java ?!• Coding is easy… What about

maintenance ?!• Code review... What are we doing

actually ?!• It was working fine with my old

machine… my new multicore machine sucks !

• Quick fixes …. Any one has done it before?!

• All consolidates to one thing… “Best Practices”

Page 3: Effective Java Session

Brief Overview

• Effective Java – Gives various rules/items to be followed in order to develop Robust, Maintainable, Efficient, Correct Project in Java.

• Concurrency In practice – Tells us how to use concurrency… What are we missing ?

• Design Patterns – Reuse the our ancestor property ! (List of common patterns while developing a software.)

Page 4: Effective Java Session

Effective Java

• Creating and Destroying the Objects.1. Favoring Static Factories over Constructors.2. Favoring Builder DP over Constructor chaining.3. Singleton Implementation with Enum !4. Private Constructors for Util/Constant like classes.5. Avoid creating unnecessary objects.6. Avoid Memory Leaks ! 7. Avoid Finalizers! ‘finalize()’ who Remembers this?!

Page 5: Effective Java Session

Advantages of Favoring Static Factories over Constructors

• What is Static Factory ? - Factory Design Pattern.

• They have names !• Can re-use the existing object if

needed !• Can return any Sub Type of their return

type.- Interface based Frameworks.- Service provider frameworks.

Page 6: Effective Java Session

Disadvantages of Favoring Static Factories over Constructors

• Can not be Sub classed … • Not readily distinguished from other factory

methods.• valueOf• of• getInstance• newInstance• getType• newType

Page 7: Effective Java Session

Favoring Builder over Constructor chaining.

• What is Constructor chaining ?• Java Bean way… Any problems ?!• How to make it Immutable? • Why Immutable ?!• What is Builder Design Pattern ?• When to use …?

Page 8: Effective Java Session

Singleton

• How to write singleton ? What’s it ??• Lazy Loading• Early loading • Thread safe ?!! • Taking care of Serialization in Singleton.• Best and simple way… To delegate all the

pain!• Yes… Enum !

Page 9: Effective Java Session

Enforce non-instantiability with private Constructor.

• Classes having only static methods.• Classes having only Global Constants.• To prevent class from sub classed.

Page 10: Effective Java Session

Avoid Creating Unnecessary Objects.

• Check for object instantiation in local methods.• Boxing… Auto Boxing… • Prefer primitives to boxed primitives.• Careful on auto boxing.• Maintaining own object pool is bad idea.• Don’t re-use whenever you should create new

one, Don’t create new one whenever you can re-use old one !

Page 11: Effective Java Session

Eliminate Obsolete Object Reference.

• Stack Example with Array.• Nulling out object reference… Should

not be done much… Rather limit the scope.

• Cache… A potential place to memory leak…

• Listeners and Callbacks.• When we should be alert of the Memory

Leaks ?

Page 12: Effective Java Session

Finalizers !

• Finalize() method… from Object• Finalizers are unpredictable, often dangerous,

and generally unnecessary.• Never do anything time-critical in finalizers.• Performance penalty?! Really want to use it ?• Explicit termination method is better… use it

with try-finally block..• Finalizer chaining and Finalizer Guardian

idioms.

Page 13: Effective Java Session

THANK YOU !