Upload
cumulations-technologies
View
108
Download
0
Embed Size (px)
Citation preview
Java Best Practices
• Major Books and Concepts.- Effective Java- Concurrency in practice- Design Patterns- Design Principles- Many more……
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”
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.)
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?!
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.
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
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 …?
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 !
Enforce non-instantiability with private Constructor.
• Classes having only static methods.• Classes having only Global Constants.• To prevent class from sub classed.
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 !
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 ?
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.
THANK YOU !