Programming

Debugging software for laymen

November 28th, 2009  |  Published in Programming

Brent Simmons provides what is possibly the best description for laymen of the joy/agony of debugging software that I’ve ever seen:

If it took 10 minutes to reduce memory usage by 96MB, then — were there a linear relationship — it should take under half a minute, less than 30 seconds, to go the rest of the way.

That’s the way things work in the real world, after all. If you have 100 bags of leaves to carry out to the curb, each bag will take about the same amount of time as the others.

Instead, it will probably take me about two hours, maybe more, to get rid of that last 4MB.

It’s almost as if you carried out 96 bags of leaves easily and quickly, then realized you can’t get those last four bags, even though they’re exactly the same as all the others, without pouring a new driveway first.

Which is crazy, right? If the real world operated like that all the time, we’d go completely nuts.

OCPython, anyone?

December 5th, 2008  |  Published in Apple, Macintosh, Python

As dynamic scripting languages become more widely used, many developers are interested in using them to write “native” Mac OS X software, rather than having to learn Objective-C (which, of course, is a dynamic language in its own right). And in fact it is often possible to do so, using Apple’s BridgeSupport to generate the necessary metadata. Apple explicitly supports bridges for Ruby (RubyCocoa) and Python (PyObjC).

Nevertheless, there is necessarily an impedance mismatch between the runtime models — when passing objects to Cocoa, the bridge must convert the object from the dynamic language’s runtime to an Objective-C object derived from NSObject, and vice versa. This introduces a certain amount of inefficiency, which is not normally a big problem, but it would be desirable to avoid it.

Now, over the last few months, I’ve seen a variety of projects to do this by using the Objective-C 2.0 runtime to implement the object model for the dynamic language. In other words, objects in the dynamic language are subclasses of NSObject and no conversion is necessary.

These range from the venerable F-Script, which is even closer to Smalltalk than Objective-C, to Tim Burks’ original language Nu, which uses a Lisp-like syntax. I believe I’ve seen a reference to a JavaScript implementation as well, but I can’t seem to find it now.

The project that seems to be getting the most buzz in the Mac community right now is MacRuby, a port of Ruby 1.9 done by the developer of RubyCocoa (who is also an Apple employee). There was even a article about it featured on the Apple developer site recently.

Conspicuous by its absence from this list is my preferred dynamic language Python. I would love to see an implementation of Python hosted on the Objective-C runtime. I’ve always felt Python’s language model was a good match for Cocoa (of course, PyObjC not only predates BridgeSupport but even Cocoa itself, going back to the days of NextStep), and I’m personally more familiar with it than Ruby or other dynamic languages. So it seems like a logical step to take.

This project couldn’t be called MacPython, because that name has already been used to denote the Mac port of Python. For now I’m calling it “OCPython”, but I’m certainly open to a cleverer name (especially if there’s one that can riff on IronPython; unfortunately the element most associated with Mac OS X is Carbon, which is entirely wrong for this project).

I’ve been thinking about which version of Python to use as a jumping-off point; originally I was leaning towards 2.5 because it’s the version shipped with Leopard, but now I’m persuaded by Bill Bumgarner’s comment to use 3.0 and take advantage of the revised, less crufty implementation. (This is based on the stated goal of the 3.0 development project; I haven’t actually looked at the source code yet.)

I wish I could say I’m prepared to get the project off the ground myself, but right now I’m between jobs, as they say, and given the current financial situation I’m actively looking for full-time employment and can’t dedicate much time to something that won’t bring in any revenue. (Of course, if anyone out there would like to fund development of OCPython, contact me immediately.)

Visual Studio 2005: Not Ready To Lead

October 26th, 2008  |  Published in Programming

Glenn Howe writes of an anomaly in Visual Studio 2005’s C++ compiler:

… I discovered today (Friday) that if you compile something like:

double x = (false) ? 0 : 1.4;

that x will not equal 1.4 as most people (and the gcc compiler) would think, but rather it will equal 1.0. Why? Because it sees 0, interprets it as an integer and decides that if both halves of the : have to have the same type, then that type will be integer. The fact that this is in the middle of an assignment to double means nothing.

Yes, in fact it does mean nothing; the type of the lvalue on the left-hand side of an assignment expression has no effect on the type of the rvalue on the right-hand side.

He goes on:

I’m not even saying that Visual Studio is wrong. It’s different from gcc which leads to platform specific bugs, …

Well, I was a language lawyer in a previous life, so I can’t let it rest there. (And those of you out there saying “Of course Visual Studio is wrong!” should recall that gcc doesn’t always get it right, either.)

Where were we? The type of the conditional operator, right. To find what that type should be, we have to look at the definition of the operator.

In C99, this is pretty straightforward. Section 6.5.15, which defines the conditional operator, says the result of the expression “is the value of the second or third operand (whichever is evaluated), converted to the type described below”. Looking “below”, it further says when you have two operands of arithmetic type, the result type is the type that would be determined by the “usual arithmetic conversions” applied to the two operands.

Now a simple jump to section 6.3.1.8, which defines the “usual arithmetic conversions”, tells us: “… if the corresponding real type of either operand is double, the other operand is converted, without change of type domain, to a type whose corresponding real type is double.”

OK, so that’s not completely straightforward, talking about “real types” and “type domains”, but basically we have two numbers, and if one of them is double the other is converted to double, and so that’s that; the type of the expression should be double, as we’d expect, and so Visual Studio 2005 is wrong.

If we’re in C.

C++ is more complicated.

(What a shock, right?)

Well, C++ has to allow for the possibility that either or both operand is a class type, and if so can it be converted to the other’s type, blah blah blah (section 5.16, if you’re following along at home).

Since we don’t have class type operands, we go on, applying the “standard conversions” to the operands (none of which apply here), and then, if “the second and third operands have arithmetic or enumeration type[,] the usual arithmetic conversions are performed to bring them to a common type, and the result is of that type.”

Hey, that was pretty simple after all. No, wait: are the usual arithmetic conversions the same in C++ as they are in C? Good question; let me see… (flip, flip, …) here we are, clause 5: yes, they are.

So from where I sit, Visual Studio 2005 is wrong in C++, too.

Wrong on conditional operators.

Wrong for America.

(I can’t wait for this election to be over…)

Micro-huh?

April 3rd, 2008  |  Published in Programming, iPod/iPhone

As reported by Macworld: “Exec touts developing iPhone apps without SDK”:

There are more ways to develop applications for the Apple iPhone device than using the company’s beta iPhone SDK. Through a combination of Microsoft and other technologies, developers can build a Web application for the iPhone, according to a speaker at the VSLive conference in San Francisco on Wednesday.

How would you use Microsoft technologies to make an iPhone application? Oh, right: VSLive = Visual Studio Live. Got it. Know your audience.

“Don’t worry about rubber-tired vehicles,” said a speaker at this week’s blacksmith convention. “They can’t take you anywhere a well-shod horse can go.

“And besides, they won’t let you make your own tires, which I find kind of offensive.”

Little details mean a lot

March 4th, 2008  |  Published in Macintosh, Programming

I’ve just filed my 2007 taxes using the much-maligned TurboTax. (I filed my 2005 taxes this past January, so I’m doing much better this year.)

TurboTax for the Mac appears to have been completely rewritten with an eye towards satisfying the Mac customer, which is a refreshing change for Intuit. (It will be interesting to see how their Financial Life product looks this fall.)

As a good example of leveraging the foundations of Mac OS X to provide specific functionality for the customer: I was pleasantly surprised to be given these choices for saving my return after filing:

  1. “Print returns” (I routinely “print” to PDF, but Mac OS X already makes this easy);
  2. “Backup to .Mac”: this puts a copy of my TurboTax data file, as well as a PDF with my return and all supporting worksheets, into a folder (Documents/TurboTax/2007) on my .Mac disk;
  3. “Burn to CD”: this uses the standard disc burning interface to put a copy of my data file as well as the TurboTax application (fully updated, as far as I can tell) onto a CD. Brilliant. (I can’t tell you how much time I wasted looking for the 2005 software. The fact that it wouldn’t run under Leopard once I found it is another issue entirely…)

These are the little details that Mac users expect in their software (otherwise they wouldn’t have bought a Mac), and companies that pay attention to things like this should be able thrive in the Mac marketplace. Granted, Intuit’s reputation on the Mac is in tatters (and deservedly so), but I’m much more inclined to check out Financial Life now than I was before.

Dancing with the elephant (revised)

November 3rd, 2007  |  Published in Apple, Macintosh, Programming

While gathering information about application compatibility with Leopard, I notice one developer, Snerdware, is struggling to keep up with the situation. They report two major problems that affect their current applications, and, with some evident frustration, blame both of them on Apple. [Note: I've substantially rewritten my commentary on the first issue, since I've learned additional information and since my main point applies to the second issue.]

The first issue affects both their products; the report here is for AddressX:

AddressX won’t startup on 10.5.0/Leopard running on an Intel-powered Mac (a log indicates “… Reason: no suitable image found. Did find: /usr/lib/libcrypto.0.9.dylib: mach-o, but wrong architecture. …”).

When we looked at the 10.5 pre-release, we encountered an OS X library issue — it installs a non-universal/PPC-only version of a library that’s critical to our applications (and, of course, all our developer systems are Intel-powered). ‘Though we filed a bug report early in October (original Problem ID: 5520955 and have now re-filed it), believe it or not, it’s still a problem with the released/commercial version of 10.5.0 (even without the bug report, you’d think that a check to ensure all binaries are universal would actually be a basic QA step — it’s one that’s easily automated!).

After corresponding with someone named Bryan D. at Snerdware and doing a little more research, I’ve learned the following things which make me more sympathetic to their problem than I originally was:

  • Apple includes two versions of libcrypto: one called libcrypto.0.9.dylib, and one called libcrypto.0.9.7.dylib, and a symbolic link libcrypto.dylib that points to the newer library.
  • In Leopard, the newer library is indeed a four-way universal binary, but the older library is PowerPC only. I originally assumed this was because only pre-Intel applications would require it, since libcrypto.0.9.7.dylib has been available in Mac OS X at least as far back as 10.3.9; and so Snerdware was making a mistake by linking to the older library and then complaining that it wasn’t universal.
  • But it turns out that Snerdware relies on features that are present in the older library (which corresponds to libcrypto version 0.9.6l), but have been removed for some reason from the newer library.
  • More strangely, Apple did include a universal binary of libcrypto.0.9.dylib in the Intel versions of Tiger, but strangely left it PowerPC-only in Leopard. Huh?
  • Snerdware originally considered compiling the older library into their program, but since it’s crypto there are all kind of export regulations that come into play (and believe me, I know about this; I had to research exactly this topic at a previous job).

Hopefully this was indeed an oversight that can be fixed in a future update; otherwise, Snerdware has a problem with no easy solution, and some portion of the blame lies with Apple.

The second issue is a more fundamental problem, and one that affects nearly all Mac developers — Apple has a history of making significant changes to core system services between OS versions. I presume that Apple doesn’t do this maliciously, but there are plenty of developers who will tell you they’ve been burned by Apple changing or dropping technologies. Apparently Snerdware is once burned and twice shy:

Since we’ve previously been seriously “bitten” by Apple’s last-minute major changes to developer pre-releases, we can’t afford to take pre-releases seriously until they are near release. [...]

With the imminent release of 10.5.0, we [...] discovered that, even ‘though OS X’s Sync Services has the same interfaces and we’ve seen no documentation/release notes that document subtle but significant changes in behavior, we see that the behavior has changed in a way that will cause us to make very major changes to Groupcal … Given that Groupcal was working very well with 10.4, this is more than annoying for us, as well. Be angry with Apple, not with us.

[...] It’s things like this that make it much more difficult for an OS X product to be a viable business proposition.

Here I have a harder time finding sympathy. By their own admission they waited until the last minute to check whether the behavior of Sync Services had changed, and now they report to their users that their flagship product won’t be compatible until the first quarter of 2008, and point the finger at Apple?

(Luckily their target market appears to be corporate workgroups running Exchange servers, and those folks are less likely to be rushing out and upgrading to Leopard, so Snerdware may have some time there.)

If my livelihood depended on my product operating correctly with Sync Services, I wouldn’t rely on Apple keeping its behavior unchanged from release to release; I’d be booting up each Leopard seed on a non-critical system and checking things out. Perhaps I’d muster up the resources to send a developer to WWDC, where Apple encourages you to bring your code, try it out on the current seed, and discuss problems with the Apple engineers who have come up from Cupertino for the week for just this reason.

(To be fair, I don’t know that Snerdware didn’t do this; but with the “can’t afford to take pre-releases seriously” comment above, I somehow doubt it.)

And then — well, maybe Sync Services does change in the September seed and I still have a lot of work to do — but wouldn’t I’d be five weeks further along?

If you choose to dance with an elephant, you can approach it one of two ways — you can wait for the dust to settle, and then see what the lay of the land is; or you can try to be more nimble and maneuver around the elephant. We independent developers are supposed to be more nimble… aren’t we?

[But sometimes, even if you're nimble, you can get still stepped on...]

Ruby vs. Python (take 147)

July 25th, 2006  |  Published in Programming, Python

Tim Bray, a new student of Ruby, admires it for its readability, and compares it to Python:

In theory, Python ought to do better, lacking all those silly end statements cluttering up the screen. But in practice, when I look at Python code I find my eyes distracted by a barrage of underscores and double-quote marks. Typography is an important component of human communication, and Ruby’s, on balance, is cleaner.

John Gruber concurs:

Now that I think about, those underscores and extra quotes are exactly why Python does not appeal to me. I find Python’s indentation-as-block rule to be quite elegant, but its use of punctuation feels clumsy.

As a Python programmer (but not a Ruby programmer), I have to say:

Huh?

I respect both Bray and Gruber, but I don’t see their point. At all.

When I look at my Python code, I see underscores in two contexts. The first is in the names of “special” methods and attributes. In practice for me, this is largely limited to __init__, since I rarely need to overload operators. (And this syntax makes such identifiers stand out as “special”. Who’s to say that this is less attractive than Ruby’s use of $ to mark global variables? Are underscores uglier than dollar signs?)

The second, more common use of underscores in my code is in long Cocoa method names, because by convention in PyObjC they replace the colons in the Objective-C selectors. Those underscores are generally acknowledged as ugly by the PyObjC community, but accepted as a necessary evil. I suspect Tim Bray is not writing PyObjC code. (Maybe he’s writing more special operators than I am.)

As for quote marks (single or double), the only “barrages” I can think of are in multi-line string literals, which are frequently used as documentation strings (or comments, effectively). I suppose this criticism might come from the common use of string literals as symbols — e.g., as keys in dictionaries (hash tables) — where Ruby has an actual symbol type, whose syntax is borrowed from Lisp keywords (a prefixed colon).

Whereas if one wanted to criticize a language for “clumsy” punctuation, one might point to any of the fourteen-odd uses of non-alphanumeric symbols in Ruby found here: marking different kinds of variables with $, @, @@, and &; four kinds of “quoting mechanisms” with %X{..}; etc. (Is that Perl I hear calling?)

Perhaps that’s why neither Bray nor Gruber commented on the controversial use of @ in Python to mark the user of a decorator function.

So I have to say I just don’t get it. To each his own, I guess. I see that Ruby is getting a lot of buzz these days, in large part because of the success of Rails, and that’s fine; if I had to throw together a quick web application I might investigate Rails too. Perhaps it’s just that I came to Python first and don’t really know Ruby very well.

Still, to me, saying Ruby’s use of punctuation is “cleaner” than Python’s is (to paraphrase Gruber) like criticizing the iPod because it doesn’t come in white…

PyWikit

June 22nd, 2006  |  Published in Macintosh, Programming, Python

In response to a challenge in the Mac forum at Ars Technica, I’ve banged out a quick service that adds “Search With Wikipedia” to your Services menu (right below “Search With Google”). It’s called PyWikit because (a) somebody suggested “Wikit” for the name and (b) I wrote it in Python, using PyObjC.

To use it, download it and move it to your ~/Library/Services folder. At the moment you will probably have to log out and log in again to update your Services menu. If I get motivated I’ll build an installer, someday. (Unlikely since I’ve only spent about an hour on this, most of which was taken up building a universal binary for PyObjC. While it is true that PyObjC rocks, its universalness is still a little, uh, rocky.)

SubEthaEdit for $0 is a great deal

April 25th, 2006  |  Published in Macintosh, Programming

SubEthaEdit from CodingMonkeys is an excellent collaborative editor that I’ve used to help create session notes at past PyCons. Many people also use it as a programming editor, although to date I’m still in the BBEdit camp.

Now, as part of BLOGZOT 2.0 on MacZOT.com, MacZOT and TheCodingMonkeys will award $105,000 in Mac software — specifically, free SubEthaEdit licenses (if enough bloggers link back to the web site to reduce the license price to $0).

Obviously, the intention is to drive a lot of visits to the MacZOT site, but SubEthaEdit for $0 is a great deal no matter how you slice it. (And MacZOT is an interesting idea itself; I’ve used it once or twice to spring for software I might not have otherwise bought, when the discount pushed the price down to impulse buying levels.)

(Thanks to Matt Deatherage for the heads-up.)

Update: Yes, enough bloggers fell for the offer to drive the cost down to $0, and I got my free license code over the weekend — thanks, MacZOT and Coding Monkeys!

New Tiger window style documented… sort of

June 20th, 2005  |  Published in Macintosh, Programming

The new “unified title and toolbar” window style I previously commented on has now made an appearance in Apple’s documentation. Unfortunately, it just tells how to activate the feature from Carbon. We’re still waiting guidance on when to use it…