cat /dev/random

June 7, 2008

Wallboard + paint + pressure = superglue

Filed under: Uncategorized — brian @ 1:20 pm

This surprised the heck out of me.  We recently finished a new TV room down in the basement.  We have a 50″ plasma TV, mounted on the wall with an Omnimount UCL ландшафтmount (quite an impressive bit of engineering — not cheap, but highly recommended.)  Since the mount weighs upwards of 40 pounds, and supports a TV that weights 100 pounds on a torque arm as long as 2ft, it needs to be anchored pretty solidly to the wall.  It is held up with 6 4″ long, 3/8″ wide lag screws, that screw into two separate studs.  It does its job well.

So, my dad and I went to remove it from the wall.  I removed the six screws, and we prepared to catch the mount.  It didn’t fall off the wall.  We tugged on it, and it still didn’t come off the wall.  Seemed stuck so tight we thought we’d missed a screw!  But we convinced ourselves there were no more screws, and the two of us pulled hard, and eventually it came off the wall — taking some of the wallboard with it.  Apparently the pressure of being screwed up against the wallboard (and maybe the heat from the TV too over a few years) turned the painted surface into a glue not only strong enough to hold a 40lb mount to a vertical wall but resist being pulled off! 

June 2, 2008

Questions from the Peanut Gallery, part I

Filed under: Uncategorized — brian @ 1:27 pm

At the “Writing the Next Great Java Book” BOF at JavaOne, there were unfortunately many more questions than we had time to answer.  Fortunately, Greg Doench saved the questions (presciently, they were submitted on paper), so I’ll answer a few here. 

Q: About how much time did you put into your book (effort, not duration)? 
A: In my case, the answers for effort and duration are the same, as I had the luxury of writing the book mostly-full-time — I made the book my foreground activity, though I still did some consulting and training while I was working on it.  I spent approximately 16 months on the book — longer than planned (but in hindsight is no surprise.) 

Q: Was the financial compensation worthwhile?
A: Unless your name is Stephen King or JK Rowling, writing books is not something you do for the financial compensation.  This is more true for technical books, because (a) the audience for books like Java Concurrency in Practice is not quite as large as the audience for Harry Potter, and (b) if you have the skills to write a good technical book you probably have the skills to get a well-paying technical job.  Without going into the details, I’ll say that the compensation is about what I expected — but I went into it with very realistic expectations.  The compensation comes in other forms. 

Q: How much support and assistance was provided by the publisher?
A: I think this is a matter of how much support and assistance you ask of the publisher — and how much the publisher thinks you need.  In our case, we did everything ourselves, including typesetting and managing the review, copy editing, and index creation.  These are things the publisher often does for authors (and might even have preferred to do), but we chose to do it ourselves, and the publisher agreed.  Of course, this was more work, but it was work we gladly did.  The A-W team was always responsive when we did ask for things.  So I think the answer is “as much as you appear to need.”

Q: How does the short half-life of technical topics affect the effort?
A: I deliberately chose a topic with a longer half-life.  This gave me the latitude to let the book tell me when it was done, rather than the schedule.  For material with a shorter shelf life, I might be inclined to choose a shorter format, so that the book is less out-of-date by the time it is published. 

Q: What would you say about books that authors release chapters to the public as they write?
A: I think this presupposes a style of writing where the author sits down and writes the book linearly.  I am sure some authors do this, and some topics are more amenable to this approach than others.  But one of the most important freedoms in writing is the freedom to refactor continuously; very often you don’t figure out the right way to present the material until you’ve presented it the wrong way (just as with code.)  There’s nothing wrong with putting the work out there early — this is a great source of free review — but you have to be careful that doing so doesn’t cause you to settle into the belief that the structure of the book has been decided.  (The same risk is true of trying to adhere to a schedule that assigns due dates to specific chapters.) 

Q: How do you avoid example source code exploding without using unrealistic examples?
A: This is really hard!  But its really important.  In JCiP, we set a rule for ourselves of “no code example more than a page”, with the target of making most of them a half page or less.  This is not easy, especially in Java! (There was only one we had to break into two separate one-page listings.)  We wanted the examples to each illustrate a single point, so that the reader could look at the example and easily see what it was trying to show.  There are some obvious tricks; eliminating boilerplate code like constructors, getters, and setters helps a little bit.  What worked for us was to pick realistic examples that the audience would immediately understand the utility of (such as a file crawler), but abstract away the irrelevant concrete details by not showing the bodies of methods that are not needed to make the point that the example is supposed to illustrate.  For example, we have a set of examples in Chapter 8 where we illustrate searching for solutions to a class of puzzles such as the “sliding block puzzles.”  But rather than focus on a specific puzzle — which would take lots of space and not offer all that much insight, we abstract the nature of the puzzle by defining an interface that specifies the initial position (in terms of an abstract Position class), valid moves (in terms of an abstract Move class), and the goal position.  Then we can illustrates various search techniques in terms of the abstract puzzle without getting bogged down in the details. 

Q: What would you say is the role of technical books in the age where the Internet is the fastest way to publish texts and technology changes so fast that one year after publishing texts become irrelevant?
A: Some technical books are simply a form of documentation; any book that has a version number in the title is likely to fall into this category.  These books have a very short shelf life.  Other books, those that tend to focus on concepts rather that specific technical details, tend to have a longer shelf life.   In any case, the publishing industry needs to become more agile in its approach to managing the authoring and production process, and explore more seriously alternate publication vectors such as electronic publishing. 

More later. 

May 31, 2008

The making of JCiP; avoiding errors in code listings

Filed under: software — brian @ 12:29 pm

At JavaOne this month, my publisher at Pearson, Greg Doench, hosted a panel/BOF entitled “Writing the Next Great Java Book.” Not surprisingly, when you get four authors in a room, you get seven opinions about what’s important in writing a book!

One thing that we (mostly Tim, actually) did for JCiP was set up an infrastructure for the book, similar to what you’d do for a software project. Version control, issue tracking, one-step build script, continuous build — all of these things offer the same benefits to book projects as they do for software.

One critical aspect of the build is the handling of program listings. It is incredibly tempting to cut and paste examples from the IDE into whatever source format you’re writing in (Word, Frame, LaTeX, DocBook), but this is a recipe for disaster — errors will invariably creep in as you try and make small tweaks (such as changing variable names) outside the IDE. And code examples with errors really undermine the reader’s confidence (or worse, they copy the incorrect example into their code.) So, we wanted to make sure that every code example compiled (and ideally, was tested.)

Our approach was to check the code into Subversion with the rest of the book artifacts, ensure that the build process compiled the code and ran the unit tests, and then automatically extract the examples from the code in a format into which they could be directly included by the build. Some systems (LaTeX, DocBook) make this sort of inclusion easier than others.

We marked the examples up with comments for formatting (bold, italic) and also with “snip here” comments that excluded the irrelevant portions of the code from the listings that actually went into the book. The attached perl script (phragmite.pl), written by Tim Peierls (based on an approach designed by Ken Arnold), takes as input a set of input files and produces a set of LaTeX files representing the extracted listings.

As an example, here is the Counter listing from Listing 4.1 of JCiP:

// !! Counter Simple thread-safe counter using the Java monitor pattern
// vv Counter
@ThreadSafe
public final class Counter {
  /*[*/@GuardedBy("this")/*]*/ private long value = 0;
  public /*[*/synchronized/*]*/ long getValue() {
    return value;
  }
  public /*[*/synchronized/*]*/ long increment() {
    if (value == Long.MAX_VALUE)
      throw new IllegalStateException(”counter overflow”);
    return ++value;
  }
}
// ^^ Counter

The first line identifies the type of the code fragment (!! for a “good example”, ?? for a “bad example” which would get decorated with a Mr. Yuk), the name of the fragment (Counter), and the listing caption. The lines with the ^^ and vv mean “snip from here to here”, and a listing can be made of multiple such fragments. The /*[*/ and /*]*/ comments mean “bold”. The following ANT target ran the script:

  <target name="listings">
    <exec dir="${bin.dir}" executable="perl">
      <arg value="${phragmite.pl}"/>
      <arg value="${listings.dir}"/>
      <arg value="${fragments.dir}/*.java"/>
      <arg value="${fragments.dir}/jcip/*.java"/>
    </exec>
  </target>

In the book’s LaTeX source, we use the following LaTeX macro to pull the listing in:

\newcommand{\JavaListing}[1]{\input{listings/#1}%

May 17, 2008

Apologies for the malware warnings

Filed under: Uncategorized — brian @ 10:53 am

Apparently, WordPress is vulnerable to some script injection bugs, and this site was hit by them.  And Google tagged the site as “spreading malware”, so the site shows up with a warning in Google search results and FF3 users can’t get to it at all.  I’ve upgraded Wordpress, scoured the DB for injected scripts, and am in the process of begging google to let me off the blacklist.

What a pain in the butt.  People suck. 

April 30, 2008

Tailored, indeed

Filed under: fun — brian @ 9:48 pm

I noticed this amusing typo at the trade show floor at SDWest this year.  My first thought: wow, they really are tailored!

 tailored_indeed.jpg

Hacker at work

Filed under: fun, life — brian @ 9:45 pm

HackerAtWork

This photo was taken in Josh Bloch’s garage on a recent trip to CA.  We were installing a new fireplace grille after the fireplace had been re-faced with some beautiful vintage tiles.  Even though this was an entirely analog activity, we still managed to get a little hacking in. 

December 10, 2007

A day and a half in the life…

Filed under: software — brian @ 5:23 pm

This is a story about Windows, Samba, debugging, and frustration. I just spent a day and a half debugging a very annoying performance problem with a brand-new system.

Background: I just recently bought a new desktop computer, which I do about every two years. Computers should last longer than that, but I find that, even with good “hygeine”, Windows systems tend to decay to the point where they exhibit weird behaviors after about two years, for which the cure is a complete reinstall of the OS and all applications. (Pause for Mac fanboys to snicker.) The “rebuild the world” process wouldn’t be so bad if it weren’t so hard to migrate all one’s data — even given the fact that a lot of my data is already in subversion. The real problem is that each application sprays its configuration and data randomly around your system, whether in the program install folder, registry, documents and settings folder, local settings folder, etc.

So, I bought a new computer, an extra-quiet one from www.endpcnoise.com. These guys specialize in quiet systems, and since I work in a home environment, the computer is usually the noisiest thing in the room. (Of particular annoyance is the variable speed fan on my existng Dell system, which, every time the system worked up a sweat, made a whiny noise. And we was fined $50 and had to pick up the garbage in the snow, but thats not what I came to tell you about.) Pretty happy with the new system overall.

So, I installed XP Pro on the new system, and proceeded to install all my applications, utilities, and all kinds of groovy things that we were talking about on the bench. And then I got to the part where I tried to use it; specifically, tried to fire up IDEA and build the project I’m working on.

Now, I’ve got a somewhat weird setup; when developing on a project, I checkout a workspace on my Linux server, which is served via SAMBA to my local network, and I run the IDE on my Windows desktop and point the IDE at my samba share. There’s a measurable performance hit vs local, but I like being able to do some things from the Linux command line and other things from the IDE, so overall its a more productive setup for me.

When you ask the IDE to “make” the project, it crawls the files in the project checking their modification times. An “empty” make on a project with ~1000 files generally takes a few seconds to figure out that there’s nothing to do. But when I set it up, the new (faster) system took about 30-60s to do an empty make.

OK, it’s debugging time. What’s different about the two systems? Same OS (XP Pro), same service pack level, both systems up to date on patches, same Java version, same IDE version, same user credentials, no host-specific information in my SAMBA config. Different hardware. Make sure my ethernet drivers are up-to-date. Test network for errors, swap cables, all that. Run IOMeter, found that both get similar throughput for large files on the same SAMBA share.

Crank up perfmon, which tells me that the new system is sending out more packets for a make than the old one. OK, crank up ethereal, get a packet capture, and find that the new system is sending/receiving 10x as many packets for the same operation:

[brian@brian-server ~]$ wc -l /tmp/*cap
258204 /tmp/new-cap
17719 /tmp/old-cap

So, what’s the difference? Let’s look at the packet capture. In the old trace, for each file being probed, it did something like this:

0.467895 192.168.1.104 -> 192.168.1.107 SMB Trans2 Request, QUERY_PATH_INFO, Query File Basic Info, Path: \work\openjfx-compiler\classes\production\openjfx-compiler\com\sun\javafx\api\tree\OnDeleteElementTree.class
0.468041 192.168.1.107 -> 192.168.1.104 SMB Trans2 Response, QUERY_PATH_INFO
0.468283 192.168.1.104 -> 192.168.1.107 SMB Trans2 Request, QUERY_PATH_INFO, Query File Network Open Info, Path: \work\openjfx-compiler\classes\production\openjfx-compiler\com\sun\javafx\api\tree\OnDeleteElementTree.class
0.468402 192.168.1.107 -> 192.168.1.104 SMB Trans2 Response, QUERY_PATH_INFO

Two requests, two responses per file. Seemed reasonable. On the new system, for each file:

2.010471 192.168.1.113 -> 192.168.1.107
SMB NT Create AndX Request, Path: \work\openjfx-compiler\classes\production\openjfx-compiler\com\sun\javafx\api\tree
2.010698 192.168.1.107 -> 192.168.1.113 SMB NT Create AndX Response, FID: 0×2754
2.010900 192.168.1.113 -> 192.168.1.107 SMB NT Create AndX Request, Path: \
2.011011 192.168.1.107 -> 192.168.1.113 SMB NT Create AndX Response, FID: 0×2755
2.011237 192.168.1.113 -> 192.168.1.107 SMB Trans2 Request, FIND_FIRST2, Pattern: \work
2.011570 192.168.1.107 -> 192.168.1.113 SMB Trans2 Response, FIND_FIRST2, Files: work
2.011752 192.168.1.113 -> 192.168.1.107 SMB Close Request, FID: 0×2755
2.011833 192.168.1.107 -> 192.168.1.113 SMB Close Response
2.012025 192.168.1.113 -> 192.168.1.107 SMB NT Create AndX Request, Path: \work\openjfx-compiler
2.012157 192.168.1.107 -> 192.168.1.113 SMB NT Create AndX Response, FID: 0×2756
2.012353 192.168.1.113 -> 192.168.1.107 SMB Trans2 Request, FIND_FIRST2, Pattern: \work\openjfx-compiler\classes
2.012631 192.168.1.107 -> 192.168.1.113 SMB Trans2 Response, FIND_FIRST2, Files: classes
2.012796 192.168.1.113 -> 192.168.1.107 SMB Close Request, FID: 0×2756
2.012897 192.168.1.107 -> 192.168.1.113 SMB Close Response
2.013100 192.168.1.113 -> 192.168.1.107 SMB NT Create AndX Request, Path: \work\openjfx-compiler\classes\production\openjfx-compiler
2.013239 192.168.1.107 -> 192.168.1.113 SMB NT Create AndX Response, FID: 0×2757
2.013445 192.168.1.113 -> 192.168.1.107 SMB Trans2 Request, FIND_FIRST2, Pattern: \work\openjfx-compiler\classes\production\openjfx-compiler\com
2.013894 192.168.1.107 -> 192.168.1.113 SMB Trans2 Response, FIND_FIRST2, Files: com
2.014095 192.168.1.113 -> 192.168.1.107 SMB Close Request, FID: 0×2757
2.014174 192.168.1.107 -> 192.168.1.113 SMB Close Response
2.014355 192.168.1.113 -> 192.168.1.107 SMB NT Create AndX Request, Path: \work\openjfx-compiler\classes\production\openjfx-compiler\com
2.014504 192.168.1.107 -> 192.168.1.113 SMB NT Create AndX Response, FID: 0×2758
2.014962 192.168.1.113 -> 192.168.1.107 SMB Trans2 Request, FIND_FIRST2, Pattern: \work\openjfx-compiler\classes\production\openjfx-compiler\com\sun
2.015169 192.168.1.107 -> 192.168.1.113 SMB Trans2 Response, FIND_FIRST2, Files: sun
2.015339 192.168.1.113 -> 192.168.1.107 SMB Close Request, FID: 0×2758
2.015428 192.168.1.107 -> 192.168.1.113 SMB Close Response
2.015633 192.168.1.113 -> 192.168.1.107 SMB NT Create AndX Request, Path: \work\openjfx-compiler\classes\production\openjfx-compiler\com\sun
2.015764 192.168.1.107 -> 192.168.1.113 SMB NT Create AndX Response, FID: 0×2759
2.015980 192.168.1.113 -> 192.168.1.107 SMB Trans2 Request, FIND_FIRST2, Pattern: \work\openjfx-compiler\classes\production\openjfx-compiler\com\sun\javafx
2.016221 192.168.1.107 -> 192.168.1.113 SMB Trans2 Response, FIND_FIRST2, Files: javafx
2.016402 192.168.1.113 -> 192.168.1.107 SMB Close Request, FID: 0×2759
2.016493 192.168.1.107 -> 192.168.1.113 SMB Close Response
2.016693 192.168.1.113 -> 192.168.1.107 SMB NT Create AndX Request, Path: \work\openjfx-compiler\classes\production\openjfx-compiler\com\sun\javafx
2.016827 192.168.1.107 -> 192.168.1.113 SMB NT Create AndX Response, FID: 0×275a
2.017096 192.168.1.113 -> 192.168.1.107 SMB Trans2 Request, FIND_FIRST2, Pattern: \work\openjfx-compiler\classes\production\openjfx-compiler\com\sun\javafx\api
2.017348 192.168.1.107 -> 192.168.1.113 SMB Trans2 Response, FIND_FIRST2, Files: api
2.017520 192.168.1.113 -> 192.168.1.107 SMB Close Request, FID: 0×275a
2.017590 192.168.1.107 -> 192.168.1.113 SMB Close Response
2.017803 192.168.1.113 -> 192.168.1.107 SMB NT Create AndX Request, Path: \work\openjfx-compiler\classes\production\openjfx-compiler\com\sun\javafx\api
2.017919 192.168.1.107 -> 192.168.1.113 SMB NT Create AndX Response, FID: 0×275b
2.018133 192.168.1.113 -> 192.168.1.107 SMB Trans2 Request, FIND_FIRST2, Pattern: \work\openjfx-compiler\classes\production\openjfx-compiler\com\sun\javafx\api\tree
2.018389 192.168.1.107 -> 192.168.1.113 SMB Trans2 Response, FIND_FIRST2, Files: tree
2.018547 192.168.1.113 -> 192.168.1.107 SMB Close Request, FID: 0×275b
2.018626 192.168.1.107 -> 192.168.1.113 SMB Close Response
2.018779 192.168.1.113 -> 192.168.1.107 SMB Close Request, FID: 0×2754
2.018851 192.168.1.107 -> 192.168.1.113 SMB Close Response
2.019157 192.168.1.113 -> 192.168.1.107 SMB Trans2 Request, QUERY_PATH_INFO, Query File Basic Info, Path: \work\openjfx-compiler\classes\production\openjfx-compiler\com\sun\javafx\api\tree\OnDeleteElementTree.class
2.019292 192.168.1.107 -> 192.168.1.113 SMB Trans2 Response, QUERY_PATH_INFO
2.019495 192.168.1.113 -> 192.168.1.107 SMB Trans2 Request, QUERY_PATH_INFO, Query File Standard Info, Path: \work\openjfx-compiler\classes\production\openjfx-compiler\com\sun\javafx\api\tree\OnDeleteElementTree.class
2.019613 192.168.1.107 -> 192.168.1.113 SMB Trans2 Response, QUERY_PATH_INFO
2.019832 192.168.1.113 -> 192.168.1.107 SMB Trans2 Request, QUERY_PATH_INFO, Query File Internal Info, Path: \work\openjfx-compiler\classes\production\openjfx-compiler\com\sun\javafx\api\tree\OnDeleteElementTree.class
2.019960 192.168.1.107 -> 192.168.1.113 SMB Trans2 Response, QUERY_PATH_INFO
2.020206 192.168.1.113 -> 192.168.1.107 SMB Trans2 Request, QUERY_PATH_INFO, Query File Network Open Info, Path: \work\openjfx-compiler\classes\production\openjfx-compiler\com\sun\javafx\api\tree\OnDeleteElementTree.class
2.020316 192.168.1.107 -> 192.168.1.113 SMB Trans2 Response, QUERY_PATH_INFO

For those of you who don’t enjoy reading packet dumps (I hope that’s all of you), what’s going on here is that it does some sort of complicated multipacket transaction for each level of directory from the project root down to the last directory in the chain, and then does four request-responses for each file. And it repeats the directory stuff for every file, even though it just asked that.

OK, more debugging — what could cause a system to deviate from the standard file system client behavior? Check all the network control panel settings, they’re all the same. Spend several hours googling through the MS knowledge base for file sharing related problems, look at the various registry keys and file versions mentioned, nope, none of them are helpful. Google for people who have had similar problems. Many have, but no one reported a solution that works, except one person, who mentioned that their network behavior changed when they changed versions of Symantec antivirus. Well, I don’t run Symantec AV, but I do run ZoneAlarm. And I do have different versions — ZA Antivirus on the old system, ZA Suite on the new. Seems like a small difference — they’re clearly built on the same base technology — but lets try it. Disabled ZA, reboot, make sure its not running, and run my IDE again — no change. Still an annoying 30-60s delay before it figures out there’s nothing to rebuild.

At this point, I asked my friends for help. Lots of sympathy. Lots of “check this, check that”, but very little advice that actually moved me towards a solution (sorry, guys).

That post about the guy with the Symantec problem gnawed at me, though. I know security programs intercept a lot of network traffic, so the theory was perfectly plausible, and the best theory I had so far. I did the “disable ZA” thing again, rebooted, and cranked up Rootkit Hook Analyzer to see if ZA still had anything hooked, and it did, even though there were no ZA processes running and the ZA TrueVector service was stopped. So, I ran the uninstaller for ZA Suite, rebooted, checked with RHA to see that everything was unhooked (it was), and ran my IDE test again — and this time, sweet success!

So, the conclusion is that the ZA Suite interferes with file sharing client behavior in a rather fundamental way (but one which only has a noticeable affect when dealing with lots of small files).

So, my system is temporarily defenseless against malware while I decide what to do. Why on earth would ZA rewrite the file system client packet stream like that? I want to send them a bill for that day and a half.

June 19, 2007

Remove checked exceptions?

Filed under: software — brian @ 9:07 am

Recently, Neal Gafter mused about whether we should consider removing checked exceptions from Java.  The motivation from this was not what you might expect, but rather an observation that checked exceptions interacts heavily with a lot of other language features, and that evolving the language might be easier if we were willing to consider removing some features.  (Neal knows this won’t ever happen, he’s just trying to get us thinking about Life After Java.)  Not surprisingly, it generated a storm of comments, ranging from “hell yeah!” to “hell no!”.

This isn’t a new topic; it comes around every few years.  A few years back I wrote about the debate surrounding checked exceptions, and the debate continues to rage.  My problem is that I think most of the vocal opponents of checked exceptions are objecting for the wrong reasons (back then, I wrote: “My opinion is that, while properly using exceptions certainly has its challenges and that bad examples of exception usage abound, most of the people who agree [ that checked exceptions are a bad idea ] are doing so for the wrong reason, in the same way that a politician who ran on a platform of universal subsidized access to chocolate would get a lot of votes from 10-year-olds”).

Reading through the against-checked-exceptions commenters on Neal’s blog, we can divide them into three primary groups:

  1. “I don’t like checked exceptions because they’re too much work.” 
  2. “Checked exceptions were a nice idea in theory, but using them correctly makes your code really ugly, and I’m left with a choice of ugly code or wrong code, and that seems a bad choice.” 
  3. “Checked exceptions are a good idea, but the world isn’t ready for them.”  (Frequent refrain from this group: “Man, have you looked at some of the code out there?”)

To the people in camp (1), I say: engineering is hard — get over it.  Error handling is one of the hardest things to get right, and one of the easiest things to be lazy about.  If you’re writing code that’s supposed to work more than “most of the time”, you’re supposed to be spending time thinking about error handling.  And, pay for your own damn chocolate. 

To the people in camp (2), I have more sympathy.  Exceptions do make your code ugly, and proper exception handling can make your code really ugly.  This is a shame, because exceptions were intended to reduce the amount of error-handling code that developers have to write.  (Ever try to properly close a JDBC Connection, Statement, and ResultSet?  It requires three finally blocks.  Ugly if you do it right.  But, almost no one ever does it right.  (The real culprit here is that close() throws an exception — what are you supposed to do with that exception?  But that’s fish under the bridge.)) 

But perhaps there’s a way to not throw the baby out with the bathwater, by providing better exception handling mechanisms that are less ugly.  Dependency injection frameworks did a lot of that for us already, for a large class of applications — and the code got a lot prettier, easier to write, and easier to read.  AFAICS, the two biggest removable uglinesses of exceptions are repeated identical catch clauses and exception chaining. 

The repeated catch clause problem is when you call a method that might throw exceptions A, B, C, and D, which do not have a common parent other than Exception, but you handle them all the same way.  (Reflection is a major offender here.) 

public void addInstance(String className) {
    try {
        Class clazz = Class.forName(className);
        objectSet.add(clazz.newInstance());
    }
   catch (IllegalAccessException e) {
        logger.log(”Exception in addInstance”, e);
    }
    catch (InstantiationException e) {
        logger.log(”Exception in addInstance”, e);
    }
    catch (ClassNotFoundException e) {
        logger.log(”Exception in addInstance”, e);
    }
}

You’d like to fold the catch clauses together, because duplicated code is bad.  Some people simply catch Exception, but this has a different meaning — because RuntimeException extends Exception, you’re also sweeping up unchecked exceptions accidentally.  You can explicitly catch and rethrow RuntimeException before catching Exception — but its easy to forget to do that.

public void addInstance(String className) {
    try {
        Class clazz = Class.forName(className);
        objectSet.add(clazz.newInstance());
    }
    catch (RuntimeException e) {
        throw e;
    }
    catch (Exception e) {
        logger.log(”Exception in newInstance”, e);
    }
}

My proposal for this problem is to allow disjunctive type bounds on catch clauses:

public void addInstance(String className) {
    try {
        Class clazz = Class.forName(className);
        objectSet.add(clazz.newInstance());
    }
   catch (IllegalAccessException | InstantiationException | ClassNotFoundException  e) {
        logger.log(”Exception in addInstance”, e);
    }
}

My compiler friends tell me that this isn’t too hard. 

The other big ugliness with exceptions is wrapping and rethrowing:

public void findFoo(String className) throws NoSuchFooException {
    try {
        lookupFooInDatabase(name);
    }
   catch (SQLException e) {
        throw new NoSuchFooException(”Cannot find foo ” + name, e);
    }
}

Now, the wrap-and-rethrow technique is very effective — it allows methods to throw exceptions that are at an abstraction level commensurate with what the method is supposed to do, not how it is implemented, and it allows you to reimplement without destabilizing method signatures.  But it adds a lot of bulk to the code.  Since this is such a common pattern, couldn’t it be solved with some sort of declarative “rethrows” clause:

public void findFoo(String className) throws NoSuchFooException
rethrows SQLException as NoSuchFooException {
    lookupFooInDatabase(name);
}

The rethrows clause is part of the implementation, not the signature, so maybe it goes somewhere else, but the idea is clear: if someone tries to throw an X out of this, wrap it with a Y and rethrow it. 

An alternate approach to this would be possible with closures and reified generics; it would be possible to write a pseudo-control construct that said “execute this closure but if it throws X, wrap it with a Y and rethrow it.”  Unfortunately, with the current state of generics, we can’t write such a generic method, we’d have to write a separate one for each exception type we want to wrap. 

These approaches focus on the symptom — because the arguments in group (2) are about symptoms.  If we could alleviate the symptoms, people might grumble less.

The people in camp (3) are saying something slightly different.  I don’t really have an answer for them, because what they seem to be saying is that no matter what mechanism you give people for dealing with failure, they won’t follow it.  Checked exceptions were a reaction, in part, to the fact that it was too easy to ignore an error return code in C, so the language made it harder to ignore.  This works on a lot of programmers who are slightly lazy but know that ignoring exceptions is unacceptable, but apparently is worse than nothing for some parts of the population.  (We’d like to take away their coding rights, but we can’t.) 

 Checked exceptions are a pain, and in some frameworks (like EJB before dependency injection), can be really painful.  Once the ratio of “real code” to “error handling code” rises above some threshold, readability suffers greatly, and readability is a fundamental value in the Java language design.  Even if the IDE generates the boilerplate for you, you still have to look at it, and there’s a lot of noise. 

On the other hand, my experiences using third party C++ libraries was even more painful than anything Java exceptions have ever subjected me to.  Virtually no packages ever documented what might be thrown, so you end up playing “whack a mole” when exceptions did pop up — and usually at your customers’s site.  If people are not forced to document what errors their code throws, they won’t — especially the people that the people in camp (3) are afraid of.  As long as those folks are allowed to code, the value we get from checked exceptions forcing developers to document failure modes overwhelms the annoyances.

But, as I said above, I think many of the annoyances can be removed by adding a small number of exception streamlining constructs.  This doesn’t help Neal with simplifying closures, but it does help us get our job done with a little less pain. 

Finally, a meta-note — its really easy to misinterpret the volume of support for “removing checked exceptions” as any sort of gauge for community consensus.  We’re intimately familiar with the pain that checked exceptions cause; we’re substantially less familiar with the pain that they free us from.  (Obviously, neither approach is perfect, otherwise there’d be no debate.) 

June 18, 2007

Living in the information age

Filed under: life — brian @ 7:31 pm

While reviewing my household budget recently, I realized that we had truly crossed into the information age — we pay more for bits than we do for energy.  (By bits, I mean both the infrastructure by which information is delivered to us in electronic form, and the content we purchase; by energy, I’m including only my home utility bills, not gasoline, but since I work at home, I’m guessing my gasoline consumption is lower than average.) 

  • Home telephone (basic line + unlimited long distance): $45
  • Cell phone (mine, including business use): $60
  • Cell phone (rest of family, 4-line family plan): $110
  • NetFlix: $20
  • DirecTV (including TiVo data fee): $75
  • Rhapsody To Go (music subscription service): $15
  • DSL: $30
  • T-Mobile WiFi access plan (reasonable coverage at cafes, hotels, airports): $30

Total: $385/month for bits. 

As to fossil fuels, our combined electric and gas bill average out to around 260/mo. 

June 17, 2007

Tivo + HD — no good choices

Filed under: life, technology — brian @ 1:38 pm

I’ve been a Tivo addict since the first DirecTivo boxes came out.  After getting a big screen TV, the standard definition picture looks pretty bad (especially as it seems that DirecTV compresses the hell out of their signals to make room for more pay-per-view channels.)  So we wanted to upgrade to some sort of HD service, but of course Tivo is a must (no third-party DVRs — no one who has had both a third-party DVR and Tivo has ever said anything good about the third-party DVRs.) 

Option 1: DirecTV’s HD Tivo.  This was released a few years back, but is going to be incompatible with the HD locals that DTV is rolling out, which will be using a different encoding.  (Some people have combined this solution with OTA HD, but I have no interest in playing games with antennas.)  And DTV has yet to roll out HD locals in this area anyway, and there’s little sign they’re coming soon.  So even if there was HD local channels here, there’s no Tivo solution that can record them, only the DirecTV DVR.

Option 2: Digital cable + Series3 Tivo.  This seems like the obvious choice, except for the high cost of the Tivo box (600+, plus the increased cost of the Tivo data service which I’d been insulated from since DTV customers were grandfathered in at the low rates).  But…no TivoToGo or MRV on the Series3 yet, which means you can’t transfer videos to the video iPod.  This has to do with content restrictions surrounding their CableCard certification, but annoying it applies not only to protected HD but also to unprotected SD content.  Ugh.  (As to MRV, if you have two Series3 Tivos with CableCards, why is there a problem moving the content from one Tivo to another?) 

Option 3: Comcast DVR with Tivo.  Comcast did a deal with Tivo where you can get their Motorola DVR and upgrade to Tivo software as it is rolled out.  But its going to be a long time before the rollout reaches here. 

MythTV is not an option; you can’t put a CableCard in a MythTV box. 

So far, no hacks have appeared for the Series3 (other than those that involve reprogramming the PROM and resoldering it) that get around these restrictions.

We’re going to bite the bullet and go with Option 2, and hope that eventually Tivo resolves its dispute with CableLabs and reinstates some form of TivoToGo and/or MRV. 

Newer Posts »

Powered by WordPress