Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Go "Find Bugs" in your software.

Life was going smoothly for me last couple of weeks. Hardly anything to
do. So I started reading articles and blogs and I ended up on a blog on
The Serverside which mentioned about the tool called "Find Bugs" I found
the name very interesting and downloaded that tool. The very first thing
I did was that I ran it on the code base of software I was working on. I
found it funny that Find Bugs reported around 700 bugs. Obviously I had
at least some faith in my code which made me believe that it was just crap!

Next thing I did was I started looking at each issue individually and
read through the explanation on why that thing was reported as a Bug.
Suddenly it started making a lot of sense to me. Believe me, 80% of them
were bugs! It efficiently detected some blunders like empty catch blocks
with catch(Exeption e) Assignments to static in non static methods of
class, Dead local store, Class casting problems, Possible Null pointer
dereference etc. And at last I found some real work to do! A lot of
work! It taught me a lot about good coding.

Find Bugs is a must have tool in your swiss army knife for code review.
It takes off a lot of hassles of manual code reviews. It comes with a
Eclipse plugin too which works well but the swing front-end provided by
Find Bugs is really good. It lets you address problems by category.
Where as in Eclipse you have to go to individual source file to find out
bugs in your code. Eclipse also has summary view to see the problems but
it looks really cluttered. Its a must use tool for all Java programmers.

Static Inner classes?

Ever tried serializing the inner class object with non serializable
outer class? We cannot do this! By default compiler adds a reference
to the outer class in the inner class. Remember! we need to access
some attributes of outer class at times. This explicit referencing helps
at that time. When you try to serialize the inner class object you will
get an error.

Now what do we do if we want an inner class object to be serialized?
There is a way to do it. This is where you can use static inner class!.
A static inner class doesn't get a reference to the outer class when
compiled. Its treated as any other standalone class all together. When
an inner class is compiled its actually compiled into a class file with
name OuterClass$InnerClass.class where as the static inner class gets
its own seperate InnerClass.class file. This also meas that you cannot
access OuterClass members from static inner class since we no
longer have any reference to the outer class.

Singleton pattern in JAVA?
Implementing Singleton in java is easy! Isn't it?
Today i came across things which you can screw
up using a singleton pattern. Now I find it very
complex than being so easy. There are some special
cases where our "so called Singleton" is not actually
a singleton.

If one uses lazy initialization of singleton instance,
then you are very likely to create more than instances
of the singleton class. in this case, the method giving
you the instance of the singleton needs to be thread safe,
or we might end up in a situation where two threads try
to get singleton and parallely create to different objects.
But It has performance overheads in some cases.

Other problem is of having singleton objects in the
distributed environment. Objects may span across
JVMs Which creates lot of problems since the
Singleton objects are unique for a JVM. In case
where we use cluster of application servers we might
face this problem.

There are lots of other factors affecting singletons
in java for detailed text read this