tags:

views:

987

answers:

4

The first thing I do when I incorporate any third party code into my application is reformat it to my personal coding preference:

// Single line comments only

// I never put spaces inside my parenthesis 
-(void)myOCDMethod
{    
    // If an if or for statement has only one instruction, I don't use brackets
    if(this)
        [self that];
    else
        [self somethingElse];

    // If I do have to use brackets, they go on their own lines so that they line up
    if(this)
    {
        [self that];
        [self andThat];
    }

    // I always put the pointer asterisk next to the instance name
    NSObject *variable = [[NSObject alloc] init];

    // I always put spaces around operators
    if(i == 0)
        x = 2;

}

What OCD coding format do you use?

+1  A: 

I do a lot of this, with a few differences:

I always insert spaces before and after parens: -(void)myOCDMethod -> - (void) myOCDMethod

I leave braces on the same line:

if (this) 
{
  //code
}
becomes
if (this) {
  //code
}

If I'm feeling particularly OCD, I'll line up my locals:


float                l1;
NSArray              *array;
ReallyLongClassName  *object;

And, finally, TABS, not SPACES.

Ben Gottlieb
A: 

Pretty much the same as yours, though I leave a space between the "-" or "+" and the opening parenthesis of the return type. Oh, and I use the (condition) ? (value1) : (value2) thing a lot, mainly for assignments and math... I know it makes the code harder to read, but it saves three lines' worth of typing.

Noah Witherspoon
Plus, it has less overhead (I don't know if it's noticeable, though).
Steph Thirion
A: 
  1. Indent the code properly
  2. Correct line breaks. (Max one line break; line break before every function and comment etc.)
  3. Correct naming conventions
Ramesh Soni
A: 

Tabs are evil in a middle of a line, while spaces rock! Example:

int    n;
double d;

Let the tab size be 4. I'll point them out with dots:

.   .   .
int     n;  // two tabs here
double  d;  // one tab here

If that code is opened on another developer's machine, where tab size is 2, he'll see the following picture:

. . . . .
int   n;
double  d;

So, you should either use same tab size (and this would not work anyways, since your code may be reused by anybody in the world - you can't force everybody), or stick to spaces. Gotcha?

Sergey Borodavkin