Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Let me start off by saying that I started programming in Visual/Real Basic, then moved on to Java, so I'm fairly used to dot syntax. However, when I finally moved to Objective-C and got used to brackets, then saw the introduction of Objective-C 2.0 and its dot syntax, I realized that I really don't like it. (for other languages it's fine, because that's how they roll).</p> <p>I have three main beefs with dot syntax in Objective-C:</p> <p><strong>Beef #1:</strong> It makes it unclear why you might be getting errors. For example, if I have the line:</p> <p><code>something.frame.origin.x = 42;</code></p> <p>Then I'll get a compiler error, because <code>something</code> is an object, and you can't use structs of an object as the lvalue of an expression. However, if I have:</p> <p><code>something.frame.origin.x = 42;</code></p> <p>Then this compiles just fine, because <code>something</code> is a struct itself that has an NSRect member, and I <em>can</em> use it as an lvalue.</p> <p>If I were adopting this code, I would need to spend some time trying to figure out what <code>something</code> is. Is it a struct? Is it an object? However, when we use the bracket syntax, it's much clearer:</p> <p><code>[something setFrame:newFrame];</code></p> <p>In this case, there is absolutely no ambiguity if <code>something</code> is an object or not. The introduction of ambiguity is my beef #1.</p> <p><strong>Beef #2:</strong> In C, dot syntax is used to access members of structs, not call methods. Programmers can override the <code>setFoo:</code> and <code>foo</code> methods of an objects, yet still access them via <code>something.foo</code>. In my mind, when I see expressions using dot syntax, I'm expecting them to be a simple assignation into an ivar. <em>This is not always the case.</em> Consider a controller object that mediates an array and a tableview. If I call <code>myController.contentArray = newArray;</code>, I would expect it to be replacing the old array with the new array. However, the original programmer might have overridden <code>setContentArray:</code> to not only set the array, but also reload the tableview. From the line, there's no indication of that behavior. If I were to see <code>[myController setContentArray:newArray];</code>, then I would think "Aha, a method. I need to go see the definition of this method just to make sure I know what it's doing."</p> <p>So I think my summary of Beef #2 is that you can override the meaning of dot syntax with custom code.</p> <p><strong>Beef #3:</strong> I think it looks bad. As an Objective-C programmer, I'm totally used to bracket syntax, so to be reading along and see lines and lines of beautiful brackets and then to be suddenly broken with <code>foo.name = newName; foo.size = newSize;</code> etc is a bit distracting to me. I realize that some things require dot syntax (C structs), but that's the only time I use them.</p> <p>Of course, if you're writing code for yourself, then use whatever you're comfortable with. But if you're writing code that you're planning on open sourcing, or you're writing something you don't expect to maintain forever, then I would strong encourage using bracket syntax. This is, of course, just my opinion.</p> <p>Recent blog post against dot syntax: <a href="http://weblog.bignerdranch.com/?p=83" rel="noreferrer">http://weblog.bignerdranch.com/?p=83</a></p> <p>Rebuttal to above post: <a href="http://eschatologist.net/blog/?p=226" rel="noreferrer">http://eschatologist.net/blog/?p=226</a> (with original article in favor of dot syntax: <a href="http://eschatologist.net/blog/?p=160" rel="noreferrer">http://eschatologist.net/blog/?p=160</a>)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload