Static typing, no workarounds

A simultaneous benefit and curse of loosely-typed languages (Perl, Ruby, Javascript, etc) is that a programmer can do just about anything with a third-party library. The ability to “fix” into other people’s code running in the same process is sometimes called “monkeypatching”.

Stricter languages, like Java, make it dramatically harder to accomplish the same goals. This is intentional, because Java also allows sandboxing of untrusted code in the same code space (pretty much impossible in existing dynamic languages despite efforts).

But what do you do if there is a problem with the third-party code and you can’t change it? In Java, you may just be screwed. For example, the Apache River distributed computing SDK uses “throws java.rmi.RemoteException” as a hint about which server methods are allowed to be invoked from the client. But, oops, Android’s Dalvik VM omitted all of the java.rmi.* classes to save resources. So trying to load a class with that “throws” declaration causes a “NoClassDefFoundError”. Because there’s no way to inject a third-party class into the “java.*” packages space, and Android developers have not been willing to add this simple Exception to Dalvik, this effectively kills all reasonable hopes of using River on Android. The River folks even talked about extreme hacks of rewriting the classes on load, but that’s too impractical. The answer from Android fans in scenarios like this seems to be “Why do you want to use that library anyway?

A dynamic language would have just created a stub for that exception if it didn’t exist. Is that better or worse?

JsChilicat: headless Javascript unit testing

One of my Avid colleagues started a Sourceforge project he calls JsChilicat to simplify automated unit testing of Javascript code. The tool invokes the Rhino Javascript engine with the EnvJS and [QUnit][qunit] frameworks on the JS side. EnvJS emulates the browser’s DOM to trick your Javascript into thinking its working in an actual browser. JsChilicat outputs JUnit-compatible XML, including console.log() output. It also does code coverage reports, but I haven’t played with that feature yet. To the best of my Google-fu, this is the only headless Javascript test kit that does code coverage.

The result is brilliant, but a little rough (ExtJS and EnvJS don’t play nice, for example). The documentation is currently insufficient, but just barely so. One or two examples of usage would be enough to reduce this to a half-hour learning curve.

I have no idea where the name comes from… Maybe it’s a pun in German or something? 🙂

Adding NDEBUG to the Xcode Release target

Programmers often want to put behavior in programs that only happens for debug builds, like logging or other diagnostics. A common convention in C is to surround your debug-only behavior with “#ifndef NDEBUG … #endif”. The double-negative is ugly, but it’s a widely used convention nonetheless.

Xcode doesn’t use this convention by default, so you need to add “-DNDEBUG” to your Release compiles to turn off the debugging behavior. To do this, Get Info on your project, go to the “Build” tab, select the “Release” target and edit the Preprocessor Macros line like in the following screenshot.

Screenshot showing Xcode project configuration dialog

(Credit: Greg Parker)

Why Skype fails to save settings

A few times when I upgraded Skype on Windows, it went into a mode where the settings would not stick. I’d have to reset them (like preferring my headset over my speakers) each time I launched Skype. I’m not the only one who experienced this — apparently Skype has an intentional “memory-only” mode where it will not save settings to disk. I don’t know why upgrading triggered the misconfiguration, but here’s the workaround.

The fix is to change this registry setting from “1” to “0”:

[HKEY_LOCAL_MACHINE\Software\Policies\Skype\Phone]
"MemoryOnly"=dword:00000001 

kqueues

FreeBSD (and thus Mac OS X) has a filesystem notification API called kqueues which allow you to autodetect changes in watched files/folders. Apple uses it behind the scenes for indexing (Spotlight). It’s really fast. If I modify a file in one window via “touch /tmp/foo.txt” then the watcher program prints a message in the other window before the touch command exits. I’m using the Obj-C UKKQueue wrapper to simplify access to the kqueue system call.

I’ve found a few cases where the watcher fails to trigger when I rapidly delete and recreate the file… I’m not sure if it’s a bug in the system or in my app (probably the latter, maybe I’m not processing the input fast enough).

[UPDATE: oh, I just learned that kqueue can only watch a file that exists. If I wanted to know when a file was created, I’d have to watch the parent directory and then start watching the file when/if it was created. So that explains my problem with recreates]

In OS X 10.5, Apple added a new FSEvents API which uses a daemon to do a system-wide kqueue for the whole filesystem and distributes the messages to listeners in user space. I expect this is probably to reduce the kernel load of a pile of apps each requesting notifications for the same files.

My current project is targetting 10.4, so I’m avoiding the 10.5-specific FSEvents API.