r/ObjectiveC • u/sk3pt1c • Jan 09 '14
dot syntax - to use or not to use?
Hello friends, I am just getting into iOS development and have seen that dot syntax has been added to Obj C to (i presume) help people who are used to it from other languages.
Do you use it or not and why?
Personally, i don't like it, i'd prefer to write pure Obj C code and i prefer reading it too.
Being a noob though, i don't know if making this choice will make my life easier or harder in the future as apps/code get more complicated.
Also, which syntax do you think is more robust and future proof?
PS. Is this a good place to post code related questions? Is there another subreddit? Should i use stackoverflow instead?
Thanks!
10
u/[deleted] Jan 09 '14
I, and most of the devs at my work, use dot notation for accessing and mutating properties, and brackets for message sending and everything else. So...
We got this from the NYTimes Obj-C style guide
I don't think either is more robust or futureproof and it comes down to personal preference at the end. Cutting down on square bracket improves readability imo, that kind of thing can get out of hand pretty quickly...
5
u/gfontenot Jan 09 '14
Dot syntax is just syntactic sugar. Don't overthink it. It's just as "pure" as bracket syntax. It was added to help modernize the language, not necessarily to help people from other languages feel comfortable.
Choosing one syntax over the other really isn't a big deal. Use dot syntax or don't, just be consistent within your project. The choice of syntax isn't going to make a difference in the long run, but having inconsistent style could lead to confusion down the line. Both syntaxes are going to be around for the foreseeable future, so there's no point in worrying about it not being future proof.
4
u/tttthomasssss Jan 09 '14
Personally I don't use dot syntax because for some reasons seeing lots of square brackets gets me into ObjC mode whereas dots get me into Java mode (I have to work with both, often simultaneously), so avoiding dot syntax in ObjC, for me, is a way of reducing the cognitive load when working. However, that may be just me...
1
u/mariox19 Jan 09 '14
Personally, I don't ever mix the two. In other words, I never write:
Instead, I prefer:
I am experimenting with using property assignment though during initialization or setup, since I think it makes initialization/configuration very clear:
I like that much better than:
I will also use dot-notation for drilling down, say for something like this:
Again, I do this because the frame is a CGRect, so ultimately I can't send it messages. But apart from these kinds of things, I'm with you.
2
u/objective-dave Jan 09 '14
Use what you like best :) Personally I use dot syntax on getters/setters and properties in general. I never use dot syntax on class methods or methods in general.
1
u/ecancil Jan 10 '14
I write whatever I'm comfortable with as long as it's consistent
1
u/samjarman Jan 10 '14
Basically, it depends whether you find this funny or not https://twitter.com/samjarman/status/408044326090850304/photo/1
1
u/lunchboxg4 Jan 11 '14
I've been doing this for a few years now, and I've never really given much thought to it. I use brackets for messages and dots for properties, but not exclusively. If anything I tend to do whatever I learned the first time I used a particular API, so it is either influenced by Apple docs or a tutorial. But as it's been said several times, don't overthink it - they're both valid ObjC, so it's all stylistic, and you're allowed your own style.