Vincent Gable’s Blog

December 7, 2009

Dot Syntax Solution

Filed under: Cocoa,Design,Objective-C,Programming,Usability | , , , ,
― Vincent Gable on December 7, 2009

Surprisingly, the addition of dot syntax to Objective-C 2.0 has been a major source of controversy. I wonder if there’s some kind of Bike Shed effect at work here: the problem dot-syntax causes is trivial1; while the clarity it brings to code is minor. So it essentially boils down to aesthetics. (For the record, I like the dot, even with it’s current flaws, but I don’t believe it’s worth fighting for).

The Actual Problem

The problem is that when you see a.b = c; you don’t know if it’s:

  1. Assigning the b field of a struct to c. This basically compiles down to one move instruction.
  2. Invoking the -setB: method on an Objective-C object. By convention -setB: should update the b property of the object to hold the value c, and nothing else. But it might have side effects, or be really slow.

A Solution

Using a different symbol to access Objective-C properties would remove all ambiguity. Nobody would mistake a@b = c; as assigning to a C-struct. It’s clearly an Objective-C construct.

But personally, I’m not a big fan of the @ character. It’s ugly; it’s noisy; there’re just too many lines in it. I think U+25B8 ‘BLACK RIGHT-POINTING SMALL TRIANGLE’ would make a better choice,

obj▸property = value;

And since ‘▸’ can’t be part of a valid C identifier, you can basically preprocess your code with s/▸/./, then compile it with existing tools.

Of course, it doesn’t matter what character(s) is picked, so long as it’s clearly different from existing C syntax; and you have a way of replacing it with a . before building it.

1 I’ve heard experienced developers complain that dot-syntax = a steeper learning curve for newbies, and that it can be confusing, but I haven’t actually seen one come out and say ‘I spent X hours debugging a problem that I couldn’t see because of it’. The fact is, any situation that dot-syntax would obscure is already pathological. In the end I just can’t see dot-syntax mattering much.

2 Comments »

  1. The only issue I have with the dot operator is that Xcode seems to be a lot more strict about using unknown property names with ids.

    For instance, if I write

    [myarray objectAtIndex:2].foo = 5;

    I get a warning until I import the class header that defines the foo property, but if I write

    [[myarray objectAtIndex:2] setFoo:5];

    it compiles without complaint.

    Comment by Bergamot — December 7, 2009 @ 5:27 pm

  2. Although I’m generally for liberally dynamic languages, I consider that a feature, not a bug. If I must compile my code, I’d like the compiler to do all it can to catch little mistakes like trying to call a method that doesn’t exist.

    Geeze, is everything about dot-syntax controversial? ;-)

    Comment by Vincent Gable — December 7, 2009 @ 5:47 pm

RSS feed for comments on this post.

Leave a comment

Powered by WordPress