Bad Java code with good intentions

My friend Deepak sent me a real-life example of the following Java code. Let's give the developer the credit of following defensive coding techniques. [Rather than calling him a dumb-a$$]



String flashPath = (String)request.getAttribute("flashPath");
String flashPathPath = flashPath.toString();




Sathya improved on it by adding checks


String flashPath = (String)request.getAttribute("flashPath");
If (flashPath instance of java.lang.String){
String flashPathPath = flashPath.toString();
}




So let's work on this some more



String flashPath = (String)request.getAttribute("flashPath");
If (flashPath instance of java.lang.String){
try{
String flashPathPath = (String) flashPath.toString(); //Let's cast it once more to be sure, if it escaped the first time, it'll surely get caught here.
}
catch (ClassCastException e)
{
// Don’t cast it
String flashPathPath = flashPath.toString();
}
}



Now this is open to all to further obfuscate.
How complicated can you make one line of code?

Comments

Popular Posts