Raizlabs-Cocoa-Style icon indicating copy to clipboard operation
Raizlabs-Cocoa-Style copied to clipboard

Added a mixed usage section for dot syntax. Added sections to clarify t...

Open KingOfBrian opened this issue 11 years ago • 6 comments

@ndonald2

KingOfBrian avatar Aug 26 '14 20:08 KingOfBrian

@KingOfBrian @ndonald2 I disagree with the advice not to mix bracket and dot syntax in the same line. Just because your first thing uses bracket, that shouldn't have to ruin the clarity of the rest of the line. And in the case of accessing struct members, you have to use dot syntax.

Example:

[UIApplication sharedApplication].keyWindow.rootViewController.view.frame

vs

[[[[[UIApplication sharedApplication] keyWindow] rootViewController] view] frame].size;

† note that I would never advocating calling .size on a CGRect in production code. It’s just a contrived example.

ZevEisenberg avatar Aug 26 '14 21:08 ZevEisenberg

@ZevEisenberg My comment to @KingOfBrian was not to do it unless it's absolutely required, e.g. in a struct, so that's a valid point. Otherwise, I respect your disagreement, but I will defend to the death the position that mixed syntax should never be used if it's not necessary. If you're chaining that many property accessors you have bigger problems to worry about.

ndonald2 avatar Aug 26 '14 21:08 ndonald2

@ndonald2 what about the example I provided? There are cases where you would definitely need to use a chain that long. Would you be OK with it, or would you prefer a) brackets, or b) breaking it into multiple lines?

ZevEisenberg avatar Aug 26 '14 21:08 ZevEisenberg

@ndonald2 my reason for being OK with mixing is that, as long as we’re OK with dot syntax for properties, we should prefer it as much as possible. I think a leading [[[[[[ is harder to read than a bunch of chained dots.

ZevEisenberg avatar Aug 26 '14 21:08 ZevEisenberg

@ZevEisenberg In your example, breaking into multiple lines is more logical. Also that kind of a chain shouldn't be called inline from more than one place in the app - if you need to get the root VC's view's frame, that should be abstracted into a utility method, e.g.:

+ (CGRect)rz_rootViewControllerFrame
{
    UIViewController *rootVC = [[[UIApplication sharedApplication] keyWindow] rootViewController];
    return rootVC.view.frame;
}

Maybe a bad example because it uses both bracket and dot syntax chains, but you get the idea.

ndonald2 avatar Aug 26 '14 21:08 ndonald2

I just realized that dot notation works on class names. I think this may take out some objections. Code completion doesn't work, but it compiles fine.

UIApplication.sharedApplication.keyWindow.rootViewController.view.frame

Found this randomly browsing the SDWebImage documentation.

KingOfBrian avatar Aug 27 '14 19:08 KingOfBrian