Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

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, 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.

Wednesday, November 9, 2011

"Interface" The Middle Man

We will always find middle man in between producer and consumer of anything. Sometimes more than one layer of middle man. Both producer and end user hate those middle man. They just rip profit by somehow involving in transaction. But no one will deny it require lots of thinking to establish a model which doesn't need middle man. Let’s understand what kind of value these middle men bring.
Like real world every software or services is integration of various modules. Each module is responsible to carry out certain task. Some component consumes the output of other; to integrate these we need middle man. In Java we say it “Interface”.  The basic idea of interface is to act like middle man. But in Java world it can do some additional things.
1)     As the name suggest Interface is basically to integrate two different components.  It exposes the functionality of one component to other in a simple manner.
2)     It can be used to hold Constants of the project. As the properties of Interface are always public and final.
3)     Java doesn’t allow multiple inheritance. So One class can’t extend multiple classes, the work around in Interface. One class can implement multiple interfaces. And a reference of an Interface can hold the Object of implementing class. 
Class A implements Ia {}
Ia ia = new A();
In this way it gives lots of option to designer.
4)     Interface enforces certain behavior on the class. For ex: if my object is instance of Serializable, then the object can be persisted. Interface is used to mark the Object.
5)     I found the best use of interface is it helps to design lots of generic stuffs. It helps in reducing tight coupling between component.
Please share your thoughts and experience with Interface in programming and real life as well.