Sunday, March 18, 2012

Life and Java both throws Exception

With each passing days we realize " man proposes and god disposes" , well we plan some thing if there is an deviation we called it exception. Exceptions are annoying and we just hate it. More or less each exception holds lots of vital information of reality,  to go forward we need to decode these exception. I have not much idea how to decode the exceptions that life throw, but in Java we can certainly plan it very well.
Java got a one of the advanced way to handle exception. To know more about exception handling framework in java follow this link.

I will focus how to design exception in applications. Designing of exceptions is start right from application design. Every time an error happens or something goes wrong, the ideal way is capture as much as information possible and notify some controller who can take alternate steps in run time. Mostly applications are integration of java classes and they perform the task my calling each other method. Consider if methodA of Class A use methodB of Class B and methodB encounter some error. Obviously methodB will not have any idea what could be the alternate flow. The best thing methodB can do is log the exception. Capture as much information possible about the error and wrap it to a customize exception to report to the methodA, methodA will have idea if methodB fail what to do. It should have the alternate plan ( exception handling block).
Mostly application are segregated into different layers like UI layer,  business layer and data access layer. So if an error happen in data layer business layer can take a decision can another operation is possible to carry out or need to report error to UI.And UI layer decide what kind of message need to displayed to the user.

Its always best practice to use application specific exception which wrap java native exceptions. So that it can contain more information about the error. And logging of these exception is very important. Logs help to track the error and the reason of error which gives the required information to fix the problem.
I hope with this we can plan better for unexpected flow in Java, And guys I need your inputs how to handle exception comes in life.


Friday, March 16, 2012

new iPad




After more than one year apple released new version of iPad. And they named it "new iPad". With in less than 2 years people around the globe accepted iPad as a new way of computing and in a lots of way iPad is changing life. Apple is successfully building the ecosystem and content by its app store, iTunes, iCloud and the latest iBook which makes iPad more and more useful.
The new iPad got lots of hardware up-gradation in compare to previous version iPad2. In original iPad Steve Job used to claim it gives a magical experience. And the  experience comes from display and touch. To make the magical experience further better, the new iPad got retina display. And this is big break through in terms of display. To complement it new iPad got a better processor, better camera and new generation antenna system.
In the same event apple had released new version of iWork, iPhoto and iMovie. And these are very powerful and simple apps which specially built for iPad. With apps each day iPad is adding more capability to itself. I am thrilled to see iBook. Reading textbook was never so fun before. It makes book reading more interactive.
I was anticipating new ipad will get Siri. Last year apple introduced Siri with iPhone 4s. which created a whole new way to interact with the system. According to me that is the one this new iPad is missing. Its a great tablet with same old price. I just hope it will be in India soon in right price.

Friday, February 24, 2012

All about imports in Java

In any Java source file first line after package declarations are import statements.  These statements enable us to use different classes from outside package. Compiler always looks at the import statements to find class definition.
Instead of import statements we can write fully qualified name. For ex:
java.io. File file = new java.io.File(filepath);
This is a perfectly valid statement. Only problem is it looks littlie complicated to read.  Good codes are not only run well but also easy to read.
So we will stick to
File file = new File(filepath);
To do so we have to write import statement at the beginning right after the package declaration.
package mej.java.example;
import java.io.File;
If we use more than one classes or interface of java.io packages we need to write multiple import statements.
package mej.java.example;
import java.io.File;
import java. FileInputStream;
or we can use wild characters like
java,io.*;
At first impression it looks simple, there are few side effects with this. It takes down the performance of compiler, as compiler has to look whole package for match.  But it doesn’t affect runtime performance of code in anyway. Many time developers misunderstood it.  There can be cases if we use multiple wild character imports. And if both the package contains class with same name, compiler will not able to resolve. And it also reduces the readability of code. It’s difficult to read which class from which package is being used. So the best way is to use import individually for each class or interface.
Another type of imports are static imports. To use it I can say something like
import static java.lang.Match;
We can directly write  ceil(4.83) instead of Math.ceil(4.38).  By static imports we can directly use the methods or members instead of referring through their class name. This is widely used in junit test cases to refer Assert class methods.
In this case also too much use of static imports decrease code maintainability.  
Please let me know your thoughts about java imports.