February 03, 2006

The value of being explicit….

So I had to explain some code I wrote a year or so ago to the colleague who was maintaing it and it looked something like this:


  public CollaboratorA doSomething() {
    final Set data = new HashSet();
    // .. populate the data
    return new CollaboratorA {
      boolean isThisTrueForSomeCriteria(final Object o) {
        return data.contains(o);
      }
    }
  }

which is all perfectly valid; but not at all intuitive. In particular; what was the lifetime of Set data?

I then rewrote this and moved the inner class into it's explicit class:


  public CollaboratorA doSomething() {
    final Set data = new HashSet();
    // .. populate the data
    return new SetBackedCollaboratorA(data);
  }

  class SetBackedCollaboratorA implements CollaboratorA {
    private final Set data;

    SetBackedCollaboratorA(final Set theData) {
      this.data = theData;
    }

    boolean isThisTrueForSomeCriteria(final Object o) {
      return data.contains(o);
    }
  }

and this was found to be much easier to understand even though under the hood there is absolutely no difference. I am not entirely sure whether the byte code would even be any different, but the intuitiveness or readability was improved; so it was a win :)

To be clear; it was not the use an an inner class that was confusing; it was the fact that an inner class was referencing a variable defined in the method, outside of the method that created the inner class and the data.

Lesson for the day (because I keep needing to learn it again ;)); code that is functional is not as valuable as code that is intuitively functional.


- 2 comments by 2 or more people Not publicly viewable

  1. Chris May

    Steve McConnel (In 'writing solid code' I think) makes the point that he'd rather have comprehensible-but-broken code than incomprehensible-but-working, because the former can be fixed and subsequently changed, whereas the latter will probably have to be re-written when a change is required.

    03 Feb 2006, 12:49

  2. The interesting point is that "comprehensible" is relative :)

    03 Feb 2006, 13:30


Add a comment

You are not allowed to comment on this entry as it has restricted commenting permissions.

February 2006

Mo Tu We Th Fr Sa Su
Jan |  Today  | Mar
      1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28               

Search this blog

Tags

Galleries

Most recent comments

  • Interesting… While I'm not completely convinced in such microbenchmarks, I'm pretty sure that 1ms … by Alexander Snaps on this entry
  • Hello. I bought the book yesterday. I was trying to find the source code for chapter 11 and chapter … by Suleman on this entry
  • http://woosight.net/account/login?username=demo by live mashup demo on this entry
  • Thanks mate ….. This blog was really helpful. by Maaz Hurzuk on this entry
  • Ty. Not directly helpful for my problem, but pointed me in the right direction. You will also get th… by Mike E. on this entry

Blog archive

Loading…
Not signed in
Sign in

Powered by BlogBuilder
© MMXXI