nytimes / objective-c-style-guide

The Objective-C Style Guide used by The New York Times
http://open.blogs.nytimes.com/2013/08/01/objectively-stylish/
MIT License
5.85k stars 1.26k forks source link

pragma marks and file structure #12

Closed stigi closed 1 year ago

stigi commented 11 years ago

Hi,

I'm pretty sure you're using pragma marks a lot.

I'm curious what's your best practise to structure code then.

Here's an example from us (@nxtbgthng):

We usually structure our implementation files like so like so (let's call this one Class.m):

/* copy right comment if necessary */

#import <Framework/Framework.h>

#import "OtherHeader.h"
#import "OtherHeaderInSemanticalGroup.h"

#import "NextHeader.h"

#import "Class.h" // we import the implementation files header last

// place for consts and defines if you can't avoid them

@interface Class () <SecretNotVisibleFromHeaderProtocol>
@property (readonly) NSString *text;
@end

@implementation Class

#pragma mark Class Methods

+ (void)load;
{
}

#pragma mark Lifecycle

- (id)init;
{
    ....
}

- (void)dealloc;
{
    ....
}

#pragma mark Accessors

@synthesize text = _text; // readonly properties don't come with an ivar by default
- (NSString *)text
{
    if (!_text) {
        _text = @"text";
    }
    return _text;
}

#pragma mark SecretNotVisibleFromHeaderProtocol

- (void)methodDefinedInTheProtocol;
{
}

#pragma mark UIResponder

- (void)methodOverridenFromUIResponder;
{
}

#pragma mark Private Helper

- (CGRect)rectFromStuff:(id)stuff;
{
}

@end

#pragma mark -
#pragma mark Stuff private to this class

// Put private implementation blocks and C functions here.
// They need to be declared on top of the file though and might make things a little more messy

So key points are:

Thanks for sharing your style guide. It's pretty close to how we do it which is a good sign for both of us ;)

-ullrich

jacobvanorder commented 11 years ago

@stigi, do you have a mark for delegate methods or is that what you mean within the "SecretNotVisibleFromHeaderProtocol" mark?

chrisladd commented 11 years ago

One note -- we usually use pragma sections to group methods, and then marks in between them to denote subgroups. So:

#pragma mark - SecretNotVisibleFromHeaderProtocol

not

#pragma mark SecretNotVisibleFromHeaderProtocol

For example:

#pragma mark - Table View
#pragma mark UITableViewDatasource
    . . . 
#pragma mark UITableViewDelegate
    . . . 
mbbischoff commented 9 years ago

This is addressed in https://github.com/NYTimes/objective-c-style-guide/pull/80.

jacobvanorder commented 9 years ago

:+1:

cdzombak commented 9 years ago

Closes #44.

Unless I am missing something, this PR does not address ordering of class vs. instance methods.

mbbischoff commented 9 years ago

Unless I am missing something, this PR does not address ordering of class vs. instance methods.

Whoops, it was supposed to. I’ll get right on that.

zohirkdj commented 6 years ago

Cool