Search

Archive for June, 2010

Being a web developer for most of my career, I haven’t had much of an opportunity to use InterOp (i.e. API calls to Microsoft Office to provide functionality to another app). But as fate would have it, I’ve been working on a WinForms app as part of a project I’m on now, and I’ve chosen to use some InterOp features instead of a) reinventing the wheel, or b) using some other 3rd party component. There are some drawbacks to using InterOp, mainly that you have to have Office installed on any computer that the software is deployed to. The other main drawback is that you need to use COM for InterOp, which means the APIs can be very confusing, inconsistent, obfuscated, etc.

So I’ve decided to share what I’ve learn. I’ve done 3 major InterOp-dependent features: a spell checker, an HTML RTE, and an Outlook email sender. Here’s the code for the spell checker:

I hope the code I’ve written makes it easier to understand than just plain InterOp calls, but here are some notes that might help:

1. Missing.Value. If you use InterOp, get used to this (unless you are using .NET 4). COM APIs were intended for languages that have optional parameter features (like VB). C# did not get that until the .NET 4 release. So, in C#, you have to put something down for each parameter.

2. I hope you don’t mind my use of KeyValuePair. Again, if this were .NET 4, I’d probably use Tuple. I didn’t really want to create a whole other class just for a private method, so I used KeyValuePair, even though I totally ignore the semantics of ‘key’ and ‘value’. It’s basically just a lazy way to have two return values. Feel free to adjust if it bothers you.

3. This code actually opens up Microsoft Word, puts text into a new Word document, runs spell checker on it, returns the updated text, and then closes Word. The user won’t see the Word document, just the spell checker window, but be aware that Word is still being opened! If you don’t close the Word document, then Word stays loaded, which could cause problems for normal use of Word later.

So, InterOp can seem a little strange, but often it can provide value to an application very quickly. A spell checker feature without InterOp would basically mean using Aspell to do the checking, and probably writing your own spell-checking UI.

I was cleaning out my Twitter favorites today. (I generally use my Twitter favorites as a ‘bookmark’ or a ‘read this later’ device). Anyway, I came across this tweet:

Jeff Atwood tweeting 'god I hate my code. It's terrible. AWFUL. Could be worse, though. Could be PHP.'

This got me a little riled up.

First, the sentiment that his code sucks is one that I can totally sympathize with. I’m very insecure about the code that I write, and I always see room for improvement. I’d refactor and refactor forever given the chance. So, it says something that the creator and developer of one of the most popular and (I think) well-crafted sites on the web (stackoverflow.com) even feels insecure about his code (which I’m sure is much better than he thinks).

Second, Atwood is one of the most outspoken PHP critics on the web. He does understand that building a compelling app is more important than the language it’s written in, but his hatred of PHP still endures.

I’m here to say that PHP, as a language, does not suck. It has flaws, yes, but it is a real, viable language. Here are the things that I think makes PHP an excellent choice for building your next web app:

PHP is a language that is easy to learn. (This is also a downside). Any schmuck can start writing a web app in minutes with some cheap hosting and a text editor. This means value can be delivered very quickly (something that I’m sure Joel Spolsky appreciates).

PHP is arguably the most well-documented and completely documented programming language. The documentation available at php.net is nothing short of astounding. Each function/class has a description, parameters, examples, ‘see also’ links, not to mention a comment thread going back years with examples and tips.

PHP is popular and open source, and therefore it’s a language that is constantly adapting and improving. This can also be a downside: naming conventions and best practices aren’t 100% consistent, stuff gets deprecated, and some people aren’t comfortable with that. Totally understandable.

PHP is a dynamic language with loose types. This means it can be very powerful, and sometimes very tricky. Some people aren’t comfortable with this. I’m not even super-comfortable with it myself. By the way, another language that can be polarizing, JavaScript, is the same way: dynamic language with loose types. Coincidence?

There are great, mature frameworks and platforms for PHP (CakePHP and WordPress, for instance), which I’ve found can be very helpful for reducing the downsides of PHP and magnifying the positives.

So that’s my case for PHP. I don’t think it deserves nearly all the crap it gets, and I think it’s one of the top used languages in the world for good reason.