Monday, July 22

Item 24 : Eliminate unchecked warnings

What is unchecked warning ?

While you compile a piece of code , It is possible that It compile with warnings.You may completely ignore these warnings to get your code compiled.
What is the threat in ignoring warnings?

ClassCast Exception. Warning suggests you How you are leaving your code with risk of ClassCast exception.So It is must to resolve all warnings.
Does @suppresswarning removes the risk?

Not at all. It makes the situation more dangerous.If you add @supprresswarning on method level to avoid a specific warning It will curb all warnings that will ever come on any line of code in your method Even if those lines are added in furuture.
So you are making your code less confident and unsafe.
So what's the remedy?

1. Resolve all warnings. (recommended)

2. Reduce the scope of @suppresswarning (if you apply that ). It should be minimum, best is to use it to jusr suppress the warnings only on one line of code On which it is applied. There you get chance to resolve warnings coming in otehr part of method or class. Never use it on class or method level unless it has just one line of code.

So to make your code completely free from risk of ClassCast Exception eliminate all unchecked warnings. Otherwise keep the scope of warning supprression to minimum

No comments:

Post a Comment