Friday, 20 February 2009

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?

No comments:

Post a Comment

How to connect Raspberry Pi Pico to a external temperature sensor(DHT11 or DHT22)

How to connect Raspberry Pi to DHT 11 / DHT 22   Connect your DHT11 sensor to the Pico accordingly -   Left pin (Signal) - GPIO Pin 22 (or a...