Clean, Modern Objective-C
harlanhaskins.com
harlanhaskins.com
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.
Also, #'s 1, 2, 4, and to a certain extent 8 are more questions of design pattern than 'style'.
Bikeshed discussions about formatting quickly go out the window when you have an automated tool that can settle all arguments.
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.
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.
- (void)doSomethingWithParameter:(NSString *)parameter;Worse than that, they distract from things that do.
Bike shed?
-(void) doSomethingWithParameter:(NSString *)parameter;
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.
IMO you should always be accessing the ivar directly unless you have a specific reason not to.
@private NSString dog;
@public NSString cat;
... Oh Wait! We do, it's just that nobody uses it anymore. http://stackoverflow.com/questions/4869935/objective-c-priva...
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?
(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?)
Why are ivars bad, exactly? They're a lot more succinct if you don't actually need what properties give you.
I'm not convinced by private properties either. Use ARC and just define your ivars in the implementation.
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).
With regards to point #8, Apple actually recommends against naming functions starting with get... also, unless they are property getter overrides. (https://developer.apple.com/library/mac/documentation/Cocoa/...)
Thanks for the article.
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`[0] - http://lmgtfy.com/?q=mvvm+objective-c [1] - http://www.objc.io/issue-1/
- I'm super guilty of just handling everything in AppDelegate (notifications, URI schemes, etc.) I need to work on this.
- CocoaPods are the greatest
-(NSData*) myMethod:(NSString*)string;
It can really be argued both ways. We've hugged the type everywhere I worked.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.
int* a;
int b;But on the other hand, it discourages something thats overused in other languages. Inheritance!
Obj-C would do well to find a better composition mechanism to make most of the inheritance issues a secondary concern.
Until Rust turns to 1.0?
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.