0

The compiler seems to be happy, I'm happy with slightly improved readability, but Xcode's code completion doesn't particularly recognize alloc and class when invoked this way:

MyClass* object = [MyClass.alloc initWithBounty:bounty];
<...>
if ([object isKindOfClass:MyClass.class])
    <...>

So I was wondering what is wrong with the above, if at all?

mojuba
  • 11,842
  • 9
  • 51
  • 72
  • The compiler won't complain because it's possible for a class property to be created at run time. Actually running this code will throw an exception though. – Jon Shier Nov 05 '13 at 01:14
  • @jshier no it doesn't throw any exceptions. I've been using this syntax for a while now, it seems to be OK in all respects. Hence my question. Maybe I'm missing something. – mojuba Nov 05 '13 at 01:15
  • nothing wrong. it just a style that I don't like. – Bryan Chen Nov 05 '13 at 01:15
  • 2
    Style. Plain and simple. See @bbum's answer to this question: http://stackoverflow.com/a/1249479/610592 – Sean Nov 05 '13 at 01:16
  • @mjouba: Ah, my mistake. See Eonil's answer then. – Jon Shier Nov 05 '13 at 01:17
  • Well, I deleted my answer because I guess I misread the question. I've never used this syntax and incorrectly thought it actually didn't work. – nhgrif Nov 05 '13 at 01:20
  • The main problem is that it's unfamiliar to 99% of Objective-C programmers. But it's a problem that Objective-C has in buckets, since it's become such a moving target of late -- it's become impossible to legitimately argue that it's "bad style", since "Objective-C style" has become so poorly defined. – Hot Licks Nov 05 '13 at 01:22
  • @Sean I understand it's an issue of style. I see two problems potentially: (1) other code maintainers who dislike the style and (2) searching in the code: if I want to find where all instances of MyClass are being created in a large code base, which syntax should I look for? But apart from that, there are no problems, as I understand it. – mojuba Nov 05 '13 at 01:23
  • possible duplicate of [Style: Dot notation vs. message notation in Objective-C 2.0](http://stackoverflow.com/questions/1249392/style-dot-notation-vs-message-notation-in-objective-c-2-0) – Bryan Chen Nov 05 '13 at 01:29
  • @mojuba I disagree that the issue of style is trivial and therefore "there are no problems". The problem of style is actually a huge one. It's not just that other devs may dislike it, it's also that it's not idiomatic and thus makes your code confusing and harder to read. (Plus, it instantly makes it look like you don't understand properties in objective-c). – Sean Nov 05 '13 at 01:40
  • @Sean I think I do understand properties in Objective C as well as in a dozen of other languages. It's a convenient but confusing concept, because it's generally not encouraged to have property getters with side effects, but nothing stops programmers from creating such getters. You can find plenty even in system frameworks. So using dot syntax on a property/method that's not yours is like saying "please, framework, just do something simple with no side effects". Which is kind of talking to yourself. I see this as a bigger issue than just a style especially in concurrent programming. – mojuba Nov 05 '13 at 02:02
  • @Bryan Chen I've seen that question and a few similar ones, but particularly `alloc` and `class` has never been discussed here. I think they deserve a separate discussion. – mojuba Nov 05 '13 at 02:04

2 Answers2

3

Well, primarily what's wrong is that dot notation is for retrieving things that are conceptually properties. alloc does not access a property of the class; it creates an object. Using it for any zero-argument method is not more readable — it's confusing.

MyClass.class is actually not problematic in that way, but there's no way to declare properties on a class and they usually aren't thought of as having properties, so the autocomplete apparently doesn't support it.

Chuck
  • 234,037
  • 30
  • 302
  • 389
1

Dot notation is originally added to be used for property access. So you can use them only for

  • A method takes no parameter and returns single value (getter).
  • A method takes single parameter and returns no value (setter).

Otherwise, recent compiler will complain about it.

Anyway, I agree to @nhgrif that using dot notation on non-property method is not a good practice.

eonil
  • 83,476
  • 81
  • 317
  • 516
  • "Not good practice" possibly, but there should be a reason behind every "good practice" because otherwise it won't be so "good" any more ;) So what are the reasons? Could you elaborate in your answer? – mojuba Nov 05 '13 at 01:20
  • Well I think code needs to represent intention clearly. Do notation has meaning of *this is a property*. If I use dot notation on non-property method, other programmers (including future myself) may have some confusion. – eonil Nov 05 '13 at 01:21
  • I'd say MyClass.class does look like a property. Alloc - not so much. – mojuba Nov 05 '13 at 01:25
  • [GitHub's ObjC style guide](https://github.com/github/objective-c-conventions) advocates dot syntax for idempotent methods, such as `NSFileManager.defaultManager` By that rule, `MyClass.class` would also be correct. I incline towards _never_ using it, personally. – jscs Nov 05 '13 at 01:26
  • 1
    If it actually feels like the class's property, I wouldn't hesitate to use dot notation. – eonil Nov 05 '13 at 01:33