Vincent Gable’s Blog

September 25, 2008

Simple Truths About Cross-Platform Apps

Filed under: Design,MacOSX,Programming,Quotes | , ,
― Vincent Gable on September 25, 2008

Scott Stevenson tells it like it is,

Even if Apple recommended cross-platform toolkits for Mac development, the basic premise of Mac software market would not change. Mac users bought the computer they did because they found the experience more appealing. Bringing an application across from Windows with minor tweaks simply won’t resonate with this sort of user.

And gives free advice,

Maybe the most important thing you will ever need to know about Mac development is this:

Mac users will generally favor an app with a better experience over the one with more features.

The full article.

September 24, 2008

I Lost My Phone And Need Ur Numbers!

Filed under: Design |
― Vincent Gable on September 24, 2008

Several times a year, I see some kind of mass-message sent out on Facebook by someone who lost their mobile phone. They want all their friends to reply with their phone numbers, so they can populate their new phone’s address book. This should not be allowed to happen. Your service-provider should automatically backup the address book on your phone for you, so that if you ever lose your phone, your contacts can be put on your new phone before it’s even active.

Blaming the “lusers” who lose their phones really is wrong headed. Even though you can sync your phone’s address book with your computer, it’s too much work for people, especially if they aren’t technophiles. (Hell I don’t do it, and I’ve got my own website.) And I would argue that it’s not even worth the effort! Losing a phone is an infrequent event, and and it’s just too easy to rebuild a social contact list. Even the most technophobic can just ask a few friends for the digits of their common friends. Obviously things like Facebook, email, and google make this process even easier. (And if all else fails, you can start calling the most-common numbers on your phone bill…)

Besides, ever since the dawn of personal computing, it’s been clear that people will not pro-activly take the time to backup their data, even if it’s single most important thing they could do. Engineers need to design around human fallibility, instead of believing they can “educate” people who’ve got better things to do.

What makes this all so bad to me is that the technology to automatically and invisibly safeguard a person’s address book has been here for decades. Whenever a contact is added to an address book, the phone could automatically send an SMS back to the service provider, telling them the name and number of the new contact. The contents of the message would be encrypted for security and privacy. On receipt, the tellco would add this tiny chunk of information to the database they already have on the customer.

And I’m sure the engineers who actually build mobile phones for a living have better ideas for doing this.

XML Parsing: You’re Doing it Wrong

Filed under: Cocoa,MacOSX,Objective-C,Programming,Quotes,Sample Code,Tips | ,
― Vincent Gable on September 24, 2008

There are lots of examples of people using text searching and regular expressions to find data in webpages. These examples are doing it wrong.

NSXMLDocument and an XPath query are your friends. They really make finding elements within a webpage, RSS feed or XML documents very easy.

Matt Gallagher

I haven’t used XPath before, but after seeing Matt’s example code, I am convinced he’s right, because I’ve seen the other side of things. (I’ll let you in on a dirty little secret — right now the worst bit of the code-base I’m working on parses XML.)

    NSError *error;
    NSXMLDocument *document =
        [[NSXMLDocument alloc] initWithData:responseData options:NSXMLDocumentTidyHTML error:&error];
    [document autorelease];
    
    // Deliberately ignore the error: with most HTML it will be filled with
    // numerous "tidy" warnings.
    
    NSXMLElement *rootNode = [document rootElement];
    
    NSString *xpathQueryString =
        @"//div[@id='newtothestore']/div[@class='modulecontent']/div[@class='list_content']/ul/li/a";
    NSArray *newItemsNodes = [rootNode nodesForXPath:xpathQueryString error:&error];
    if (error)
    {
        [[NSAlert alertWithError:error] runModal];
        return;
    }

(I added [document autorelease]; to the above code, because you should always immediately balance an alloc/init with autorelease, outside of your own init methods.)

September 21, 2008

The Graphing Calculator Story

Filed under: Programming | , ,
― Vincent Gable on September 21, 2008

Just read this. It’s easily the most amazing software development story I have ever heard. And yes, I used that software in school.

September 20, 2008

Regex Matching and Filtering in Cocoa With NSPredicate

Filed under: Cocoa,MacOSX,Objective-C,Programming | , , ,
― Vincent Gable on September 20, 2008

Apples documentation of Regular Expressions With NSPredicate has the full scoop, but basically you do
[NSPredicate predicateWithFormat:@"SELF MATCHES regex-here"];. Unfortunately, you can only test if strings match a regex. You can not use an NSPredicate-regex to extract parts of a string. Depending on what you need to do, this may or may not be enough to save the day.

September 18, 2008

I Would Rather Have a Runtime Error Than a Compile Error

Filed under: Programming,Quotes,Reverse Engineering | , , ,
― Vincent Gable on September 18, 2008

And the weird thing is, I realized early in my career that I would actually rather have a runtime error than a compile error. [(some laughs)] Because at that time… now this is way contrary to popular opinion. Everybody wants early error detection. Oh God, not a runtime error, right? But the debugger gives you this ability to start poking and prodding, especially in a more dynamic language, where you can start simulating things, you can back it up… You’ve got your time-machine debuggers like the OCaml one, that can actually save the states and back up.

You’ve got amazing tools at your disposal (in the debugger)… Whereas if the compiler gives you an error that says “expected expression angle-bracket”, you don’t have a “compiler-debugger” that you can shell into…

So, you know, in some sense, your runtime errors are actually kind of nicer.

— An excerpt from one of Steve Yegge’s (long!) talks.

I think there is a real nugget of truth in this. At runtime, you can examine your program’s state, but there is absolutely no way to do that at compile time. Without a debugger, you can’t just look at some nontrivial code and know what the value of x is when there’s an error reading y. (Adding "print x", recompiling, and trying again, would work of course, but that’s just using your compiler as an inefficient debugger!)

Similarly, Strong Typing vs. Strong Testing , essentially argues that some tests can only be made at runtime. (If you read any links on this page, read it, it’s much shorter and to the point).

September 17, 2008

The Price of Cool

Filed under: Design,Quotes | , , ,
― Vincent Gable on September 17, 2008

For those who might doubt such a high value of cool, consider the self-winding Rolex, which sports 1/10th the accuracy of a Timex at 1000 times the price. With Rolex, the technology is grossly inferior, and still people will pay thousands to own it.

Bruce Tognazzini

September 16, 2008

We’re In This Together

Filed under: Design,Security | ,
― Vincent Gable on September 16, 2008

Leaving work late last Friday, I was impressed with the Bosch brand alarm-panel by the door. I botched entering the access-code, trying to arm the system, and the tiny LCD said,

Invalid Code
Let’s try again.

Security systems are designed to keep people out, have Spartan interfaces out of necessity, and consequently are often somewhat hostile to use. It’s a small thing, but that phrasing “let’s try again” made me smile, and that made a difference.

-description, Little-Known Hero of Debugging

Filed under: Cocoa,MacOSX,Objective-C,Programming,Sample Code,Tips | , , ,
― Vincent Gable on September 16, 2008

Or: How to Make NSLog() Useful With Your Objects

Say you have an archaically named NSArray that you want to inspect — it’s easy to do, since NSLog(@"The bestiary is %@", bestiary); prints out the array’s contents

2008-09-16 19:46:06.445 Tester[2678:10b] The bestiary is (
Cheetah,
Pumpa,
Jaguar,
Panther,
Tiger,
Leopard
)

But if you try to NSLog your own object, you get pretty useless output, like

myObject = <MyObject: 0x53f330>

Fortunately, it’s easy to fix! Just implement the method -(NSString*) description; and whatever it returns will be printed by NSLog and GDB (po object, will print object in GDB and the Xcode debugging console).

Here’s an (unfortunately complex) example,

- (NSString*) description;
{
  return [NSString stringWithFormat:@"<%@ %p> = {\n\tquestion=%@,\n\tanswer=%@,\n\tsource=%@\n}", [self className], self, self.question, self.answer, self.source];
}

output:

myObject = <MyObject 0x53eed0> = {
  question=What is the Best Thing Ever Of All Times, Ever?,
  answer=The Internet!,
  source=http://www.cabel.name/2008/01/2007-cabel-yay-awards.html
}

Useful Formatters and such

These macros have made my debugging-life easer.

%p tells NSLog to print the address of a pointer.

-className returns gives the name of a class as an NSString.

Don’t manually print out a Cocoa struct, ever, there are already NSStringTo* functions to do that for you, like NSStringFromPoint().

NSStringFromSelector() works as advertized (and paired with NSSelectorFromString() is very useful in general).

%lld tells NSLog to print a long long (64-bit integer). See also, printf reference.

%Lf tells NSLog to print a long double. See also, printf reference.

Best Practices

Whenever you make a new object, I strongly recommended immediately implementing a description method, and religiously keeping it up to date (it’s not hard, honest!). This won’t fix bugs, but it will make finding some of them much easier.

September 14, 2008

Fast Enumeration Really Is Faster

Filed under: Cocoa,MacOSX,Objective-C,Programming,Tips | , ,
― Vincent Gable on September 14, 2008

Your code will also run faster because the internal implementation reduces message send overhead and increases pipelining potential.

Matt Gallagher

I’ve written before about the advantages of a better enumeration paradigm, clearer code that’s faster to write, shorter, and with fewer bugs.

If you program in Cocoa, you should adopt fast enumeration post-haste.

« Newer PostsOlder Posts »

Powered by WordPress