Indentation

1. Spaces VS Tabs

image

The author of this source code is using TABs not only for indenting, but also for aligning! That way his alignment gets messed up when somebody uses a different TAB size. The solution of this problem is to simply just use what ever you want for indenting, but use spaces for aligning.

2. Line Length

Avoid lines longer than 120 characters, since they’re not handled well by many terminals and tools.

3. Wrapping Lines

· Break after a comma.

· Break before an operator.

· Prefer higher-level breaks to lower-level breaks.

· Align the new line with the beginning of the expression at the same level on the previous line.

· If the above rules lead to confusing code or to code that’s squished up against the right margin, just indent 8 spaces instead.

// Don't use this indentation 

if ((condition1 && condition2) 
        || (condition3 && condition4) 
        ||!(condition5 && condition6)) { // Bad wraps 
    doSomethingAboutIt(); // Make this line easy to miss 
} 

// Use this indentation instead 

if ((condition1 && condition2) 
        || (condition3 && condition4)// 8 space indentation 
        ||!(condition5 && condition6)) { 
    doSomethingAboutIt(); 
} 

// Or use this 

if ((condition1 && condition2) || (condition3 && condition4) 
        ||!(condition5 && condition6)) { 
    doSomethingAboutIt(); 
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: