Archive

Archive for the ‘Bad Code’ Category

Sadly Singleton

September 18th, 2008 No comments

Here is some code I came across today:

      public class SingletonWannabe
      { 
        private static String SERVER_URL;
        private static int port;
        private static String protocol;
 
       public SingletonWannabe(String url, int port, String protocol) {
          this.SERVER_URL = url;
          this.port = port;
          this.protocol = protocol;
       }

       public static String getServerUrl() {
         return SERVER_URL;
       }
 
      public static int getPort() {
        return port;
      }

      public static String getProtocol() {
        return protocol;
     }
}

This was being used as a singleton in an application 🙁

Categories: Bad Code Tags:

@ThreadSafe javadoc annotation

June 30th, 2008 2 comments

Recently I came across this line of code:


private static final SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm a");

Unfortunately, this code belongs to a class whose instances will be used by multiple threads. Even though the API documentation for SimpleDateFormat mentions it being not thread safe, somehow it failed to get the developers attention.

As I got to think more about this, a @ThreadSafe JavaDoc element might be useful for API writers to convey their intent more clearly. Perhaps the generated JavaDoc for a thread safe class can use a different color or font making the distinction more visual.

Categories: Bad Code Tags:

Closing Output Stream in JSP tag

June 19th, 2008 No comments

Here is a code snippet from a custom JSP tag I ran into today:


try
{
out.write(hoursSelectHTML.toString());
out.flush();
}
catch (IOException e)
{
logger.error("Exception writing to the output stream... ", e);
}
finally
{
try
{
out.close();
}
catch (IOException e)
{
// Simply ignore this exception
}
}return SKIP_BODY;

If you are still wondering what is wrong with it, pay attention to the part where the stream is being closed 😉

Categories: Bad Code Tags: