Old overengineered project :)

I was looking through some old backups (~2003), and found a Java program I wrote to create thumbnails of photos for the website I was running at home. What a stupid idea that was, especially since I was running Linux at the time. There were plenty of programs that could do that conversion for me, not to mention running my own photo gallery wasn’t the brightest idea either. The worst part of the code is how over engineered it was for such a simple task. I was going to delete it, but I went ahead and put the project, PhotoResizer, up on github with all my other projects. Want to see how not cool it is? Sure you do, you need a good laugh right about now don’t you? 🙂

So let’s take a look at some of the over-engineering. First off, I wonder where I was in my coding when I did this. It was 2003, and I was a Java weanie. I lived and breathed Java and thought it was the greatest language ever. Six years later, I know for a fact that’s just plain WRONG. As a Java weanie I used to think we always needed interfaces for everything “just in case” I needed to add another implementation.

So here’s the Configuration interface 🙂 now why the hell would I want another configuration implementation? What are the odds that I’d need more than one type of config file?

public interface Configuration {
 
    String getProperty(String key);
 
    int getPropertyAsInt(String key) throws NumberFormatException;
}

If you have an interface that means you probably have a concrete implementation, yep I did:

package net.zeusville.core.config;
 
import java.util.Properties;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
 
/**
* @author jmrodri
*
* To change the template for this generated type comment go to Window -
* Preferences - Java - Code Generation - Code and Comments
*/
public class PropertyConfigImpl implements Configuration {
 
    private Properties properties;
 
    public PropertyConfigImpl(String filename)
        throws InvalidConfigurationException {
        try {
            properties = new Properties();
            FileInputStream fis = new FileInputStream(filename);
            properties.load(fis);
        }
        catch (FileNotFoundException e) {
            throw new InvalidConfigurationException("Could not find ["
                    + filename + "]", e);
        }
        catch (IOException e) {
            throw new InvalidConfigurationException("Could not read ["
                    + filename + "]", e);
        }
    }
 
    /*
* (non-Javadoc)
*
* @see net.zeusville.util.Configuration#getProperty(java.lang.String)
*/
    public String getProperty(String key) {
        return properties.getProperty(key);
    }
 
    public int getPropertyAsInt(String key) throws NumberFormatException {
        return Integer.parseInt(getProperty(key));
    }
}

LOL! That is certainly a lot of code to read in a .properties file. It gets better 🙂 Because we all know that every interface and concrete implementation needs a factory or in my case a Manager.

public class ConfigurationManager {
 
    private static Configuration configuration;
 
    public static Configuration getConfiguration() {
        if (configuration == null)
            configuration = new PropertyConfigImpl(
                    "config/PhotoResizer.properties");
 
        return configuration;
    }
 
}

Wow, that’s just sad. I can’t believe I actually wrote this stuff. I’m not done yet 🙂 Like Billy Mays would say ‘… but that’s not all, there’s MORE!’

How much more could I have done to over-engineer this solution? I mean I’m just wanting to create thumbnails of a directory of pictures. Just read the list of files, and for each one convert to a configurable thumbnail size, write it out. Done right? no way José I went all out. No Java program is acceptable without THREADS! Don’t bother getting off the floor, because what I’m about to show you next will make you laugh your @$$ off.

Simple threads weren’t good enough for this high powered solution 🙂 I went all out and created a queue of work items with a pool of worker threads doing the conversion. No really, that’s what I did. Check out the InterThreadQueue.

Here’s the run() method from the ImageTransformer which looks to be the guts of PhotoResizer.

10 thoughts on “Old overengineered project :)

  1. That’s…so beautiful. It looks exactly like all other Java code ever written. More method and class definitions than lines of code. 🙂

  2. I hope it is using javax.foo.SomeHorriblyComplexImageLibrary versus find and and an “-exec” call to ImageMagick’s “convert”, otherwise I must subtract points.

  3. #!/usr/bin/env python
    import Image
    import os
    SRC_DIR=’/home/jmrodri/data/graphics/in’
    DEST_DIR=’/home/jmrodri/data/graphics/out’
    WIDTH=64
    HEIGHT=64

    def thumbnail(src, dest, width, height, format = “PNG”):
    im = Image.open(src)
    im.thumbnail((width, height))
    im.save(dest, format)

    for f in os.listdir(SRC_DIR):
    thumbnail (SRC_DIR + “/” + f, DEST_DIR + “/” + f, WIDTH, HEIGHT)

    1. Ugh didn’t keep the indents while formatiing, so using ‘->’ for space.. You need to install Python Imaging Library :).. yum install python-imaging-devel

      #!/usr/bin/env python
      import Image
      import os
      SRC_DIR=’/home/jmrodri/data/graphics/in’
      DEST_DIR=’/home/jmrodri/data/graphics/out’
      WIDTH=64
      HEIGHT=64

      def thumbnail(src, dest, width, height, format = “PNG”):
      -> im = Image.open(src)
      -> im.thumbnail((width, height))
      -> im.save(dest, format)

      for f in os.listdir(SRC_DIR):
      -> thumbnail (SRC_DIR + “/” + f, DEST_DIR + “/” + f, WIDTH, HEIGHT)

    1. bash rocks even more:

      SRC_DIR=’/home/jmrodri/data/graphics/in’
      DEST_DIR=’/home/jmrodri/data/graphics/out’
      WIDTH=64
      HEIGHT=64
      for img in $ SRC_DIR/*.png; do
      $OUTPUT=`basename $img`
      convert $img -resize ${WIDTH}x$HEIGHT $OUTPUT
      done

      Anyway, the story is great. You should send it to http://thedailywtf.com/

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s