0

My coding team has chosen implement a coding standard of using @property's for all instance ivars. For publicly facing things, we of course define them in our .h files, but for our private things, we define them in the .m file in an interface above our implementation.

  1. Does it matter if I refer to them as self.myvar = whatever or as [self setMyvar:whatever]? It doesn't seem to matter at all to me, and there seems to be a great deal of mixing one way or the other in our code base.
8
  • Your question doesn't actually seem to have anything to do with your coding standard. Commented Apr 19, 2013 at 16:39
  • I am on narcotics while recovering from surgery, so I'm not surprised. Commented Apr 19, 2013 at 16:47
  • 1
    we define them in the .m file in an interface above our implementation. This is a so-called "class extension" Commented Apr 19, 2013 at 16:48
  • @rmaddy That's true, but it's still a valid question. In the same way whether while (true) is better than for(;;) Commented Apr 19, 2013 at 16:49
  • Thanks for all the answers. This is very illuminating for me. I was hesitant to ask the question before for fear of looking dim, and with all the other questions, I wasn't sure which was still true since arc was being used. Commented Apr 19, 2013 at 16:50

4 Answers 4

5
self.myvar = whatever

is syntactic sugar for

[self setMyvar:whatever]

They're exactly the same thing. No difference at all.

Sign up to request clarification or add additional context in comments.

Comments

5

As others have indicated, foo.bar and [foo bar] are equivalent (save for the additional type requirements on the former, but that is minor).

FWIW, our team decided to eschew the dot syntax completely. The motivation is to avoid ambiguity; a message send always looks like a message send and .s are always used to access structure members.

We also limit our use of @{} and @[] to the creation of collections only. All accesses are done via indexOfObject:, objectForKey:, etc...

As well, we use ARC everywhere save for a couple of border files that sit between ObjC and C++. And we have the static analyzer turned on for all DEBUG builds and all warnings are treated as hard errors. We've also turned on just about every compiler warning that is practical (there are some that simply aren't practical to use).

3 Comments

That is pretty interesting. It gives me good food for thought and to discuss with my team.
I can understand the ambiguity of dot notation, but why did you choose to not use @[] for accessing?
Performance ambiguity, particularly in framework code related to encoding/decoding wire protocols. If you see foo[i], it isn't clear if that is a C array, an NSArray or -- without knowing the type of i -- possibly a hash lookup of some type.
2

There is plenty of similar questions.

In our coding standards, we don't care about using dot notation or method to access the property. We even sometimes use dot notation for methods which are not formally declared as property because with library methods it's sometimes hard to know without checking the docs and it doesn't make a difference.

It never makes sense to forbid direct method calls (hard to enforce).
I saw coding standards forbidding the dot notation.

In general I tend to prefer dot notation because it enables me to split assignments into visually separated parts, e.g.

self.a = x;

against

[self setA:x];

The second just seems less readable to me but it's a matter of personal taste.

On the other hand, sometimes it's easier to use the method directly, e.g. when you have the object as an id and you would have to cast to use the dot notation.

I think that mixing both is a good solution. Choose the one that will increase the readibility at the given place.

Comments

2

self.myVar = x is actually compiled to [self setMyVar: x];. There's no run-time difference.

However; for ease of code readability, I'd advise sticking with one scheme or the other. If you've already had properties enforced, it'd be better to leave everything in the dot notation - if for no other reason than because this allows your code to be more easily searched.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.