All of these things are something you use an automated formatter for, not regard as some kind of 'modern objc'. I guess people like to focus on pointless stuff like this because it's easier to worry about than whether you should use an eventbus or a delegate - you know, actual hard choices.
Stylistic stuff like this is not something you need to give rules for. Create an automated formatter, run it on your or others source files, and get back to solving real problems.
You're right to an extent, but I don't think "Run your code through a formatter when you're done hacking it together" is a viable, maintainable, or scalable policy. Especially not for enterprise or team based workflows.
Also, #'s 1, 2, 4, and to a certain extent 8 are more questions of design pattern than 'style'.
> 2. Use consistent spacing and use whitespace liberally.
The very definition of a formatting bikeshed.
> 1. No instance variables.
I'm not convinced here - he certainly doesn't explain why you shouldn't use them. I guess he likes to use self.var rather than var - but it's important to note that this does have some performance implications.
> 4. Modularize and model.
I'll give you 4 though - using compiler recognizable types is always a boon. I'm not sure if anybody really needs to be told to modularize and model, most people would understand that without being told. It does lock-in your public API though, making changes to a library require updating all apps that use the library. Meanwhile, adding a new field into a dict means existing apps can upgrade the library easily. Still, I'd definitely agree that types are always a better bet in the long run.
> but I don't think "Run your code through a formatter when you're done hacking it together" is a viable, maintainable, or scalable policy.
Just hook clang-format or clang-tidy with your build system. A lot of projects do it.
You will be surprised how inconsistent people actually are, even in project where almost everyone is sticking to these practices at 100%. And the larger the project, the worse it gets (most people are inconsistent in different ways).
The only way to get this right is to either produce hard errors when they are violated (e.g. your code won't compile, or won't be committed) or do it automatically. clang-tidy is just the way to go.
I'm surprised that he provides five versions of the method definition syntax and none of them are what Apple does and what I consider to be the standard:
No problem! Have you commonly come across all these variations? I have seen a couple different things but my experience working in objective-c written by others has been limited, so my impression was that the above was more widely accepted.
This and the linked guides claim there is no advantage to using instance variables (preferring properties), but I see at least one advantage: simplicity. When you're reading code that uses an instance variable you know exactly what it's doing, you don't have to check the attributes of the property, or the getter/setter implementation, or think about any KVO going on.
I'm not saying this makes instance variables preferable, but there is something to be said for minimising the number of places you need to look when trying to understand a snippet of implementation.
Exactly!!!! So much of objc programming is about maintaining internal state. Dot syntax and automatically synthesized properties just obfuscates whats actually going on.
IMO you should always be accessing the ivar directly unless you have a specific reason not to.
The ridiculous amount of effort that we have to go through to define private properties (just sheer amount of text around them) makes me miss the pre-property days... I still define iVars the old fashioned way. The whole language would do well to have some syntax sugar added akin to Java's private, public, protected for properties so we could just go
Well we have syntax sugar for iVars... I think it's so messy putting some properties in the header and some in the implementation file with a private category - come on we can't have better syntax than that?
Having public things be in the header and private things in the .m seems exactly right! The header should only expose the public interface, not the implementation details, no?
(On the other hand, defining properties in an anon category, instead of just plain old instance variables in the .m, seems pointless to me -- why have a layer of indirection for the class to access itself?)
I'm a heavy user of properties in other languages like C#, but in Objective C, to define the anonymous category with all the property ceremony and verbiage, feels...excessive...if all you need is some private state, and you don't care about generated accessors or KVO.
Why are ivars bad, exactly? They're a lot more succinct if you don't actually need what properties give you.
I'm still learning Objective-C but it always bothers me a bit when a controller conforms to many protocols. Are there strategies to manage this (other than #pragma mark)?
I really like point #4. I'd take that further and say that most "keyed access" classes should be wrapped in a more domain-specific wrapper class (thinking of NSUserDefaults, and the like).
the only other strategy I have seen is to instantiate handlers for the sub concerns. so set self.tv.delegate = myTVDelegate; or something like that.
Apple warns against it because it goes against key value coding I think. I am kinda on the I wish they had a consistent prefix for stuff. maybe it's just my feeble mind, but I like to have the code completion remind me of what's available. the sometimes inconsistent naming conventions in different classes makes it tough to remember all the nuances when you don't use them every day.
- Use MVVM and some method of bindings (e.g. ReactiveCocoa) [0]
- Split up ViewControllers and DataSources [1]
- Split up classes using Categories (I've seen so many blown up AppDelegates)
- Use CocoaPods (this should be a given by now)
- probably much more I forgot about...
oh, and btw: those `@directives` go by the name of `literals`
Build some stuff with Reactive Cocoa before you jump on that bandwagon. It pollutes the stack of everything it touches and its not very useful unless you use it pervasively...
I agree completely. Its use case is highly specialized and all it does is make a bunch of code nicely split up into methods and crams it into fewer methods with a ton of blocks. Even the example of "before" and "after" on the github page shows that how complicated it makes things look.
I'm guilty of #4, but I think it's a judgement thing. If you're just going to briefly use the dictionary, I personally think it's a waste of time to go to the effort of creating a class. If it's core to your program it makes more sense since you can then tap into things like KVO a bit better. And, prior to ARC and automatic synthesis of properties, it made for a ton of boilerplate code.
It's a nice little list. My one nitpick is that he referred to to the class extension [1] as an anonymous category and while it also has been called this it's generally documented as a class extension by Apple and so it makes sense to call that out so folks can go read about how it actually works.
In response to point #3, my counter-example is that when you list a method's arguments or return type, the parentheses include the star. In other words, the star does not "belong" to the variable in that particular case.
-(NSData*) myMethod:(NSString*)string;
It can really be argued both ways. We've hugged the type everywhere I worked.
I agreed with you for about ten years, and then I stopped doing that because of the OPs example but also because idiomatic c and c++ are written this way and I just decided to stop swimming upstream after reading enough of it.
But it can be very confusing when dealing with dereferences.
So I feel you there. Doing C for 15 years you just get used to it.
You'll have to redefine the properties in the subclass's class and specify @dynamic so that it knows the properties were synthesized in the parent class
I've experimented with putting the category in a private, implementors-only header file and it seemed to work fine. But I'm not sure if that's canonical.
I am a rookie Objective-C learner and all these are pretty known to me. I now think "probably people are writing worse code than I do". These are pretty straightforward facts.
Yes its true, you can hack your way to to some idealized notion of objective C.
Accessing everything through properties is lame, and it sucks, and it only seems like a good idea if you use ARC because you think it will make it easier to find the retention bugs, it actually makes it harder.
Also properties are not the same as private members, although obj C trendists want them to become the same.
No, I disagree it takes you further away from object programming not closer.
The current trends in objective C are to hack the file syntax to make it seem like its more encapsulated and use properties to make ARC happy.
Both are wrong, obj C is a leaky abstraction. Too bad. You will never make it better by making your code harder to read and debug.
Yes in obj C inheritance sucks, protocols suck, constants suck, encapsulation sucks, and in the end if you know how to make something, it really doesn't matter. Stop obsessing on shit nobody cares about and build something.
Even with all your recommendations obj C is a bandaid. But falling off your skateboard can be fun. Live a little.
Stylistic stuff like this is not something you need to give rules for. Create an automated formatter, run it on your or others source files, and get back to solving real problems.