Wednesday, 20 February 2013

Writing Performance Effective Java Code



Writing Performance Effective Java Code

Based on my prior experience and/or learning's, here are few tips I suggest every Java programmer to follow for writing performance effective Java Code.

  1. Use ‘StringBuilder’ instead of String concatenation: Always prefer the usage of ‘StringBuilder’ when you have requirement of concatenating large number of string values. 
  1. Do not prefer to use immutable class constructors; instead use static factory methods if available.  [eg: use of String constructor class for literals. Instead prefer the usage of String literals.] 
i.e. Prefer String s1=”abc”;    instead of     String s1=new String(“abc”);
      Use      Integer.valueOf(String)    instead of     new Integer(String);
       
  1. Prefer the use of primitive types instead of boxed primitive or wrapper types: 
Eg: Integer sum=0;
       for(int i=0; i<1000; i++)
             sum+=i;

The above program with wrapper type is much slower than this one:
int sum=0;
      for(int i=0; i<1000; i++)
             sum+=i; 

  1. Prefer the use of primitive data comparisons to String value comparisons.
    1. In primitives, prefer this comparison order. i.e. use boolean, byte, char, short, int, long instead of ‘float’ and ‘double’ comparisons 
  1. Minimize the usage of synchronization and synchronized utility classes in Java API’s wherever possible. 
eg. Prefer use of ‘StringBuilder’ to ‘StringBuffer’, ‘ArrayList’ to ‘Vector’, ‘HashMap’ to ‘Hashtable’ etc.
  
  1. Minimize the mutability of classes: Always declare the member variables or objects with least possible access specifier and make them ‘final’ when there is guarantee that it’s not changed. 
    1. Try to have all the member variables or objects as ‘private’ unless other access specifier is required
    2. Make all the unchanged primitive variables ‘final
    3. Make all the arrays or collections whose references should remain same as ‘final
    4. Make all the methods ‘final’ when you are sure that the method definition should not be changed.

  1. Keep re-usable code in static final methods: If there is any business logic, or functionality which does not belong to any of your classes or entity, move the methods into a utility class and make all of methods static and final. 
  1. Minimize the scope of local variables: Declare local variables only when it is used for first time. Do not declare them in the very beginning; and use it some where in the middle. By the time reader would have forgot the initial value of variable if any defined.  
  1. Use static block initializers to initialize or construct the expensive objects instead of having them in the methods which gets called many times. 
  1. Prefer the usage of interfaces to reflection: when you follow reflective way of instantiating your class i.e. (with use Class.forName(classname-to-load)), you 
    1. Lose all benefits of compile-time type checking
    2. Suffer with slow performance in method invocation.

Appreciate for taking time and reading through the different tips. If you would like to share your feedback or add on another tip, feel free to comment on the post.

Tuesday, 12 February 2013

Displaying jGrowl message at custom location


Displaying jGrowl message at a custom location:

If you are displaying jGrowl messages in your website, there are five default placement options available for you; top-left, top-right, bottom-left, bottom-right, center. This must be changed in the defaults before the startup method is called.

To do this, you'll have something like
    <script type="text/javascript">
           $.jGrowl.defaults.position = 'top-right';
    </script>

However, jGrowl does not let you directly specify your own positioning of message in web page. 
Solution Approach: Add your own position in the jGrowl CSS file:
To do this, you will have to add one CSS position entry or override any of the default CSS definitions provided by jGrowl in 'jquery.jgrowl.css' file like shown below:
 div.jGrowl.YourDisplayArea {
position: absolute;
left: 3%;
top: 20%;
}

Similarly, you could also position with respect to 'right', 'bottom' attributes available in CSS.

And you can use the new position in your jGrowl messages which will bring up the message in the location as you desired.


$.jGrowl("sample JGrowl message displayed in 'YourDisplayArea '",
{
sticky: true,
position: 'YourDisplayArea'
});


Monday, 11 February 2013

jGrowl messages at different positions


Displaying different jGrowl messages at different positions within the same page


In normal scenario, if you want to display only one jGrowl message in any part of the web site, you will just do it so by creating the messaging and specifying the position accordingly as given in this example (i.e. with any of values for positions --> top-left, top-right, bottom-left, bottom-right, center)

eg: 

          $.jGrowl('This is sticky message which appears on bottom right corner of your website page', 
                        {
sticky: true,
header: 'Simple jGrowl message',
position: 'bottom-right'
});

This displays the simple sticky jGrowl message as expected in 'bottom-right' of page.

However, think about if you want to have one more message displayed; say, in 'top-left' corner; For this, you may try to accomplish this by adding one more similar jGrowl message with position as 'top-left' like one below:

 $.jGrowl('This sticky message should appear on top left', 
                        {
sticky: true,
header: 'Simple jGrowl message',
position: 'top-left'
});

But it doesn't work; instead the jGrowl will display this message just below to the first one (kind of attached to it); This is because by default jGrowl displays all of the messages using default container i.e. default <div> section it uses and hence it will group all of your messages together (if you have multiple); with 'close-all' button to close all of these jGrowl messages at a time.

Solution (Custom containers):

To display jGrowl messages at different locations, you would need to create your own custom containers; This is as simple as creating your own HTML '<div>' section any where in your web page with an 'id' value. You can have the '<div> elements any where in <body> section of HTML or webpage code.

Lets create two DIV's for our examples:

<div id="jgrowlMsg1" class="bottom-right"></div>
<div id="jgrowlMsg2" class="top-left"></div>

And now we just say, 
 
 $('jgrowlMsg1').jGrowl('This is sticky message which appears on bottom right corner of your website page', 
                        {
sticky: true,
header: 'Simple jGrowl message',
position: 'bottom-right'
});



$('jgrowlMsg2').jGrowl('This sticky message should appear on top left', 
                        {
sticky: true,
header: 'Simple jGrowl message',
position: 'top-left'
});


This will bring up messages in different locations as desired in 'bottom-right' and 'top-left' of the webpage respectively.