Indentation Styles

Let the religious war begin.

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

54 Replies - 4032 Views - Last Post: 01 January 2010 - 11:08 AM

#1 Nykc  Icon User is offline

  • Gentleman of Leisure
  • member icon

Reputation: 725
  • View blog
  • Posts: 8,638
  • Joined: 14-September 07

Indentation Styles

Posted 19 July 2008 - 09:04 AM

I know this subject can tend to get heated amongst true hardcore programmers, but curiosity has the best of me.

So lets here about it and why do you prefer this method of indentation?


if (expression) { 
     statements
}

if (expression)
{
     statements
}

if (expression)
     {
     statements
     }



the above mentioned ways seem to be the most common I came across and I notice different programmers prefer different ways from my experience.

Personally I prefer the top two depending on the situation. Most commonly I will use the top if statement - to me it is more readable and cleaner looking.

Sometimes I prefer to write my code this way.
#include <iostream>
using namespace std;

int main()
{ 

    int a = 1, b = 1, c;
    if (c = (a - b )) { 
        cout << "The value of c is: " <<  c;
    return 0;
}



So lets discuss.

This post has been edited by Nykc: 19 July 2008 - 09:06 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Indentation Styles

#2 Cerolobo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 450
  • Joined: 05-April 08

Re: Indentation Styles

Posted 19 July 2008 - 09:23 AM

I tend of use Allman/ANSI style, with a tab size of 2. IE

int main(void)
{
  int a;
  int foo;

  if(a == foo)
  {
	/* stuff */
  }

  return 0;
}


It's just easier for me to figure out what scope all the variables are in. Plus, it usually makes it easier to modify (you can comment out a full if statement, for example).

Edit: Plus, it prevents errors like this...
	 if (c = (a - b )) {   // <-- Open bracket, but no close bracket
		 cout << "The value of c is: " <<  c; 

This post has been edited by Cerolobo: 19 July 2008 - 09:27 AM

Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2246
  • View blog
  • Posts: 9,236
  • Joined: 18-February 07

Re: Indentation Styles

Posted 19 July 2008 - 10:59 AM

I used to use a style like:

if (condition) 
{
	while(condition2) 
	{   
		/* some stuff */
	}
}


And then I worked a project where they actually specified the style:
if (condition) {
	while(condition2) {   
		/* some stuff */
	}
}
I seem to have adopted this change (you can actually see the change in the posts in this forum). I agree that this can make matching brackets harder -- but I find it easier to read and I find the indention enough to locate missing brackets. (I suppose this works for me because I ALWAYS use brackets so a line that begins "if" opens a bracket...).

Really it matter very little to me how you indent, so long as you do. The one thing though that I insist upon that other programmers see as useless waste of time is that I ALWAYS use brackets. I don't generally write code like this:
if (condition)
	while (condition)
		/* statement here */;
I hate the missing brackets.

But really it should not be a religious battle because although we may have our own style, we all have to get used to reading other peoples code and a programmer should be able to read any of the styles.
Was This Post Helpful? 0
  • +
  • -

#4 Einherjar  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 10-February 08

Re: Indentation Styles

Posted 19 July 2008 - 12:28 PM

I always use:

if()
{
  stuff;
}



Just easier for me to read and figure out where and what all the brackets go to.
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2246
  • View blog
  • Posts: 9,236
  • Joined: 18-February 07

Re: Indentation Styles

Posted 19 July 2008 - 12:59 PM

on my laptop here I tend to use Dev-C++ as an ide... and I have to say that it sucks at helping me with the code format. I have no idea what style it was programmed with, but it never does what I want. Given the other IDE's that I use, I am used to them conforming to my style.

This post has been edited by NickDMax: 19 July 2008 - 01:00 PM

Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1632
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Indentation Styles

Posted 19 July 2008 - 01:05 PM

If I have several lines of code to execute inside an if statement, I use the normal


if(something here)
{
       //do my stuff here
}
else
{
     //do other stuff here
}



Otherwise I've been known to use the one line version


if(somethinghere) //Do something here


Was This Post Helpful? 0
  • +
  • -

#7 Tom9729  Icon User is offline

  • Segmentation fault
  • member icon

Reputation: 180
  • View blog
  • Posts: 2,641
  • Joined: 30-December 07

Re: Indentation Styles

Posted 19 July 2008 - 05:51 PM

I ALWAYS use brackets, and I always start the brackets on the line after a statement.

For comments within functions, I stick with double forward-slash comments (that way I can use multiline comments to easily comment out a whole section).
Was This Post Helpful? 0
  • +
  • -

#8 Nykc  Icon User is offline

  • Gentleman of Leisure
  • member icon

Reputation: 725
  • View blog
  • Posts: 8,638
  • Joined: 14-September 07

Re: Indentation Styles

Posted 19 July 2008 - 07:19 PM

Very informative. It is just interesting to see all the different styles people use. I remember writing some code and when a senior programmer looked at it he always commented on the way I used indentation and then reading over some textbooks I came across the argument of which style is better.

@NickDMax - I use Code::Blocks IDE and haven't had too many formatting issues with it yet, of course none of my programs have been more than 250 lines of code as of yet either.
Was This Post Helpful? 0
  • +
  • -

#9 Tom9729  Icon User is offline

  • Segmentation fault
  • member icon

Reputation: 180
  • View blog
  • Posts: 2,641
  • Joined: 30-December 07

Re: Indentation Styles

Posted 19 July 2008 - 07:44 PM

Most IDE's will have some kind of auto-formatting. The good ones will actually let you change it. ;)
Was This Post Helpful? 0
  • +
  • -

#10 no2pencil  Icon User is offline

  • Head MFIC
  • member icon

Reputation: 5063
  • View blog
  • Posts: 26,437
  • Joined: 10-May 07

Re: Indentation Styles

Posted 19 July 2008 - 07:50 PM

I think I'm the only person in the world that puts the curley bracket on the same line as the conditional
if {
   ...
}



In C/C++ I always use brackets. In PHP I will sometimes cheat & just use one line

if(!$_GET['stuff']) echo "It didn't get!");



I know I probably shouldn't, but I sometimes do.

I never put a single line on the 2nd line under the conditional without brackets. It looks juvenile, & it almost impossible to read. The same can be said for curley brackets on the same line... never, it looks juvenile.
Was This Post Helpful? 0
  • +
  • -

#11 Tom9729  Icon User is offline

  • Segmentation fault
  • member icon

Reputation: 180
  • View blog
  • Posts: 2,641
  • Joined: 30-December 07

Re: Indentation Styles

Posted 19 July 2008 - 08:13 PM

Juveniles `gotta write code too yah know. ;)
Was This Post Helpful? 0
  • +
  • -

#12 no2pencil  Icon User is offline

  • Head MFIC
  • member icon

Reputation: 5063
  • View blog
  • Posts: 26,437
  • Joined: 10-May 07

Re: Indentation Styles

Posted 19 July 2008 - 08:14 PM

View PostTom9729, on 19 Jul, 2008 - 11:13 PM, said:

Juveniles `gotta write code too yah know. ;)

Sure. Granted. They should, however, properly indent. But they don't.
Was This Post Helpful? 0
  • +
  • -

#13 RodgerB  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 66
  • View blog
  • Posts: 2,284
  • Joined: 21-September 07

Re: Indentation Styles

Posted 20 July 2008 - 12:02 AM

My style is pretty much PsychoCoder's. C# and Visual Studio softly nudge you into using it (with its auto-correctness).
Was This Post Helpful? 0
  • +
  • -

#14 KyleG  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 44
  • Joined: 11-June 08

Re: Indentation Styles

Posted 20 July 2008 - 11:39 AM

It would be nice if this thread also had a poll.

I use the first style in that list, with the opening curly on the same line as the conditional statement. It's the style that I'm accustomed to right now, but for years I used the other popular one with a new line for the curly. I don't have a preference though. There's really no rock-solid, undeniable reason for using either style.

View PostRodgerB, on 20 Jul, 2008 - 12:02 AM, said:

My style is pretty much PsychoCoder's. C# and Visual Studio softly nudge you into using it (with its auto-correctness).

You know, you can chose (in the Options menu) which direction VS softly nudges you. I'm not saying you should change your style. But anybody using VS should definitely set their curly style in the options so you're not fighting the IDE every step of the way.
Was This Post Helpful? 0
  • +
  • -

#15 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2246
  • View blog
  • Posts: 9,236
  • Joined: 18-February 07

Re: Indentation Styles

Posted 20 July 2008 - 12:11 PM

There was at one time a reason for one over the other.

The style:
for (condition) {
	while (condition) {
		//do something here
	}
}


was used in publishing (back in the hay day of computer programming magazines) to conserver real estate. This also produces (arguably) smaller source code files -- which is handy when disk space was expensive.
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »