0

From 'Programming in Objective-C', 6th Edition, Stephen G. Kochan:

Although it’s syntactically correct to write a statement such as myFraction.print, it’s not considered good programming style. The dot operator was really intended to be used with properties; typically to set/get the value of an instance variable. Methods that do other work are typically not executed using the dot operator; the traditional bracketed message expression is the preferred syntax.

Are there performance drawbacks to backup this position, or is this merely social convention?

3 Answers3

3

I don't think it's social convention that drives this. I think it's the improved readability that you get from it.

There isn't a performance hit but you will find it easier working with other devs if you follow the standard.

The Times Online style guide is one I work to and is fairly standard with Apple's own coding style.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
2

It's just a social convention. Properties are just a syntax sugar. So when you use dot-notation, it is automatically translated into a call to a getter/setter.

But I'm totally agree with Mr. Stephen G. Kochan. Using dot-syntax for calling methods is misleading.

FreeNickname
  • 7,398
  • 2
  • 30
  • 60
0

Messaging methods that are not properties with the dot syntax is allowed because at the time properties and dot operator were introduced there were hundreds of millions of lines of code just using get/set accessors methods, and that meant you could use the dot operator to signal that you were using property-like accessors even though they were not declared as properties. Theoretically, this could be removed by now.

Using the dot operator for something else than getting or setting properties is bizarre and will make your code incredibly confusing for yourself and for others, but there's no performance tax at runtime for doing so. On the flip side, there's also no reason to not declare your properties as properties any longer - particularly since it results in less code in Objective-C and enables them to show up as properties instead of as methods in Swift.

Jesper
  • 7,477
  • 4
  • 40
  • 57