<?xml version="1.0"?>
<rss version="2.0">
  <channel>
    <title>blog.onlysimpler.com</title>
    <link>http://blog.onlysimpler.com</link>
    <description>A blog... only simpler</description>
    <copyright>Copyright 2005-2007 Shane Bell</copyright>
    <item>
      <title>Slowing Down AJAX With AOP</title>
      <link>http://blog.onlysimpler.com/entries/slowing_down_ajax_with_aop.html</link>
      <description>&lt;p&gt;
  A web application I'm working on at the moment uses quite a lot of AJAX to make it more interactive and to add a 
  little eye candy to the UI. A common feature of the application is to show a progress indicator when a page is 
  waiting for a response from the server. It's a small rotating graphic that provides some visual feedback to the 
  user so they know something is happening. A good example of this is Gmail's red &quot;Loading...&quot; message.
&lt;/p&gt;
&lt;img src=&quot;/media/gmail_loading.png&quot; alt=&quot;Gmail's loading message&quot;/&gt;
&lt;p&gt;
  But one problem I kept running into during development was the fact that the server was responding too quickly. Because 
  I was running the server locally on my development machine, the browser was receiving a response from the server 
  almost immediately. There was barely any lag.
&lt;/p&gt;
&lt;p&gt;
  While this is usually a good thing, in my case it was proving to be a major pain. Before the page had time to show 
  the progress indicator the server had already returned a response. This made it very difficult for me to test if 
  the AJAX code for the progress indicator was working correctly. Also, I was unable to see the application as 
  it would appear in the real world - with some lag.
&lt;/p&gt;
&lt;p&gt;
  My first reaction was to modify the server code to include something like this:
&lt;/p&gt;
&lt;pre&gt;
// take a nap for 1.5 seconds
try {
    Thread.sleep(1500);
catch (InterruptedException e) {
    // ignore any exceptions
}&lt;/pre&gt;
&lt;p&gt;
  But I soon got sick of adding that to every method I was trying to test. And worse than that, I kept committing this
  &quot;temporary&quot; code to version control by mistake! I needed a way to slow things down without having to constantly modify
  code.
&lt;/p&gt;
&lt;p&gt;
  &lt;a href=&quot;http://en.wikipedia.org/wiki/Aspect-oriented_programming&quot;&gt;Aspect orient programming&lt;/a&gt; was the answer. 
  I've become a big fan of AOP over the last year or so. It's really come into the mainstream recently, mainly due
  to it's extensive usage in &lt;a href=&quot;http://www.springframework.org&quot;&gt;Spring&lt;/a&gt; and to a lesser extent 
  &lt;a href=&quot;http://jcp.org/en/jsr/detail?id=220&quot;&gt;EJB 3&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
  My project uses Spring which has some &lt;a href=&quot;http://www.springframework.org/docs/reference/aop.html&quot;&gt;pretty 
  comprehensive AOP support&lt;/a&gt; right out of the box so it didn't take long to knock together a solution to my problem.
&lt;/p&gt;
&lt;p&gt;
  First of all I created an advice that would sleep for a configurable period of time.
&lt;/p&gt;
&lt;pre&gt;
import org.aspectj.lang.ProceedingJoinPoint;

public class DelayAdvice {

    private long delayPeriod;

    public DelayAdvice() {
    }

    public void setDelayPeriod(long delayPeriod) {
        this.delayPeriod = delayPeriod;
    }

    public Object delay(ProceedingJoinPoint pjp) throws Throwable {
        try {
            Thread.sleep(delayPeriod);
        } catch (InterruptedException e) {
            // ignore
        }
        return pjp.proceed();
    }
}
&lt;/pre&gt;
&lt;p&gt;
  Then it was simply a matter of configuring Spring with a pointcut on the services I wanted to slow down.
&lt;/p&gt;
&lt;pre&gt;
&amp;lt;aop:config&amp;gt;
    &amp;lt;aop:pointcut id=&quot;serviceLayerPointcut&quot; expression=&quot;execution(public * com.onlysimpler.SomeService.*(..))&quot;/&amp;gt;
    &amp;lt;aop:aspect ref=&quot;delayAdvice&quot;&amp;gt;
        &amp;lt;aop:around pointcut-ref=&quot;serviceLayerPointcut&quot; method=&quot;delay&quot;/&amp;gt;
    &amp;lt;/aop:aspect&amp;gt;
&amp;lt;/aop:config&amp;gt;

&amp;lt;bean id=&quot;delayAdvice&quot; class=&quot;com.onlysimpler.DelayAdvice&quot;&amp;gt;
    &amp;lt;property name=&quot;delayPeriod&quot; value=&quot;2000&quot;/&amp;gt;
&amp;lt;/bean&amp;gt;
&lt;/pre&gt;
&lt;p&gt;
  Simple but effective.
&lt;/p&gt;</description>
      <author>Shane Bell</author>
      <pubDate>Thu, 30 Aug 2007 19:50:23 +0000</pubDate>
      <guid isPermaLink="true">http://blog.onlysimpler.com/entries/slowing_down_ajax_with_aop.html</guid>
    </item>
    <item>
      <title>Be Careful With DateFormat</title>
      <link>http://blog.onlysimpler.com/entries/be_careful_with_dateformat.html</link>
      <description>&lt;p&gt;
  I think the best way to improve as a developer is to accept the fact that every line of code you've ever
  written could have been a whole lot better. Nobody writes perfect code, and the sooner you accept that,
  the sooner you'll start learning new ways you can improve.
&lt;/p&gt;
&lt;p&gt;
  Yesterday this &lt;a href=&quot;http://blogs.atlassian.com/developer/2007/07/dateformat_objects_and_threads.html&quot;&gt;
  blog post&lt;/a&gt; from the boys at &lt;a href=&quot;http://www.atlassian.com&quot;&gt;Atlassian&lt;/a&gt; highlighted a little gem
  that I realised I've been guilty of on more than a few occasions. 
&lt;/p&gt;
&lt;p&gt;
  It turns out that &lt;a href=&quot;http://java.sun.com/javase/6/docs/api/java/text/DateFormat.html&quot;&gt;java.text.DateFormat&lt;/a&gt;
  isn't thread safe. Who would have guessed? Well, besides the fact that there's a huge disclaimer at the
  bottom of the Javadoc, who would have guessed? RTFM next time I suppose.
&lt;/p&gt;
&lt;p&gt;
  Anyway, the Atlassian blog post pretty much sums it up - DateFormat isn't thread safe, and more importantly,
  the most common symptom from thread contention is output that is perfectly valid, but also completely wrong!
  Chances are you'll get a nicely formatted date, it just wont be the date you were expecting. Needless to say,
  that's bad. 
&lt;/p&gt;
&lt;p&gt;
  I wonder how many hair-pulling, late nights have been spent by developers around the world trying to figure
  that one out.
&lt;/p&gt;
&lt;p&gt;
  I was curious to see what the results would be if two threads fought over a single DateFormat so I put 
  together some code to test it out.
&lt;/p&gt;
&lt;pre&gt;
public class TestDateFormat {

    public static void main(String[] args) {

        // a date format that two threads will contend for
        final DateFormat dateFormat = new SimpleDateFormat(&amp;quot;dd/MM/yyyy&amp;quot;);

        class DatePrinter extends Thread {

            private String dateString;

            public DatePrinter(String dateString) {
                this.dateString = dateString;
            }

            public void run() {
                while (true) {
                    try {
                        Date parsedDate = dateFormat.parse(dateString);
                        String parsedDateString = dateFormat.format(parsedDate);
                        if (! dateString.equals(parsedDateString)) {
                            System.err.println(dateString + &amp;quot; != &amp;quot; + parsedDateString);
                        }
                    } catch (Exception e) {
                        System.err.println(e);
                    }
                }
            }
        }        

        new DatePrinter(&amp;quot;01/08/2007&amp;quot;).start();
        new DatePrinter(&amp;quot;14/03/1978&amp;quot;).start();
    }
}
&lt;/pre&gt;
&lt;p&gt;
  The code creates a single DateFormat instance and two threads that both attempt to use it simultaneously.
  The threads are constructed with a string representation of a date. Each thread simply loops around
  indefinitely, converting its date string into a date object, and then converting that date object back into
  a string again. If all goes well then the original string should equal the converted one. If not, then it
  prints a message to the console.
&lt;/p&gt;
&lt;p&gt;
  Running the program produces a flurry of output as the threads argue over who's turn it is to use the
  DateFormat. Here's a snippet of some of the output it produced when I ran it:
&lt;/p&gt;
&lt;pre&gt;
14/03/1978 != 12/01/1982
14/03/1978 != 14/03/1414
14/03/1978 != 01/08/1978
14/03/1978 != 01/08/2007
01/08/2007 != 01/08/20072007
14/03/1978 != 13/06/56933
01/08/2007 != 15/07/19781984
01/08/2007 != 17/10/56168
01/08/2007 != 01/08/0001
01/08/2007 != 01/08/0000
14/03/1978 != 28/08/1983
&lt;/pre&gt;
&lt;p&gt;
  As well as the occasional (and very strange) exception:
&lt;/p&gt;
&lt;pre&gt;
java.lang.NumberFormatException: For input string: &amp;quot;.1401E.14012E2&amp;quot;
java.lang.ArrayIndexOutOfBoundsException: -343
&lt;/pre&gt;
&lt;p&gt;
  As you can see, many of the dates that are produced are nicely formatted, but very, very wrong. The Atlassian post
  suggests a number of good solutions for fixing things to ensure you get the results you expect.
&lt;/p&gt;
&lt;p&gt;
  I have to admit, I'm guilty of using DateFormat in the past with absolutely no regard for thread safety whatsoever.
  I wonder how many nasty defects I've left in my wake as a result. My apologies to any past colleagues who ended up 
  stuck in the office until 3am fixing a DateFormat related bug I left behind :)
&lt;/p&gt;
&lt;p&gt;
  But at least now I've learned something to make my code just that little bit better next time.
&lt;/p&gt;</description>
      <author>Shane Bell</author>
      <pubDate>Wed, 01 Aug 2007 23:35:23 +0000</pubDate>
      <guid isPermaLink="true">http://blog.onlysimpler.com/entries/be_careful_with_dateformat.html</guid>
    </item>
    <item>
      <title>Watch your ABCs</title>
      <link>http://blog.onlysimpler.com/entries/watch_your_abcs.html</link>
      <description>&lt;p&gt;
  Through a process that can only be described as blatant generalisation, I've reached the following conclusion...
&lt;/p&gt;
&lt;p&gt;
  There is a direct correlation between bad spellers and bad developers (or is it the other way around?).
&lt;/p&gt;
&lt;p&gt;
  It seems that wherever I go I come across code that's riddled with spelling mistakes and typos. I'm not
  talking about comments (that would be bearable), I'm talking about important stuff - class names, methods,
  variables, config files etc.
&lt;/p&gt;
&lt;p&gt;
  Now I know what you're thinking. &quot;Get over Shane, you whining bastard, what's the big deal about a typo&quot;. 
  Well if you just calm down for a minute, I'll tell you what's wrong with them.
&lt;/p&gt;
&lt;p&gt;
  First of all, they waste people's time. The other day I spent about 10 minutes chasing down a problem that
  was all due to a stupid typo. Someone had mistyped the name of a class so it was almost what you'd expect
  it to be, but not quite. Instead of &quot;LogoutController&quot; it was called &quot;LogoutContoller&quot;. This meant that a
  separate config file that was referencing the name of the class didn't quite align with the actual (and
  incorrect) name and hence the whole thing didn't work.
&lt;/p&gt;
&lt;p&gt;
  Sure, it only took 10 minutes to track down the problem. But because of all the fuss, by the time I fixed it
  I'd completely forgotten what I was originally trying to do. It took another 20 minutes for me to get back
  into the &lt;a href=&quot;http://www.joelonsoftware.com/articles/fog0000000339.html&quot;&gt;flow&lt;/a&gt;. What could have taken
  the original developer 30 seconds to fix ended up wasting half an hour of my day.
&lt;/p&gt;
&lt;p&gt;
  But the real problem lies deeper than that.
&lt;/p&gt;
&lt;p&gt;
  I can forgive the occasional spelling mistake. We're only human and people make mistakes. But some developers
  have a real knack for it and seem to do it all the time. It's not that they can't spell, or have difficulty
  typing. It's much simpler and more fundamental than that. Usually it boils down to one of two things - they're
  either too lazy to fix their own mistakes, or too stupid to notice they've made one. Either way, that's bad.
&lt;/p&gt;
&lt;p&gt;
  That's the real issue. If someone is too lazy to fix something as fundamental as a spelling mistake, what 
  hope do you have of them fixing something that's actually important? And if they can't even see a spelling 
  mistake then the chances of them spotting a real problem in the code are slim to none.
&lt;/p&gt;
&lt;p&gt;
  The lazy ones know there's a problem, and sometimes, for a brief moment, they even consider fixing it. But 
  instead, they prefer to pretend they didn't see it and move on. As for the stupid ones, well they're even
  worse.
&lt;/p&gt;
&lt;p&gt;
  Bad spelling is a warning sign of two personality traits you really don't want in a developer - laziness and
  lack of attention to detail. Computers are precise. Code either works or it doesn't, and unfortunately, close
  enough isn't good enough when it comes to programming. You want to work with developers who get things right,
  not the ones who almost get things right, but only sometimes, and only if they can be bothered.
&lt;/p&gt;
&lt;p&gt;
  My wife says I'm too quick to judge, and perhaps she's right. How can I make such a blatant generalisation
  based on something so minor. Well, besides the fact this is my blog and I can say whatever I damn well please,
  there's something else...
&lt;/p&gt;
&lt;p&gt;
  The funny thing about generalisations is this - they're generally true.
&lt;/p&gt;
&lt;p&gt;
  So I'm sticking to my guns. As far as I'm concerned, bad speller = bad developer.
&lt;/p&gt;</description>
      <author>Shane Bell</author>
      <pubDate>Tue, 31 Jul 2007 22:40:21 +0000</pubDate>
      <guid isPermaLink="true">http://blog.onlysimpler.com/entries/watch_your_abcs.html</guid>
    </item>
    <item>
      <title>There is Always a Way</title>
      <link>http://blog.onlysimpler.com/entries/there_is_always_a_way.html</link>
      <description>&lt;p&gt;
  The office where I'm currently working uses &lt;a href=&quot;http://www.mcafee.com&quot;&gt;McAfee&lt;/a&gt; security products.
  Someone, somewhere in the organisation seems to believe that these will protect us from evil. Anyway,
  one of these products is &lt;a href=&quot;http://www.mcafee.com/us/enterprise/products/anti_virus/file_servers_desktops/virusscan_enterprise_80i.html&quot;&gt;
  McAfee VirusScan&lt;/a&gt; - and it's &lt;a href=&quot;http://blogs.msdn.com/ericlippert/archive/2004/03/10/87384.aspx&quot;&gt;
  driving me nuts&lt;/a&gt;.  It has a painfully annoying feature called On-Access Scan that basically scans every
  single file you access just in case it's a virus. Every single file! Needless to say, throughout the course
  of a day this can be a lot of files. Now it wouldn't be so bad if it was quick about it, but it takes
  forever! And sadly, due to the way it's been installed, there's no in between - it either scans every file,
  or none at all.
&lt;/p&gt;
&lt;p&gt;
  As you can imagine, this gets pretty annoying real fast. Something as simple as compiling some code ends up
  taking 5 times as long while McAfee checks your code for viruses. It's like one of those friends that everyone
  had at high school who was always hanging around even though nobody liked him. No matter where you went, he
  always seemed to show up.
&lt;/p&gt;
&lt;p&gt;
  Fortunately I'm lucky enough to have administrator privileges on my PC so I was able to get rid of the 
  annoying friend whenever I pleased. Occasionally he'd show up again after a reboot, but I could always get
  rid of him with a few swift kicks, I mean clicks.
&lt;/p&gt;
&lt;p&gt;
  Now you might think that turning off the virus scanner isn't the smartest move, but our internet access is
  so restricted in the office (thanks to our big brother proxy) that there's simply no chance of me finding
  a site that might have something sinister to offer. As for contracting a virus via email - well we use
  Lotus Notes and I don't think hackers can be bothered writing viruses for Notes!
&lt;/p&gt;
&lt;p&gt;
  Anyway, back to the annoying friend. So things were under control, I could get rid of the virus scanner
  any time I liked. But yesterday things changed....
&lt;/p&gt;
&lt;p&gt;
  When I arrived at work yesterday, my friend was back, snooping around my files. Weird. I hadn't rebooted
  recently, so why was he back? I didn't give it much thought at the time, I just disabled the scanner and
  got on with my day. But 20 minutes later he was back again. Hmmm.
&lt;/p&gt;
&lt;p&gt;
  I discovered that over night the system administrators had installed another annoying McAfee product - 
  &lt;a href=&quot;http://www.mcafee.com/us/enterprise/products/system_security_management/epolicy_orchestrator.html&quot;&gt;
  ePolicy Orchestrator&lt;/a&gt;. This tool lets you create a security policy that can be enforced over the network.
  Every 30 minutes, my PC was checking back with the policy server to make sure it was running everything it
  should be. Unfortunately, it noticed the virus scanner wasn't running and started it up again.
&lt;/p&gt;
&lt;p&gt;
  Bugger. The my friend was back and this time he wouldn't go away. Time for a new plan.
&lt;/p&gt;
&lt;p&gt;
  I could have tried un-installing the virus scanner, or the even whole suite of McAfee products. But I
  figured that probably wouldn't go down too well with the IT guys. So instead I wrote this little Ruby
  script that launches when I log in.
&lt;/p&gt;
&lt;pre&gt;
trap('INT') { exit }

while (true) do
  system 'net stop mcshield &amp;gt; NUL 2&amp;gt;&amp;amp;1'
  sleep 60
end
&lt;/pre&gt;
&lt;p&gt;
  For now that seems to keep him away. I wonder what the sys admins will try next :)  
&lt;/p&gt;</description>
      <author>Shane Bell</author>
      <pubDate>Thu, 26 Jul 2007 19:45:13 +0000</pubDate>
      <guid isPermaLink="true">http://blog.onlysimpler.com/entries/there_is_always_a_way.html</guid>
    </item>
    <item>
      <title>Excellent Interview Question (Part 2)</title>
      <link>http://blog.onlysimpler.com/entries/excellent_interview_question_part_2.html</link>
      <description>&lt;p&gt;
  Just a quick follow up to my &lt;a href=&quot;/entries/excellent_interview_question.html&quot;&gt;
  previous article&lt;/a&gt; on technical interview questions. It proved to be quite a popular post
  and I received lots of feedback and alternate solutions to the maze problem. Needless
  to say, many of the solutions were far superior to mine :)
&lt;/p&gt;
&lt;p&gt;
  Here are a couple of my favourites.
&lt;/p&gt;
&lt;p&gt;
  The first is from &lt;a href=&quot;http://letterblock.com/&quot;&gt;Joshua Paine&lt;/a&gt;. Joshua's solution
  is written in Javascript and wins the award for the simplest and most elegant solution.
&lt;/p&gt;
&lt;pre&gt;
var maze = [
      1, 0, 0, 0, 0,
      1, 0, 1, 1, 1,
      1, 0, 1, 0, 1,
      1, 0, 1, 0, 1,
      1, 1, 1, 0, 1 ];

function solvable(maze,w,h,x,y) {
  x || (x=0);
  y || (y=0);
  var i = y*w+x;
  if(x&amp;lt;0 || y&amp;lt;0 || x&amp;gt;=w || y&amp;gt;=h || !maze[i]) return false;
  if(x==(w-1) &amp;amp;&amp;amp; y==(h-1)) return true;
  maze[i] = 0;
  return (solvable(maze,w,h,x,y+1) ||
          solvable(maze,w,h,x+1,y) ||
          solvable(maze,w,h,x,y-1) ||
          solvable(maze,w,h,x-1,y));
}

solvable(maze,5,5);

&lt;/pre&gt;
&lt;p&gt;
  The second is from Aleksey Eschenko who wrote his solution in Ruby. Again, this
  solution is simple and to the point.
&lt;/p&gt;
&lt;pre&gt;
class Maze

  def initialize(array, width, heigth)
      @array, @width, @heigth = array, width, heigth
  end

  def routes
    calculate_routes([ [ [0, 0] ] ])
  end

  def [](x, y)
    if ( x &amp;gt;= @width || y &amp;gt;= @heigth || x &amp;lt; 0 || y &amp;lt; 0 ) then -1 else @array[y * @width + x] end
  end

  private

  def possible_steps(route)
    x, y = route.last
    [[x + 1, y], [x - 1, y], [x, y + 1], [x, y - 1]].select { |xy| !route.include?(xy) &amp;amp;&amp;amp; self[*xy] == 1 }
  end

  def finished?(route)
    route.last == [@width - 1, @heigth - 1]
  end

  def calculate_routes(routes)
    return routes if routes.empty? || routes.select { |route| finished?(route) }.size == routes.size
    resultset = []
    routes.each do |route|
      if finished?(route)
        resultset &amp;lt;&amp;lt; route
      else
        possible_steps(route).each { |xy| resultset &amp;lt;&amp;lt; (route + [xy]) }
      end
    end
    calculate_routes(resultset)
  end

end

maze = Maze.new([ 1, 0, 1, 0,
                  1, 1, 1, 0,
                  1, 0, 1, 1,
                  1, 0, 0, 1], 4, 4)

puts !maze.routes.empty?

&lt;/pre&gt;
</description>
      <author>Shane Bell</author>
      <pubDate>Mon, 18 Jun 2007 23:07:00 +0000</pubDate>
      <guid isPermaLink="true">http://blog.onlysimpler.com/entries/excellent_interview_question_part_2.html</guid>
    </item>
    <item>
      <title>Excellent Interview Question</title>
      <link>http://blog.onlysimpler.com/entries/excellent_interview_question.html</link>
      <description>&lt;p&gt;
  I've always been a big believer in technical interviews that really test a candidate's knowledge.
  I don't believe you can get a good understanding of someone's technical abilities any other way.
&lt;/p&gt;
&lt;p&gt;
  Recently I had just such an interview where I was asked a fantastic technical question. Actually,
  it wasn't an interview as such, but a test. I went in, sat a written test and went home. I didn't
  even have the chance to speak to anyone until I had proven my technical ability on their test.
&lt;/p&gt;
&lt;p&gt;
  Personally I think that's a little harsh, but I guess it's effective. Why waste time interviewing
  someone unless you're sure they have the skills to do the job?
&lt;/p&gt;
&lt;p&gt;
  In fact, I've been in that situation before. I was interviewing a candidate who had a fantastic CV,
  but 10 minutes into the interview he'd already said enough stupid things to convince my colleague and
  I that he was clueless. But despite that, we still had to continue with the interview knowing full well
  that it was a complete waste of everyone's time.
&lt;/p&gt;
&lt;p&gt;
  Anyway, back to the interview question...
&lt;/p&gt;
&lt;p&gt;
  The task was to write a program (on paper) to solve a maze. Given a representation of a maze, the
  program simply had to return a boolean value indicating whether the maze could be solved.
&lt;/p&gt;
&lt;p&gt;
  Consider the following diagram. The starting point of the maze is the top left-hand corner and the goal
  is to travel through the maze, square by square, to the bottom right-hand corner. You can only move to
  squares that contain a 1, and you can only move up, down, left and right (diagonal is not allowed). 
&lt;/p&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;5&quot;&gt;
  &lt;tr&gt;
    &lt;td style=&quot;background-color: #EEEEEE;&quot;&gt;1&lt;/td&gt;
    &lt;td&gt;0&lt;/td&gt;
    &lt;td&gt;0&lt;/td&gt;
    &lt;td&gt;0&lt;/td&gt;
    &lt;td&gt;0&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td style=&quot;background-color: #EEEEEE;&quot;&gt;1&lt;/td&gt;
    &lt;td style=&quot;background-color: #EEEEEE;&quot;&gt;1&lt;/td&gt;
    &lt;td&gt;0&lt;/td&gt;
    &lt;td&gt;0&lt;/td&gt;
    &lt;td&gt;0&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;0&lt;/td&gt;
    &lt;td style=&quot;background-color: #EEEEEE;&quot;&gt;1&lt;/td&gt;
    &lt;td style=&quot;background-color: #EEEEEE;&quot;&gt;1&lt;/td&gt;
    &lt;td&gt;0&lt;/td&gt;
    &lt;td&gt;0&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;0&lt;/td&gt;
    &lt;td&gt;0&lt;/td&gt;
    &lt;td style=&quot;background-color: #EEEEEE;&quot;&gt;1&lt;/td&gt;
    &lt;td&gt;0&lt;/td&gt;
    &lt;td&gt;0&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;0&lt;/td&gt;
    &lt;td&gt;0&lt;/td&gt;
    &lt;td style=&quot;background-color: #EEEEEE;&quot;&gt;1&lt;/td&gt;
    &lt;td style=&quot;background-color: #EEEEEE;&quot;&gt;1&lt;/td&gt;
    &lt;td style=&quot;background-color: #EEEEEE;&quot;&gt;1&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;
  The highlighted path in the maze above is the valid path through the maze.
&lt;/p&gt;
&lt;p&gt;
  Note that a maze would typically have a number of dead ends making it more difficult to solve, and could
  potentially have more than one valid path to the finish. A more typical maze is shown below.
&lt;/p&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;5&quot;&gt;
  &lt;tr&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;0&lt;/td&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;1&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;0&lt;/td&gt;
    &lt;td&gt;0&lt;/td&gt;
    &lt;td&gt;0&lt;/td&gt;
    &lt;td&gt;1&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;1&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;0&lt;/td&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;0&lt;/td&gt;
    &lt;td&gt;1&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;0&lt;/td&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;0&lt;/td&gt;
    &lt;td&gt;1&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;
  From the program's point of view, the maze was represented as an array of integers, as well as the width
  and height of the maze. For example:
&lt;/p&gt;
&lt;pre&gt;
int[] maze = { 1, 0, 0, 1, 1, 0, 1, 1, 1 };
int width = 3;
int height = 3;&lt;/pre&gt;
&lt;p&gt;
  Represents the following maze:
&lt;/p&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;5&quot;&gt;
  &lt;tr&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;0&lt;/td&gt;
    &lt;td&gt;0&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;0&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;1&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;
  Assuming a basic x, y coordinate system with (0, 0) being the top-left hand corner and (2, 2) being the bottom
  right-hand corner, finding the value at a given position in the maze can be determined as follows:
&lt;/p&gt;
&lt;pre&gt;
int value = maze[(y*width)+x];&lt;/pre&gt;
&lt;p&gt;
  So that's it! You have a pen and paper, and one hour - go write a program that accepts those parameters and
  determines whether the maze is solvable.
&lt;/p&gt;
&lt;p&gt;
  Believe me, with only a pen and paper this is HARD!
&lt;/p&gt;
&lt;p&gt;
  &lt;strong&gt;NOTE:&lt;/strong&gt; If you want to have a go at this yourself then I suggest you stop reading now and come back when you're done.
&lt;/p&gt;
&lt;p&gt;
  Here's the solution I came up with. I've cleaned things up a little, but the program is essentially the same as
  the one I frantically scrawled on paper in my interview. My solution had to be written in Java.
&lt;/p&gt;
&lt;p&gt;
  First of all, here's the basic algorithm I came up with.
&lt;/p&gt;
&lt;p&gt;
  &quot;Starting at position (0,0) in the maze, find all adjacent squares containing a 1 that have not already been moved to.
  For each of these square, attempt to recursively solve the maze from that position.&quot;
&lt;/p&gt;
&lt;p&gt;
  My first class was a simple utility class to represent a coordinate within the maze. Nothing too exciting here:
&lt;/p&gt;
&lt;pre&gt;
public class Coordinate {

    private int x;
    private int y;

    public Coordinate(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public boolean equals(Object other) {

        if (!(other instanceof Coordinate)) {
            return false;
        }

        Coordinate c = (Coordinate) other;
        return (c.x == this.x &amp;amp;&amp;amp; c.y == this.y);
    }
}&lt;/pre&gt;
&lt;p&gt;
  Next up was the class to actually solve the maze. The comments pretty much explain everything.
&lt;/p&gt;
&lt;pre&gt;
public class Solver {

    // the details of the maze
    private int[] maze;
    private int width;
    private int height;

    // a list of visited coordinates
    private List&amp;lt;Coordinate&amp;gt; visited = new ArrayList&amp;lt;Coordinate&amp;gt;();

    /**
     * Create a new maze solver.
     * 
     * @param maze the maze to be solved.
     * @param width the width of the maze.
     * @param height the height of the maze.
     */
    public Solver(int[] maze, int width, int height) {

        // is the maze valid?
        if (maze.length != (width * height)) {
            throw new IllegalArgumentException(&quot;Illegal maze arguments.&quot;);
        }

        this.maze = maze;
        this.width = width;
        this.height = height;
    }

    /**
     * Determine whether the maze can be solved.
     * 
     * @return a boolean indicating whether the maze can be solved.
     */
    public boolean canSolve() {

        // start solving recursively from the start position
        return canSolve(new Coordinate(0, 0));
    }

    /**
     * Given a position, determine whether the maze can be solved from there.
     * 
     * @param position the current position in the maze.
     * @return a boolean indicating whether the maze can be solved.
     */
    private boolean canSolve(Coordinate position) {

        visited.add(position);

        // have we reached the end of the maze?
        if ((position.getX() == width-1) &amp;amp;&amp;amp; (position.getY() == height-1)) {
            return true;
        }

        // recurse for each adjacent coordinate
        List&amp;lt;Coordinate&amp;gt; adjacent = getAdjacent(position);
        for (Coordinate coordinate : adjacent) {
            if(canSolve(coordinate)) {
                return true;
            }
        }

        // if we make it to here then the maze is unsolvable :(
        return false;
    }

    /**
     * Get a list of all adjacent coordinates in the maze that can be moved to
     * from the given coordinate. An adjacent coordinate can only be moved to
     * if it contains a '1' and if it is above, below, left or right of the
     * given coordinate and hasn't yet been visited yet.
     * 
     * @param position the position in the maze.
     * @return a list of adjacent coordinates that can be moved to.
     */
    private List&amp;lt;Coordinate&amp;gt; getAdjacent(Coordinate position) {

        List&amp;lt;Coordinate&amp;gt; adjacent = new ArrayList&amp;lt;Coordinate&amp;gt;();

        int x = position.getX();
        int y = position.getY();

        // create a list of potential adjacent coordinates
        Coordinate[] potentialCoordinates = {
            new Coordinate(x-1, y), // left
            new Coordinate(x+1, y), // right
            new Coordinate(x, y-1), // above
            new Coordinate(x, y+1)  // below
        };

        // only return unvisited adjacent coordinates containing a '1'
        for (int i = 0; i &amp;lt; potentialCoordinates.length; i++) {
            Coordinate c = potentialCoordinates[i];
            if (!visited.contains(c) &amp;amp;&amp;amp; getValueAt(c) == 1) {
                adjacent.add(c);
            }
        }
        return adjacent;
    }

    /**
     * Get the value at the given coordinate in the maze.
     * 
     * @param c the cordinate to get the value at.
     * @return the value at the given position, or -1 if the position is invalid.
     */
    private int getValueAt(Coordinate c) {

        int x = c.getX();
        int y = c.getY();

        // only return the value from the maze if the given position is valid
        if (x &amp;gt;= 0 &amp;amp;&amp;amp; y &amp;gt;= 0 &amp;amp;&amp;amp; x &amp;lt; width &amp;amp;&amp;amp; y &amp;lt; height) {
            return maze[(y*width)+x];
        } else {
            return -1;
        }
    }
}&lt;/pre&gt;
&lt;p&gt;
  And that's it really. Calling it is pretty straight forward.
&lt;/p&gt;
&lt;pre&gt;
int[] maze = { 
        1, 0, 0, 0, 0,
        1, 0, 1, 1, 1,
        1, 0, 1, 0, 1,
        1, 0, 1, 0, 1,
        1, 1, 1, 0, 1
}; 

Solver solver = new Solver(maze, 5, 5);
System.out.println(solver.canSolve());&lt;/pre&gt;
&lt;p&gt;
  I'm sure there's a much more elegant way to solve this using some fancy matrix mathematics,
  but I was pretty happy with this solution. The complete source code is available 
  &lt;a href=&quot;/downloads/mazesolver.jar&quot;&gt;here&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
  Send me an email if you have a better solution.
&lt;/p&gt;
</description>
      <author>Shane Bell</author>
      <pubDate>Sat, 09 Jun 2007 12:19:00 +0000</pubDate>
      <guid isPermaLink="true">http://blog.onlysimpler.com/entries/excellent_interview_question.html</guid>
    </item>
    <item>
      <title>Readable Code</title>
      <link>http://blog.onlysimpler.com/entries/readable_code.html</link>
      <description>&lt;p&gt;
  Why do so many developer's insist on writing horribly unreadable code? I think it's because they're
  just plain lazy!
&lt;/p&gt;
&lt;p&gt;
  Ok, perhaps that's a little harsh. Maybe it's more correct to say that most developers write unreadable, messy 
  code because they don't see the point in doing any different.
&lt;/p&gt;
&lt;p&gt;
  &quot;It's only going to be read by a computer right? And a computer can read code no matter how messy it
  is so why bother?&quot;. Sure, a computer might be able to read it, but people wont!
&lt;/p&gt;
&lt;p&gt;
  One of my favorite quotes from &lt;a href=&quot;http://mitpress.mit.edu/sicp/full-text/book/book.html&quot;&gt;this book&lt;/a&gt; is -
  &quot;programs must be written for people to read, and only incidentally for machines to execute&quot;. I couldn't agree more.
&lt;/p&gt;
&lt;p&gt;
  How many times have you written code that worked first time and never had to be changed again? Probably not
  very often. The bulk of a piece of code is typically written in one sitting. But it's re-visited over and over
  again. Every time a defect is found or an improvement is made, some poor sucker has to read the code and figure
  out what it's supposed to do. And here's the thing... at some point that sucker is going to be you!
&lt;/p&gt;
&lt;p&gt;
  Even if you were the original author, chances are you're not going to remember what it does or how it does it.
  So that leaves you with one option - read it and figure it out.
&lt;/p&gt;
&lt;p&gt;
  Here's where things get interesting. If the author bothered to take the time to do things right and write clean, clear,
  commented code then this can be a simple and pleasant experience. However, if he just cobbled it together because he
  had to go home and was running late for his train, it can be far from pleasant. What should have taken you a couple of
  minutes to read and understand can end up wasting an entire afternoon and do terrible things to your blood pressure in
  the process.
&lt;/p&gt;
&lt;p&gt;
  So what do I mean when I say &quot;readable&quot; code? I don't want to get into religious debates on tabs vs spaces or
  whether to put braces at the end of a line or on the next. That's really not that important. What is important
  is to write code that is easy for others to read and understand. Here are a few things to consider...
&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;
    Don't try to be clever and make things more complicated than they need to be. Keep it simple and to the point.
    You might think it's cool to use a bitwise left shift instead of just multiplying by 2, but believe me it's not!
  &lt;/li&gt;
  &lt;li&gt;
    Don't try to optimise your code. First of all you probably don't need to (at least not yet), and second, no offence,
    but chances are the compiler will do a better job of optimising your code than you can anyway!
  &lt;/li&gt;
  &lt;li&gt;
    Be consistent. Decide on a coding standard and stick to it, don't just make it up as you go along.
  &lt;/li&gt;
  &lt;li&gt;
    Be clear about what you're trying to do. If it's not clear, write a comment. But don't waste space with obvious
    and redundant comments like &quot;iterate through the array&quot;.
  &lt;/li&gt;
  &lt;li&gt;
    Use sensible names. If you choose a poor or misleading name for a class or variable it can make the code incredibly
    difficult to understand. People will make assumptions based on these names, so take a few minutes to think of names
    that actually make sense.
  &lt;/li&gt;
  &lt;li&gt;
    Think before you write. Two minutes of thinking time now could save you hours of pain later.
  &lt;/li&gt;
  &lt;li&gt;
    Keep it clean. If something isn't used, get rid of it. Don't comment it out, delete it! You can always go back to your
    source control if you need it.
  &lt;/li&gt;
  &lt;li&gt;
    Remember that there's no such thing as temporary code. Despite what you might think, the code you're writing right now
    will probably have a much longer life that you think. So take the time to do it right the first time.
  &lt;/li&gt;
  &lt;li&gt;
    Finally, as I've said before... &lt;a href=&quot;/entries/dont_be_lazy.html&quot;&gt;don't be lazy!&lt;/a&gt;
  &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
  Basically it all comes down to taking a little pride in your code. You should be able to look back at code you've written
  and be proud of it. Not only will it make your life easier when you need to re-visit the code, but it will benefit the
  whole team. If you spend the time doing things right in your code, pretty soon you'll see the external effects of a more solid,
  reliable and bug-free piece of software.
&lt;/p&gt;
</description>
      <author>Shane Bell</author>
      <pubDate>Wed, 30 May 2007 11:13:00 +0000</pubDate>
      <guid isPermaLink="true">http://blog.onlysimpler.com/entries/readable_code.html</guid>
    </item>
    <item>
      <title>What Kind Of Developer Are You?</title>
      <link>http://blog.onlysimpler.com/entries/what_kind_of_developer.html</link>
      <description>&lt;p&gt;
  I've worked with lots of different developers. Some good, some bad, and some really, really bad. But everyone is 
  pretty easy to categorize...
&lt;/p&gt;
&lt;p&gt;
  &lt;em&gt;Political correctness note: The following paragraphs refer to developers as being male. This is simply to make
  the article a little easier to read and because I don't have the patience to type (s)he or his/hers everywhere. 
  So don't make a big deal out of it.&lt;/em&gt;
&lt;/p&gt;
&lt;p&gt;
  &lt;em&gt;Tongue In Cheek Note: Also be aware that this should be taken with a grain of salt, try not to take it too
  seriously. However, we all know there's a little bit of truth in every joke.&lt;/em&gt;
&lt;/p&gt;
&lt;p&gt;
  &lt;strong&gt;The Cobbler:&lt;/strong&gt;
  The cobbler doesn't really like coding, he just does it to pay the bills. As a result, he as absolutely no interest 
  in doing anything properly. He just beats away at the code, cobbling it into shape until the unit tests pass. Once 
  he gets that elusive green bar, he checks it in and moves onto the next task. No time is wasted on comments or 
  writing code that someone else may actually be able to read one day, that stuff just slows him down. If the tests 
  pass then that's good enough. Cobblers tend to slow the entire team down as other developers often have to clean up 
  the mess that they have left behind.
&lt;/p&gt;
&lt;p&gt;
  &lt;strong&gt;The Academic:&lt;/strong&gt;
  The academic never actually gets anything done. He spends all day reading up on the latest design patterns and 
  architectural best practices and running performance metrics against the code. Academics are typically very 
  intelligent, and tend to write pretty good code. The problem is they rarely get around to actually writing any code. 
  If they could just put down that whitepaper and get coding they might be able to get some work done! Academics are 
  sometimes called &quot;Mirror Men&quot; as they are always looking into things.
&lt;/p&gt;
&lt;p&gt;
  &lt;strong&gt;The Modeler:&lt;/strong&gt;
  The modeler is a close cousin of the academic, and gets about the same amount of work done. Typically a modeler 
  has just completed his masters and thinks the real world is just like a university assignment. As a result, he 
  spends much of his time drawing fancy diagrams that nobody either understands or cares about.
&lt;/p&gt;
&lt;p&gt;
  &lt;strong&gt;The Wannabe:&lt;/strong&gt;
  The wannabe wrote some Pascal at high school and thinks that writing Excel macros qualifies a programming. His CV 
  consists mostly of acronyms and he is convinced he's experienced in every programming language ever invented. 
  Unfortunately, wannabes are typically clueless and cause more harm than good. Put them on a side project with the 
  academics and the modelers to keep them away from your real code.
&lt;/p&gt;
&lt;p&gt;
  &lt;strong&gt;The Hardware Guy:&lt;/strong&gt;
  The hardware guy is actually very useful to have around. He can build you a load balanced Linux cluster running RAID
  5 in an afternoon with his eyes shut. In his spare time, he likes to dabble in programming. Unfortunately, he 
  couldn't code his way out of a wet paper bag. But so long as you keep him busy building hardware, and keep him as 
  far away from an IDE as possible you'll be ok.
&lt;/p&gt;
&lt;p&gt;
  &lt;strong&gt;The Architect:&lt;/strong&gt;
  There are two kinds of architect - those that were once (and often still are) excellent developers and understand 
  how software development works, and those that became architects because they sucked at programming. If you're 
  unfortunate enough to work with the latter then you might have to sprinkle a little fairy dust throughout your code 
  now and again to get their impossible ideas to work.
&lt;/p&gt;
&lt;p&gt;
  &lt;strong&gt;The Hacker:&lt;/strong&gt;
  Hackers are usually far more interested in writing Linux device drivers for their cell phone than those boring 
  projects thrust upon them by their real jobs. The good news is that they are often exceptional developers and if you 
  treat them right, you might actually convince them to do some real work for you now and again.
&lt;/p&gt;
&lt;p&gt;
  &lt;strong&gt;The Old Timer:&lt;/strong&gt;
  These guys are old enough to have worked with Von Neumann back in the day. Buy an old timer a few beers and he might 
  just tell you a few stories about &quot;the good old days&quot; of programming with punch cards and lab coats. Old timers tend 
  to fall into two camps. There are those who kept up with technology and know absolutely everything there is to know 
  about computers. Be nice to these guys and maybe you'll learn a thing or two. Then there are the old timers who 
  didn't quite manage to keep up. They often struggle to use a mouse and don't quite understand what this &quot;internet&quot; 
  thing is all about.
&lt;/p&gt;
&lt;p&gt;
  &lt;strong&gt;The Consultant:&lt;/strong&gt;
  The consultant works for one of those big clueless consulting companies that charges way too much for developers 
  that deliver far too little. Consultant programmers often have arts degrees and don't know the first thing about 
  programming. But with their snappy suits and a bit of charm, somehow they manage to sneak through anyway.
&lt;/p&gt;
&lt;p&gt;
  &lt;strong&gt;The Historian:&lt;/strong&gt;
  Historians like to party like it's 1981. Their tool box consists of vi and the command line. They tried an IDE once 
  upon a time, but didn't care for it, so went back to their pre-historic development practices. If you ever need to 
  know how to write a vi macro then the historian is your man. However, if you need some code written any time soon, 
  ask someone who uses an IDE.
&lt;/p&gt;
&lt;p&gt;
  &lt;strong&gt;The Genius:&lt;/strong&gt;
  The genius is a rare beast. He writes readable, maintainable, well designed, stable, bug free code. Basically he 
  just &quot;gets it&quot; and does things right. Not only does he get the job done, but he takes the time to do it properly. 
  If you manage to find one of these rare developers, do anything you can to keep them happy. I mean it, anything.
&lt;/p&gt;
</description>
      <author>Shane Bell</author>
      <pubDate>Mon, 19 Mar 2007 19:55:43 +0000</pubDate>
      <guid isPermaLink="true">http://blog.onlysimpler.com/entries/what_kind_of_developer.html</guid>
    </item>
    <item>
      <title>Generic Methods in Java</title>
      <link>http://blog.onlysimpler.com/entries/generic_methods_in_java.html</link>
      <description>&lt;p&gt;
  Here's something interesting I only just discovered about generics in Java.
&lt;/p&gt;
&lt;p&gt;
  You're probably well aware that you can write a generic method like this:
&lt;/p&gt;
&lt;pre&gt;
public static &amp;lt;T&amp;gt; Collection&amp;lt;T&amp;gt; commonElements(Collection&amp;lt;T&amp;gt; c1, Collection&amp;lt;T&amp;gt; c2) {
    Collection&amp;lt;T&amp;gt; results = new ArrayList&amp;lt;T&amp;gt;();
    for (T t : c1) {
        if (c2.contains(t)) {
            results.add(t);
        }
    }
    return results;
}&lt;/pre&gt;
&lt;p&gt;
  This is a simple method that returns a list of elements that are common between two collections. This compiler 
  automatically determines the type of the returned collection based on the type of the input parameters c1 and c2.
&lt;/p&gt;
&lt;p&gt;
  Here's an example of how it might be used (assume the &lt;code&gt;commonElements()&lt;/code&gt; method is in a class called
  Example)
&lt;/p&gt;  
&lt;pre&gt;
Collection&amp;lt;Integer&amp;gt; c1 = Arrays.asList( new Integer[] { 1, 2, 3});
Collection&amp;lt;Integer&amp;gt; c2 = Arrays.asList( new Integer[] { 2, 3, 4});
Collection&amp;lt;Integer&amp;gt; common = Example.commonElements(c1, c2);&lt;/pre&gt;
&lt;p&gt;  
  Ok, nothing new so far.
&lt;/p&gt;
&lt;p&gt;
  So here's what I only just discovered. You can also write generic methods that determine the return type based on
  what you're assigning the result to. For example:
&lt;/p&gt;
&lt;pre&gt;
public static &amp;lt;T&amp;gt; List&amp;lt;T&amp;gt; createEmptyList() {
    return new ArrayList&amp;lt;T&amp;gt;(); 
}&lt;/pre&gt;
&lt;p&gt;
  This method will create an empty list of whatever type you assign it to. So you could call the method like this:
&lt;/p&gt;
&lt;pre&gt;
List&amp;lt;Integer&amp;gt; listOfIntegers = Example.createEmptyList();&lt;/pre&gt;
&lt;p&gt;
  So long as the compiler can determine the type of variable that you're assigning to, then all is well. 
  There is one interesting edge case where this doesn't quite work. In the example below, the compiler can't 
  determine the type and will complain with a &quot;type mismatch&quot; error.
&lt;/p&gt;
&lt;pre&gt;
boolean condition = true;
List&amp;lt;String&amp;gt; listOfStrings = (condition ? Example.createEmptyList() : null);&lt;/pre&gt;
&lt;p&gt;  
  To fix it, you need to prefix the method call with the type like so:
&lt;/p&gt;
&lt;pre&gt;
boolean condition = true;
List&amp;lt;String&amp;gt; listOfStrings = (condition ? Example.&lt;strong&gt;&amp;lt;String&amp;gt;&lt;/strong&gt;createEmptyList() : null);&lt;/pre&gt;
&lt;p&gt;
  I only learned about this feature of generics when I accidentally discovered that Java 5 introduced a new static method 
  to the &lt;code&gt;java.util.Collections&lt;/code&gt; class called &lt;code&gt;emptyList()&lt;/code&gt;. It serves a similar purpose to the 
  old &lt;code&gt;EMPTY_LIST&lt;/code&gt; field, but caters for generic collections.
&lt;/p&gt;</description>
      <author>Shane Bell</author>
      <pubDate>Wed, 07 Feb 2007 10:04:00 +0000</pubDate>
      <guid isPermaLink="true">http://blog.onlysimpler.com/entries/generic_methods_in_java.html</guid>
    </item>
    <item>
      <title>EclEmma</title>
      <link>http://blog.onlysimpler.com/entries/ecl_emma.html</link>
      <description>&lt;p&gt;
  An excellent (and free) &lt;a href=&quot;http://www.eclipse.org/&quot;&gt;Eclipse&lt;/a&gt; plugin called &lt;a href=&quot;http://www.eclemma.org/&quot;&gt;
  EclEmma&lt;/a&gt; was recently released that is well worth having a look at.
&lt;/p&gt;
&lt;p&gt;
  EclEmma is a code coverage tool that measures exactly how much of your codebase is being executed when you run your
  application. Typically, you'd use it to measure how much coverage your unit tests are getting, but it's not limited
  to just tests. You could just as easily use it to perform usability testing for a GUI application to find areas of
  your application that aren't being used.
&lt;/p&gt;
&lt;p&gt;
  The integration with Eclipse is very nice. You can select exactly which code you want to cover (although there are 
  few &lt;a href=&quot;http://www.eclemma.org/faq.html#usage02&quot;&gt;limitations&lt;/a&gt; around this) and the 
  &lt;a href=&quot;http://www.eclemma.org/images/screen.png&quot;&gt;end result&lt;/a&gt; is a nice navigable list of source files showing
  percentage covered for each method. It highlights your source code too so you can see exactly which lines are
  (and probably more importantly those that are not) being executed.
&lt;/p&gt;
&lt;p&gt;
  Behind the scenes, EclEmma is based on the popular open source code coverage tool
  &lt;a href=&quot;http://emma.sourceforge.net/&quot;&gt;Emma&lt;/a&gt; (although you probably already guessed that from the name) which has
  been around for quite some time and is rock solid. Emma is also the tool of choice for the new code coverage 
  functionality in version 6 of &lt;a href=&quot;http://www.jetbrains.com/idea/&quot;&gt;IntelliJ IDEA&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
  One of the unfortunate side-effects of code coverage is that it can be extremely frustrating! It's all too tempting
  to keep hammering away at your unit tests to try and reach that elusive 100% coverage. A word of advice... don't
  bother! You can waste many an hour doing that, time that's better spent writing real code :)
&lt;/p&gt;
</description>
      <author>Shane Bell</author>
      <pubDate>Mon, 05 Feb 2007 16:21:51 +0000</pubDate>
      <guid isPermaLink="true">http://blog.onlysimpler.com/entries/ecl_emma.html</guid>
    </item>
    <item>
      <title>Schema Spy</title>
      <link>http://blog.onlysimpler.com/entries/schama_spy.html</link>
      <description>&lt;p&gt;
  If you're working on a project that uses a relational database (I don't think I've ever worked on one that didn't) 
  then you really need to check out &lt;a href=&quot;http://schemaspy.sourceforge.net&quot;&gt;SchemaSpy&lt;/a&gt;. It's a great tool that 
  automatically produces a complete overview of your schema - diagrams and all.
&lt;/p&gt;
&lt;p&gt;
  Here's a &lt;a href=&quot;http://schemaspy.sourceforge.net/sample/index.html&quot;&gt;sample&lt;/a&gt; of what it can produce.
&lt;/p&gt;
&lt;p&gt;
  It's very useful for navigating around the tables and seeing relationships between them. It even does some analysis 
  of your schema to identify any potential problems.
&lt;/p&gt;
&lt;p&gt;
  One thing to note is that it takes a static &quot;snapshot&quot; of your schema. The information is not produced in real time,
  so you'll need to re-run the program now and again to keep it up to date.
&lt;/p&gt;
&lt;p&gt;
  The application itself is a single jar file that you can execute from a command line. You'll also need a JDBC driver
  so it can talk to your database.
&lt;/p&gt;
&lt;p&gt;
  Finally, in order to produce all the fancy diagrams, you'll need the &quot;dot&quot; executable from 
  &lt;a href=&quot;http://www.graphviz.org/&quot;&gt;Graphviz&lt;/a&gt;. Check the docs for more info on that.
&lt;/p&gt;
&lt;p&gt;
  And best of all - it's free (as in beer).
&lt;/p&gt;</description>
      <author>Shane Bell</author>
      <pubDate>Tue, 21 Nov 2006 21:48:00 +0000</pubDate>
      <guid isPermaLink="true">http://blog.onlysimpler.com/entries/schama_spy.html</guid>
    </item>
    <item>
      <title>Subversion - The 30 Second Guide</title>
      <link>http://blog.onlysimpler.com/entries/subversion_30_second_guide.html</link>
      <description>&lt;p&gt;
  Setting up a &lt;a href=&quot;http://subversion.tigris.org&quot;&gt;Subversion&lt;/a&gt; repository is one of those things that I can 
  never seem to remember how to do. I guess that's because you tend to do it once at the start of a project and it 
  might be 6 months before you need to do it again. And by that time you've forgotten how to do it and need to go 
  search through the docs.
&lt;/p&gt;
&lt;p&gt;
  So here is a very quick guide to setting up a new repository.
&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;
    Create a new repository.&lt;br/&gt;
    &lt;pre&gt;$ svnadmin create path_to_repository&lt;/pre&gt;
  &lt;/li&gt;
  &lt;li&gt;
    Modify the svnserve.conf and passwd files.
  &lt;/li&gt;
  &lt;li&gt;
    Create new project directory.&lt;br/&gt;
    &lt;pre&gt;$ svn mkdir svn://path/to/repo/myproject&lt;/pre&gt;
  &lt;/li&gt;
  &lt;li&gt;
    Create a sub-directory for the trunk (you can create branches and tags later if necessary).&lt;br/&gt;
    &lt;pre&gt;$ svn mkdir svn://path/to/repo/myproject/trunk&lt;/pre&gt;
  &lt;/li&gt;
  &lt;li&gt;
    Checkout the project and start working.&lt;br/&gt;
    &lt;pre&gt;$ svn checkout svn://localhost/repository/project/trunk&lt;/pre&gt;
  &lt;/li&gt;
&lt;/ol&gt;</description>
      <author>Shane Bell</author>
      <pubDate>Mon, 23 Oct 2006 22:13:00 +0000</pubDate>
      <guid isPermaLink="true">http://blog.onlysimpler.com/entries/subversion_30_second_guide.html</guid>
    </item>
    <item>
      <title>Synchronized Collections in Java</title>
      <link>http://blog.onlysimpler.com/entries/synchronized_collections_in_java.html</link>
      <description>&lt;p&gt;
  This is just a quick word of advice to anyone out there writing threaded code with collections. I recently had to
  fix an old neglected web application that was having some serious threading issues and came across this little gem.
&lt;/p&gt;
&lt;p&gt;
  Creating a synchronized collection is as easy. All you need to do is...
&lt;/p&gt;
&lt;pre&gt;List&amp;lt;Long&amp;gt; myList = Collections.synchronizedList(new ArrayList&amp;lt;Long&amp;gt;());&lt;/pre&gt;
&lt;p&gt;
  However, be careful when iterating over the collection. You still need to manually synchronize code that iterates
  over it, even though the collection itself is synchronized. Standard operations like adding or removing elements
  from the collection are thread-safe and will behave as expected in a multi-threaded environment. But iterating is
  not as simple.
&lt;/p&gt;
&lt;p&gt;
  If you want to iterate over a collection that will potentially be accessed by other threads simultaneously, always
  synchronize on the list itself. ie:
&lt;/p&gt;
&lt;pre&gt;
List&amp;lt;Long&amp;gt; myList = Collections.synchronizedList(new ArrayList&amp;lt;Long&amp;gt;());

// thread safe code
synchronized(myList) {
    for (Long l : myList) {
        // do something with l here
    }
}&lt;/pre&gt;
&lt;p&gt;
  Failure to synchronize on the collection can cause all kinds of unexpected behavior. Typically you'll get a 
  ConcurrentModificationException if the collection is modified while you're iterating over it, but there are no
  guarantees. And that's the problem. An application that falls over and dies is bad, but at least you know it's
  broken. An application that continues on merrily despite being broken is a very bad thing!
&lt;/p&gt;</description>
      <author>Shane Bell</author>
      <pubDate>Thu, 28 Sep 2006 08:45:17 +0000</pubDate>
      <guid isPermaLink="true">http://blog.onlysimpler.com/entries/synchronized_collections_in_java.html</guid>
    </item>
    <item>
      <title>Closures in Java</title>
      <link>http://blog.onlysimpler.com/entries/closures_in_java.html</link>
      <description>&lt;p&gt;
  There's been &lt;a href=&quot;http://gafter.blogspot.com/2006/08/closures-for-java.html&quot;&gt;some talk&lt;/a&gt; recently about adding 
  &lt;a href=&quot;http://blogs.sun.com/roller/resources/ahe/closures.pdf&quot;&gt;closures&lt;/a&gt; to the Java language. I've spent quite
  a bit of time writing Ruby code recently, and it's amazing how useful closures and code blocks can be. They're one
  of those things - once you know how to use them, you start noticing all kinds of places in your code that could be
  solved using closures. It's like when you learn a definition of a new word and suddenly you start hearing it in
  conversations everywhere.
&lt;/p&gt;
&lt;p&gt;
  It will be interesting to see how the proposal turns out, but unfortunately we'll have to wait until Java 7 to use 
  them. Considering how many people still haven't made the move to Java 5, this could be a while for some. In the 
  meantime, there are a number of hacks that you can use to provide similar functionality to a closure. In fact, 
  &lt;a href=&quot;http://jakarta.apache.org/commons/collections/&quot;&gt;Apache Commons Collections&lt;/a&gt; even has some built in 
  classes that provide closure-like functionality.
&lt;/p&gt;
&lt;p&gt;
  But if you need something quick and dirty, this should do the trick.
&lt;/p&gt;
&lt;p&gt;
  First, define a simple interface to represent a closure:
&lt;/p&gt;
&lt;pre&gt;
public interface Closure {
    public void execute(Object o);
}&lt;/pre&gt;
&lt;p&gt;
  The next step is to create a method that calls the execute method of the closure. The following example iterates
  over a collection and calls the execute method on every object.
&lt;/p&gt;
&lt;pre&gt;
public static void execOnEach(Collection&amp;lt;?&amp;gt; collection, Closure closure) {
    for (Object o : collection) {
        closure.execute(o);
    }
}&lt;/pre&gt;
&lt;p&gt;
  Finally, we write some code that uses the closure interface to execute a block of code on every object in a 
   collection. We do this by creating an anonymous instance of the closure.
&lt;/p&gt;
&lt;p&gt;
  This example uses a collection of StringBuffer objects. The first call to &lt;code&gt;execOnEach()&lt;/code&gt; converts each 
  buffer to upper case, while the second call reverses the contents of the buffers.
&lt;/p&gt;
&lt;pre&gt;
public static void main(String[] args) {

    Collection&amp;lt;StringBuffer&amp;gt; c = new ArrayList&amp;lt;StringBuffer&amp;gt;();
    c.add(new StringBuffer(&quot;abcdefg&quot;));
    c.add(new StringBuffer(&quot;hijklmn&quot;));
    System.out.println(&quot;c = &quot; + c);

    // capitalise everything
    execOnEach(c, new Closure() {
        public void execute(Object o) {
            StringBuffer buffer = (StringBuffer) o;
            buffer.replace(0, buffer.length(), buffer.toString().toUpperCase());
        }
    });

    System.out.println(&quot;c = &quot; + c);

    // reverse the strings
    execOnEach(c, new Closure() {
        public void execute(Object o) {
            StringBuffer buffer = (StringBuffer) o;
            buffer.reverse();
        }
    }); 

    System.out.println(&quot;c = &quot; + c);
}&lt;/pre&gt;
&lt;p&gt;
  This is a pretty trivial example, but you get the idea.
&lt;/p&gt;</description>
      <author>Shane Bell</author>
      <pubDate>Wed, 27 Sep 2006 08:45:13 +0000</pubDate>
      <guid isPermaLink="true">http://blog.onlysimpler.com/entries/closures_in_java.html</guid>
    </item>
    <item>
      <title>Exposing Your Privates</title>
      <link>http://blog.onlysimpler.com/entries/exposing_your_privates.html</link>
      <description>&lt;p&gt;
  If you've ever written any serious &lt;a href=&quot;http://www.junit.org&quot;&gt;unit tests&lt;/a&gt; for your code then you'll know
  that testing private methods can be a bit tricky. The only class that has access to a private method is the class
  that defines it. So how are you supposed to test it from another class that doesn't have visibility of the method?
&lt;/p&gt;
&lt;p&gt;
  One solution would be to NOT declare it as private. This would work, but it's not much of a solution. Methods are 
  made private for good reason and you can't just go around changing their access to suit your testing needs. That's 
  just wrong. 
&lt;/p&gt;
&lt;p&gt;
  Purists would say that you should test private methods via the public methods that expose their functionality. 
  Purist say a lot of crap.
&lt;/p&gt;
&lt;p&gt;
  Seriously though, sometimes you need to test a private method, and there just no way around it. Fortunately there's 
  easy solution to the problem. A bit of reflection should do the trick.
&lt;/p&gt;
&lt;pre&gt;
/**
 * Invokes a private method on a class or object. This method
 * can invoke static methods on classes, and instance methods
 * on objects.
 * 
 * When invoking an instance method, the instance of the object
 * must be passed as the first parameter. For static methods,
 * this parameter can be null.
 * 
 * @param object the object to invoke the method on (this is
 *        only applicable for instance methods, can be null
 *        for static methods).
 * @param clazz the class of object.
 * @param methodName the name of the method to invoke.
 * @param parameters parameters passed to the method.
 * @return result from the method or null if method returns void.
 */
public static Object invokePrivateMethod(Object object,
                                         Class clazz,
                                         String methodName,
                                         Object... parameters) {

    // build an array of parameter types
    Class[] params = new Class[parameters.length];
    for (int i = 0; i &amp;lt; parameters.length; i++) {
        params[i] = parameters[i].getClass();
    }

    Object result = null;

    // try to execute the method
    try {
        Method method = clazz.getDeclaredMethod(methodName, params);
        method.setAccessible(true);

        // are we invoking a static method or an instance method?
        if (object != null) {
            result = method.invoke(object, parameters);
        } else {
            result = method.invoke(clazz, parameters);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return result;
}&lt;/pre&gt;
&lt;p&gt;
  The javadoc comment pretty much explains it, but here's a quick example that you shows how to use it. 
&lt;/p&gt;
&lt;p&gt;
  Let's assume we have a class called Greeter that looks like this.
&lt;/p&gt;
&lt;pre&gt;
public class Greeter {

    // a private instance member
    private String name;

    // public constructor
    public Greeter(String name) {
        this.name = name;
    }

    // static method that responds with a cheery greeting
    private static String sayHello(String name) {
        return &quot;Hello &quot; + name;
    }

    // instance method that responds with a cheery greeting
    private String sayHello() {
        return &quot;Hello &quot; + this.name;
    }
}&lt;/pre&gt;
&lt;p&gt;
  Notice it has two private methods. One is a static method, the other is an instance method. Using 
  &lt;code&gt;invokePrivateMethod()&lt;/code&gt; that we defined earlier we can invoke these methods as follows.
&lt;/p&gt;
&lt;pre&gt;
// invoke a static method
invokePrivateMethod(null, Greeter.class, &quot;sayHello&quot;, &quot;Shane&quot;);

// invoke an instance method
Greeter greeter = new Greeter(&quot;Shane&quot;);
invokePrivateMethod(greeter, greeter.getClass(), &quot;sayHello&quot;);&lt;/pre&gt;
&lt;p&gt;
  Now you really have no excuse for not writing unit tests ;)
&lt;/p&gt;
</description>
      <author>Shane Bell</author>
      <pubDate>Fri, 25 Aug 2006 13:26:55 +0000</pubDate>
      <guid isPermaLink="true">http://blog.onlysimpler.com/entries/exposing_your_privates.html</guid>
    </item>




  </channel>
</rss>