Friday, April 13, 2007

Ruby OLE

The other day someone came over to my desk and asked me how they could programatically invoke the "Find Computer" function of the Windows shell. Normally you just press CTRL-WINDOWS-F and up pops an Explorer window ready for searching. I didn't have a quick answer for him... so I immediately took up the challenge to figure it out.

After a couple dead ends I started looking at Windows Script Host (WSH). It allows you to write VB or JavaScript to access the ActiveX API of a Windows application. After a bit of googling I found the "Shell.Application" documentation on MSDN. After futzing around with Javascript for a few minutes I started thinking that I should be able to do the same things with Ruby. So with a little more searching I discovered that by adding "require 'win32ole'" at the top of my ruby script I could instantiate an instance of "Shell.Application".

The only problem left was finding the right API call. A three line Ruby script like this gave me a list of methods:

require 'win32ole'
wsh = WIN32OLE.new('Shell.Application')

wsh.ole_methods.collect{|m| m.to_s}.sort.each{|m| puts m}


A quick change and my final script looked like:

require 'win32ole'
wsh = WIN32OLE.new('Shell.Application')

wsh.FindComputer()


or in JavaScript:

wsh = WScript.CreateObject("Shell.Application");
wsh.FindComputer();


If you want to take this scripting stuff to the next level I suggest you go check out David Mullet's "Ruby on Windows" blog. He describes some of the same things I just demonstrated above and goes into a lot greater depth about how to use Ruby to script think like Word and Excel.

And in case you're wondering why I had to use Ruby to get a list of methods instead of just looking it up in MSDN, well just try looking at the MSDN page I referenced above with Firefox instead of IE.... That's right... the site navigation only works correctly in Internet Explorer.

Tuesday, April 10, 2007

It's spelled S-Q-L

Why do so many Java developers suck at SQL? Are they just born that way? Are they brainwashed? Do they think that Hibernate will just do it all for them? What's the problem?

The reason I ask is that I'm working on a Java system where the developers obviously thought nothing of letting Hibernate lazily instantiate every relationship as they iterated through collections and navigated down relationships. Not once did they stop to consider the SQL that was being thrown across the network to the database or the performance penalty that it would incur. Just a couple weeks ago I turned on Hibernate's SQL logging just to see 631 queries go flying by in order to prepare the data for a single JSP. WTF?! 631 Queries!!?

Then today I saw another atrocious example of bad code that instantiated several collections of some very large classes from the database to just throw them all away after navigating through them to get a count. Whatever happened to count(*)? In the end I replaced hundreds of lines of convoluted Java code with about twenty lines of SQL. Yes SQL, not HQL (which rocks BTW), but just wasn't appropriate for this task.

Perhaps it's unfair of me to pick on Java developers but for one reason or another a lot of the Java developers I run into have this resistance to leveraging the relational query engine at their disposal. They don't strike a good balance between the object oriented and relational worlds that their systems stride. Use the right language for the right task. And if you can't determine when to turn to SQL, turn on SQL logging and watch what the heck is going on. When you see SQL tearing by, just pause for a moment and ask yourself if there's a better way to do what you want. And remember you have decades of querying technology just waiting to be used. Don't be afraid of writing an elegant query.

Tuesday, April 03, 2007

Prototype, Sitemesh and Struts

The Java environment for my current project is stuck at Java 1.4, but that doesn't mean I have to build my webapps like it's 2002 (that's when 1.4 was released). As a matter of fact I decided to take some lessons from Ruby on Rails.

First of all I decided to ditch Tiles in favor of Sitemesh. Tiles gets the job done but at the expense of a little too much abstraction for my taste. Sitemesh uses the decorator pattern and just simply feels a whole lot cleaner. While not as simple as dropping an RHTML file into a "layouts" directory, it's certainly a whole lot easier than the XML config needed for Tiles.

Secondly, I use DispatchAction to get multiple "action" methods in one Struts Action subclass. It's nothing I haven't done before but keeping the number of classes down is a good thing.

Finally I decided I liked the way RoR uses Prototype and Scriptaculous to provide a little more responsiveness to my webapps. So I decided to jump into Javascript and use Prototype in my Struts webapps. I had to configure Sitemesh to leave my AJAX responses alone and I needed to read a bit of the Prototype docs but it was pretty straightforward. I used the Rails partial convention to let my Java app generate HTML fragments through a Struts action and a JSP and then let my Javascript function replace a piece of the DOM with the result of the AJAX call.

When given lemons, make lemonade. When given ancient Java technology, spice it up with the techniques you learn by looking at alternative implementations.

Objective-C 2.0

Being a bit of a Mac-head and programmer to boot I've taken various looks at writing native Mac OS X apps withe XCode (the IDE), Cocoa (the frameworks), and Objective-C (the language). In general I've come away relatively happy with the toolset. But I must admit, I've filed my own share of enhancement requests with Apple. The top of my list was refactoring support in the IDE. I know it probably sounds lame to the hard core coders but after developing with Java for so long I just expect it and perhaps more embarassingly I feel I need it. Global regex search and replace just doesn't cut it. Secondly, I dislike all the boiler-plate "noise" that appears in your code to declare properties (instance var & accessor/mutator methods) and all the manual memory management.

Well Apple's been looking around at the competition and listening to feedback and judging by their website (here and here) and a few message boards (like this one), Apple has a lot in store for developers. Objective-C 2.0 sports Garbage Collection for the first time, some syntactical sugar for iterating over collections, and a nice simple way of declaring properties. XCode provides refactoring (YAY!) and more enhancements to keep your eyeballs on the code while editing and debugging. And even Interface Builder got cleaned up a bit and exposes nice simple ways of using core animation in Leopard. I'm looking forward to seeing it all in action.

BTW: I read on the RubyCocoa mailing list a few months ago that it might be bundled with Leopard too. Writing Mac OS X apps with Ruby... very cool... Have to wait and see if that one happens.

The New C#

About a year and a half ago I blogged about LINQ (Language INtegrated Query). Anders Hejlesberg starred in a video demoing object/xml/database query functionality and some new language features that Microsoft was playing with.

It's been in the oven for quite a while so it should be just about done, and as a matter of fact it looks like C# 3.0 is scheduled to be released this year. I suggest checking out this MSDN site to read the details for yourself or watch some of the videos. Things like implicitly typed local variables, extension methods, lamda expressions, and anonymous types all seem to be inspired by the features of the so-called scripting languages like Ruby, although Microsoft goes out of their way to make sure you don't confuse the similarity in syntax with the underlying implementation (and with good reason).

In short I'm still quite impressed. I've seriously looked at C# in the past and mostly dismissed it as a Java clone. But with these new features I think it's finally coming out from under it's Java shadow and beginning to shine on its own.