Null References

There’s an interesting discussion going on these days on Ruby blogs about, basically, how to avoid one of the most common, annoying, easy-to-create bugs in any programming language: calling a method on a null reference (or pointer, depending on your language).

This single issue happens all the time, in garbage-collected and non-managed languages, static and dynamic, weakly and strongly typed; you have a handler variable “pointing” to an object, and before calling any methods on it, you’d better be sure that the object is there; you end up using assertions, “if” statements (and all of its variants), boilerplate code all over the place, when everything you want to do is to call that damn method. It’s frustrating, time-consuming and oh so common that we just try to not to think about it anymore. Continue reading

How to Grab (or Capture) your Screen with Cocoa?

Lately I got curious to know how could I grab the entire desktop of my computer, and save it into a file, or display it into an NSImageView component. I started to look around on the web and discovered that:

  • There’s no direct support for that in Cocoa
  • There’s a lot of different ways to do it, both supported and unsupported, cross-processor and not, easy and complicated

I have found several useful resources in my quest, like this one, this other one, and finally this one. But what I wanted most was a complete application to play with, so what I did is to put all the different implementations I’ve found in one single application, called “ScreenshotDemo”:

Amazingly, the approach that seems the ugliest turned to be the most appropriate, that is, using an NSTask instance wrapping the /usr/sbin/screencapture utility. With it, the application feels lighter, easier to maintain, in the true, purest Unix style: using a collection of small utilities, all chained one to the other, is better than having an overbloated tool that does everything, but just bad.

You can just download the (universal) binaries and the source code from the ScreenshotDemo Project page.

By the way, for those that would like to do the same in C#, like me :) just check out this code. It isn’t much easier in .NET, as you can see ;)

And last but not least, here’s how to do it in wxWidgets, explained by Julian Smart, the creator of this incredible library.

A new programming language every year…

Somewhere I read that it was a good thing to learn at least one new programming language every year; I think I have kept up that trend since 1992:

And this year’s winner is: LINQ. The main purpose of learning it is to prepare the LINQ conference in the TechDays next week… and this is huge indeed!

Back from holidays, a quick tip on CocoaMySQL and MySQL 4.1 / 5

I haven’t blogged for a while, having been in Argentina and Bolivia seeing family and friends; now back to Switzerland!

Just a small blog entry for those that have installed MySQL 4.1 or 5 in Mac OS X, and have trouble using CocoaMySQL to use it properly: I found this website where somebody has modified the code so that it works! Thanks!

The problem is that the security mechanism of MySQL has been changed from version 4.0 to 4.1, and many clients, including phpMyAdmin had to be changed to access it.

Xcode 2.2, GCC and Ruby on Rails under Mac OS X Tiger

I wanted to update recently Ruby on Rails to version 0.14.3 (the final 1.0 release seems to be closer than ever) on my Mac, under Mac OS X Tiger 10.4.3 with Xcode 2.2 (the latest version) installed. Well, it turns out to be quite difficult.

First of all, I tried the classic method “sudo gem update” but that won’t work, for many reasons:

  1. To begin with, it seems that Xcode 2.2 changes the default path of the fixrbconfig file; thus, you should do the following to fix it:
    1. cd /usr/lib/ruby/1.8/powerpc-darwin8.0 and then
    2. sudo ln -s /usr/lib/ruby/1.8/universal-darwin8.0/* ./; this is because Xcode 2.2 has some additional support for multi-platform development (“Universal Binaries” as Apple calls them) and thus, some paths have changed internally.
  2. Once you’ve done this, you will re-run “sudo gem update” but it will get stuck when trying to update the mysql gem. To solve this, do the following:
    1. sudo gcc_select 3.3; the default version for Xcode is GCC 4.0.x
    2. sudo gem install mysql — –with-mysql-dir=/usr/local/mysql –with-mysql-include=/usr/local/mysql/include/ –with-mysql-lib=/usr/local/mysql/lib/ –with-mysql-config=/usr/local/mysql/bin/mysql_config; this way, the mysql gem will be compiled using GCC 3.3
    3. Then, last but not least, sudo gem update should finish updating the other gems without problem :)
  3. Finally, you can reset the GCC 4.0 doing this: sudo gcc_select 4.0

Actually I found another way of correcting it (see comment 55), but it applies only if you use fink.

If someone has problems with these instructions, just leave me a comment.

Update, 2005-11-26: Actually I found out that this problem appears when you have installed Ruby in your system using Fink… since I had installed Fink for getting Maven (that I do not yet use) I just uninstalled and removed Fink from my system, then reinstalled Ruby, RubyGems and Rails, and everything came back to normality.

Update, 2005-12-13: This article is much more comprehensive than mine :) check it out! (Local copy if the link is broken)

April Highlight

Even if lately I’m stepping in analysis and architecture tasks, I consider myself to be a developer; as such, I expect next week with high expectations: it will be one of the most important of the year from a technical viewpoint:

  • On Monday, April 25th, Microsoft will release the Beta 2 of version 2.0 of the Microsoft .NET Framework and developer tools.
  • On Friday, April 29th, Apple will release Tiger, also known as MacOS X 10.4, the fifth iteration of its Unix-based operating system, maybe the best operating system available today.

On one side, the Beta 2 of any product, particularly a development environment, is always a huge step forward: Beta 2 means that the interfaces of the APIs are stable (well, subject to minor changes) and that the development teams at Microsoft are concentrating in eliminating bugs and in raising the performance bar.

On the other side, I think that the best operating system just got better. And Apple’s last quarter results (43% more computers sold, 558% more iPods sold) are just a direct result of this commitment to high quality. Continue reading