Moved blog

November 15, 2008 by catphive

I’ve finally gotten to moving my super awesome blog to my web server. This wordpress.com account will now be studiously ignored.

Cardinal Sins of Error Handling

November 11, 2008 by catphive

Java and many modern dynamic languages have the handy ability that you can try to recover almost any kind of error, even null pointer errors.

This of course doesn’t mean you should actually do it.

I see a lot of people using

try {

catch (Exception e) {

//ignore the error

}

Especially on the outer look of a process they want to live for a long time.

This isn’t a good idea for a couple of reasons:

  1. The only sort of exceptions that you really want to catch in java are checked exceptions, and the compiler will actually *force* you to catch these exceptions. Unchecked exceptions, most of which are descended from java.lang.RuntimeException, indicate that something has gone horribly and unrecoverably wrong and that it is time for you application to die.
    If you want your service to live on… just make it restart automically when it dies. This is not that hard. If instead you try to ignore errors that aren’t meant to be recovered from, it’s likely that the next request to your service will fail with a similar error, and again ignore it. Then your service will become a black hole which requests go into, but never return.
    What’s worse, you won’t get any feeback about it, since unless your service actually *dies* errors are likely to go unnoticed in tests.
  2. catch(Exception e) doesn’t actually do what you think it does. If you use this construct a lot, you probably think it catches all exceptions. It does not. There are *at least* three primary kinds of exceptions:
    1. Checked exceptions: descended from Exception, but not RuntimeException. These are meant to be handled by the caller and are part of the function interface through the throw clause. IOException is a common example.
    2. Unchecked exceptions: descended from RuntimeException, which is confusingly descended from Exception. NullPointerException is the most common example. Can be caught with “catch(Exception e)” but should not be caught that way (more on this later).
    3. Errors: Descended from Error. AssertionError is an example. Cannot be caught with a catch(Exception e)

    I said at least because you can come up with your own heirarchy of exceptions by implemting Throwable. The correct way to catch *anything* is with a “catch (Throwable t)”. Of course, you should only do this if you rethrow t because otherwise you can leave your program in an invalid state. Probably this is only useful for logging then rethrowing, or debugging.

  3. Now, suppose you want to catch all unchecked exceptions, but not errors. Unchecked exceptions aren’t really meant to be handled, but Errors are meant to kill your application *for realz*.
    “try {
    f();
    catch(Exception e) {}”
    might seem obvious, but it munges together handling of checked exceptions *and* unchecked exceptions in the same code. If f can throw a checked exception, your compiler will *force* you to handle it, so handle your unchecked exceptions with:
    “catch(RuntimeException e)”
    Then if you also need to handle checked exceptions add additonal checked exception catch clauses for the *specific types* the compiler forces you to handle. For instance, the compiler might tell you to handle IOException:
    “try {
    f();
    }
    catch(IOException e) {
    //you should actually be able to recover from this here
    }
    catch(RuntimeException) {
    //you probably can’t recover from this. At best you can try to ignore it.
    }”
    Even better, you should try to add specialized handles for subtypes of your IOException that you know, perhaps from documentation or experience, that f() might throw.
    “try {
    f();
    } catch(FileNotFoundException e) {
    // handle the exception in a way specific to the file not being found.
    } catch(IOException e) {
    // handle the more general case if you can, or rethrow it in a runtime exception like so:
    // throw new RuntimeException(“I don’t know how to handle this exception!”, e);
    } catch(RuntimeException e) {
    // Probably should just log and rethrow.
    }”

I’m not a big fan of how the Java exception type system works. Having exceptions that don’t descend from Exception, or having exceptions that aren’t called exceptions just seems stupid to me. Still, at least they work better than C++ exceptions, where any type can be thrown (yes I know that you can *handle* any time as well, but still…), and exception specifications don’t force you to handle the exception at compile time (which is why no one uses them).

Safari Books Online

October 11, 2008 by catphive

I’ve been using Safari Books Online pretty heavily at work. It’s a great tool, but there are definitely problems that make it not nearly as useful of a tool as it could be.

Awesome stuff:

  • With a corporate subscription, I can avoid having to ask my manager to expense books, which is nice since I read enough that if I had to pay $50 for a book, it would come out to a pretty big sum on some months.
  • When I want to start reading, I can do so immediately, instead of waiting weeks for amazon to get the book to me.
Awful stuff:
  • The browser based reader is aweful. You can only read one page at a time, and you can’t rapidly flip back and forth between sections, or navigate in any way at a reasonable speed. They need to both increase the serving capacity of safaribooksonline.com so that I can get responses quicker, and build a more responsive UI. They are essentially selling a service that needs to be used as interactively as a desktop application, but only their interface feels as slow and clunky as your average web 2.0 site.
  • Downloads, which I think are only available to corporate users, are limited per month, and have to be downloaded chapter by chapter. I understand limiting the number of downloads per month to prevent piracy. However, 5 seems low. Do people really only read 5 chapters a month?
  • There is no option that I can see to download an entire book, even if you have enough tokens. This is annoying because I don’t want to have to close and open lot sof PDF files when navigating. I want to have nice hierarchical PDF bookmarks of the table of contents like other ebooks do. To be fair, they have that *within* the chapter*, just not *between* chapters.

The *single* biggest problem with safari books online, too big to be dealt with in a bullet point, is that it has too many O’Rielly books, but is missing key books from a number of other publishers.  O’Rielly Books is known for being a proponent of open source, but it is not known for actually putting out good books. Most O’Rielly books are either useless fluff, or stuff you could have gotten from online documentation.

Thankfully safaribooks online also covers a lot of addison-wesley, which are the gold standard in computer books. Still, a number of key books I’ve wanted to read have been missing, so I’ve had to get them from Amazon.

In particular, they seem to be missing most textbooks discussing computer science topics, and instead have tons of buzzword books, and books on a particular technology (again, stuff I can usually pick up faster using online documentation).

Looking over my bookshelf, I can’t help but notice that some of the better books I’ve read/am reading aren’t available through safari::

  1. The GoF Design Patterns book.
  2. Pattern Oriented Software Architecture.
  3. The Art of Computer Programming.
  4. Artificial Intelligence: A modern approach.

Also, just an idea, If Safari really wants to add value to their service, I’d like to see some reformatted RFCs. I don’t want any content subtracted or added, my only problem with RFC’s is that they are published in ASCII and not html. Just format the text more nicely than can be done in ASCII, add some hyperlinks for navigation, and replace the ascii art with nice graphical diagrams, and they would have something that, if not worth paying for by itself, would make me feel better about the whole deal.

Protocol Buffers

August 5, 2008 by catphive

Google has finally released protocol buffers to the world at large. I used version 1 of protocol buffers a lot last summer at Google, and they are totally awesome.

Protocol buffers are a kind of meta file format and meta file format parser if you will. Protocol buffers are a dead easy means of specifying a highly efficient and extensible file format and then automatically generating the code in your favorite language that reads and writes that format. The link has a basic example.

Think of all those tasks that you used XML for, even though you knew it was terribly inefficient *cough* web services *cough*, just because writing a custom binary format and the code to read and write it wouldn’t be worth the effort. Protocol buffers provide a more efficient approach that is just as easy, if not easier, to use. Protocol buffers dump to human readable text for debugging, and the generated parser is significantly easier to use, and more type safe than an XML parser since the parser is generated for *your* data layout specifically.

GPLv3 Chris DiBona and Richard Stallman

July 2, 2008 by catphive

There’s an interesting article up on GPLv3 adoption. The number they provide are actually pretty iffy, especially since they seem to conflate GPLv2 with the “or later” clause as the same thing as a GPLv3 license, which according to my understanding isn’t legally true for most purposes related to the changes from GPLv2 to GPLv3.

The more interesting part is the interviews. They talk to Chris DiBona, who is in charge of google’s open source efforts, about why Google’s code hosting service doesn’t support AGPLv3. DiBona gives a very reasonable defense. Specifically, there are very few people using AGPLv3, and license proliferation is becoming very problematic. There are numerous open source licenses out there that are duplicate, or mutually incompatible, or have wierd requirements that developers who use them don’t know about, and it is getting increasingly difficult to put together a software package that uses different open source libraries without a subtle conflict the developer doesn’t know about.

I’ve met Chris DiBona briefly, and he seemed like a pretty reasonable guy. In contrast, the Stallman interview summarized:

Ernest Park: … Do you have any comments on the GPLv3 site and the progress that we’ve been maintaining?

Richard Stallman: In general, I’m rather unhappy with Palamida, both for terminology (it generally uses the term “open source”, which stands for values I disagree with) …

(refuses to answer question)

Ernest Park: would you mind providing a comment less vague and subjective, focused more on the community acceptance and success of the GPLv3 family of licenses?

Richard Stallman: The free software movement is not merely personal. It is a political movement like the environmental movement, the civil rights movement, etc.

(in other words, no)

Wow.  Not only does he refuse to answer the question, but he goes on to compare himself and his movement to MLK and the civil rights movement, and the environmental movement. Someone should explain to this guy that being famous on the internet doesn’t put you in the same league as Martin Luther King Jr and Ghandi. It puts you in the same league as Tila Tequila and Maddox.

Every single interview I’ve ever seen of Stallman he manages to say something equivalent. I’ve even seen him criticize the One Laptop Per Child project for prioritizing the education of poor children over the advancement of free software. He just strikes me as very egotistical and combative, and the exact kind of guy I’d never want to meet in person.

EDIT: Here’s another example of Stallman’s rhetorical style at its best, taking some digs at Bill Gates for his retirement and some nice third party commentary.

Yet, I rarely see his name come up without being referred to as the leader of the free software movement, or being called a visionary. Is there a real basis for this? What is his vision exactly as it differs from the more common open source model? That all open source developers and users are just as filled with spite and bile as he is?

Anyway, that’s a bit of a rant, but I was kind of ticked off and felt like putting my thoughts down.

Yay for google style guide

July 1, 2008 by catphive

Google published their C++ style guide for the whole world to see. Aside from advocating against the use of exceptions, it is a pretty good primer on how to write clean C++ code. Google probably has one of the cleaner and most consistent code bases.

Other exported google internal stuff to look at is gflags. A rediculously easy way to turn command line options into typed variables. However, it uses static registration, an idiom I’ve become very disenchanted with over time. The listing of options gets enourmous because all linked in libraries also show their options, even if your tools will provide sensible defaults to the library. Still, probably the easiest way to handle command line options for smaller tools.

Japan Pictures 4

June 30, 2008 by catphive

I was in kofu for a couple of days with no internet access so I have a backlog.

Kofu was super nice. I stayed at a very expensive and totally awesome ryokan there for two nights. I’d been hiking through tokyo all week, so I mostly hung out at the ryokan (with a side trip to kofu castle). They had this incredible traditional japanese dinner made from their own kitchen by these old ladies that worked there. It included all sorts of differetn stuff: a single shrimp maybe 7 inches long, really good sashimi, like 5 kinds of meat, fried fish, different kinds of mushrooms, fresh fruit, tofu, and stuff I’d never seen before and don’t know what to call. I wish I’d taken picture.

Kofu is nestled in chubu’s mountains, which are absolutely bueatiful. It is probably only a little bit smaller than seattle population wise, but has a small town feel at least on the outskirts.

It takes too long to add subtitles to images on wordpress, so here’s a quick summary in rough order of the pictures below:

1. Looked at anime crap in Akihabara. They had life sized dolls of asuka that I’m sure brian will drool over. Also astro boy! Atomu!

2. Notice that black guy with a huge pachinko ball afro. Inside this establishment are many people throwing their money away.

3. Manga.

4. The wise and inscrutable Colonel Sanders. He is worshipped as a household god in japan.

5. Electronics crap from akihabara.

6. The advertisement on the front of what I *think* was a house of ill repute right in Akihabara! When I took this photograph, the japanese guys studying the ad jumped out of the way *real quick*.

7. Various statues from the western art museum in ueno.

8. Shrine stuff from Ueno.

9. The night stuff with lots of glowing signs is from Roppongi, and includes Roppongi crossing. Roppingi is basically party central for japan.

10. No!!!!!!!!! Drugs

11.  The super azusa to kofu was awesome, so It took a picture. I got a reserved seat (equivalent to first class on an airline, but since it’s a train it doesn’t cost that much). Almost my entire cabin was empty, I had plenty of leg room, and a chair I could lean way back. Plus, they sell snacks during the trip. This is way different than the tokyo metro trains that are usually standing room only. I’ve been on the trains during the hours when people nearly need to get packed in, and it is *not* fun.

12. Lots of moutains from the train, as well as rice paddies. There are whole towns full of houses where everyone’s backyard is a rice paddy that the family that lives there maintains part time. I’m not sure how this works.

13. Kofu castle was alright, but the main attraction was that after I hiked to the top I got an awesome view of all the surrounding mountains, city, and countryside. I regret I didn’t get to go hiking up the mountains, but it was raining like crazy on and off, and I didn’t want to get caught out there in my short sleaved shirts.

Japan Pictures 3

June 26, 2008 by catphive

I’ve noticed that wordpress seems to be turning my high resolution photos into tiny thumbnails, so I’ll probably have to post these on some photosharing site at some point :(

I hung out in Shinjuku today, shot some pictures of tokyo from a 45 story building, went to a jazz show, and ate at some thai place.

Japan Pictures 2

June 25, 2008 by catphive

Japan Pictures 1

June 24, 2008 by catphive

I bet you all wish you were here. Hahah! Sucks to be you.