Proper, consistent indentation of source code is a crucial component to keep from going insane when staring at lines of text for hours on end. Most people adhere to a single favored style, be it K&R, ANSI, GNU, or whatever*, which for the most part is acceptable because they aren’t all that different from one another, and most modern IDE’s are capable of understanding different coding styles and adapting auto indentation settings to the wants of the programmer. However, there is also a tool included (or at least available for) *nix systems, called indent which can actually manipulate C/C++ source code to modify it’s indentation style.

Take for example this extremely lazy solution to projecteuler.net problem #3 It has many levels of indentation, comments, and is nice and short, which will help emphasize the major differences between coding styles in my examples. My personal favorite coding style follows the ANSI standard, which is good for distinguishing code blocks, but takes up a lot more vertical space than other styles.

//* Euler Three */
#include <iostream>
using std::cout;
using std::endl;

#include <cmath>
using std::sqrt;

bool isPrime(unsigned long long);

int main()
{
  unsigned long long NUM = 600851475143; // as provided by projecteuler   problem #3
  for (unsigned long long i = NUM / 2; i >= 2; i--)
    {
    // The following for-loop is going to do something relavent
    for (unsigned long long t = i; t >= 2; t--)
    {
      if ((t * i) == NUM)
      {
        if (isPrime(t))
        {
            cout << "PFF: " << t << endl;
         }
       }
     }
   }
  return 0;
}

bool isPrime(unsigned long long n)
{
  bool prime = true;
  for (unsigned long long i = 3; i <= sqrt(n); i += 2)
  {
    if (n % i == 0)
    {
      prime = false;
      break;
    }
  }
  if ((n % 2 != 0 && prime && n > 2) || n == 2)
  {
    return true;
  }
  else
  {
    return false;
   }
}



Now, let’s use indent to change to the more common K&R style:
$ indent -kr Three.cpp


The resulting source file:

/* Euler Three */

#include <iostream>
using std::cout;
using std::endl;

#include <cmath>
using std::sqrt;

bool isPrime(unsigned long long);

int main()
{

    unsigned long long NUM = 600851475143;	// as provided by projecteuler problem #3
    for (unsigned long long i = NUM / 2; i >= 2; i--) {
	// The following for-loop is going to do something relavent
	for (unsigned long long t = i; t >= 2; t--) {
	    if ((t * i) == NUM) {
		if (isPrime(t)) {
		    cout << "PFF: " << t << endl;
		}
	    }
	}
}
return 0;
}

/*
	The follwing function was copied and pasted from somewhere on the internets
	and should probably have some sort of credit to the author or something
*/
bool isPrime(unsigned long long n)
{
    bool prime = true;

    for (unsigned long long i = 3; i <= sqrt(n); i += 2) {
	if (n % i == 0) {
	    prime = false;
	    break;
	}
    }
    if ((n % 2 != 0 && prime && n > 2) || n == 2) {
	return true;
    } else {
	return false;
    }
}

The style is changed, and all I had to do was invoke a command with a single switch and the file name as the parameter. There are other switches available, of course, including:
-bad (force blank lines after declarations)
-bap (force blank lines after function bodies)
-bl (format according to the Pascal standard)
-bf (forces braces on if statements)
-bls (put braces on the line after struct declaration lines)
-kr (format to the Kernighan & Ritchie coding style)
-orig (format to Berkeley coding style)
-st (send to standard output )
-bbo (break after boolean operators)
-bc (break after commas)
… and a bazillion more options, all available in the man pages :p

* for more reading on indentation styles, check out the wiki entry