Tips for writing clear/organized code?

I have a friend who's started learning c++ also, and he commented that it's really hard for him to read my code. I have been studying a bit longer than he has, so I know a bit more syntax, but I think it's also because my code isn't wrote out neatly. Any tips?
Hard to tell you where you need to improve without seeing what you're writing.
Structure your code using indentation so that distinct blocks of code (those within { } braces) are obviously separate from the surrounding outer code.

e.g.

1
2
3
4
5
6
7
8
9
10
11
if (a == 5)
{
if (b == 8)
{
cout << "Both";
}
}
else
{
cout << "a != 5";
}


1
2
3
4
5
6
7
8
9
10
11
if (a == 5)
{
  if (b == 8)
  {
    cout << "Both";
  }
}
else
{
  cout << "a != 5";
}


Which of the above is easier to read?

You should also use spaces to separate out statements, constants and operators.

if((a==5)&&(b==8))cout<<"Both";

or

if ( (a == 5) && (b == 8) ) cout << "Both";

Spaces between brackets are questionable but clearer.

Use meaningful variable names.

1
2
3
4
5
6
7
8
9
10
for (int a = 1; a < 12; ++a)
{
  for (int b = 1; b < 31; ++b)
  {
    for (int c = 0; c < 24; ++c)
    {
       cout << c << "o'clock, " << b << "/" << a << endl;
    }
  }
}


or

1
2
3
4
5
6
7
8
9
10
for (int month = 1; month < 12; ++month)
{
  for (int day = 1; day < 31; ++day)
  {
    for (int hour = 0; hour < 24; ++hour)
    {
       cout << hour << "o'clock, " << day << "/" << month << endl;
    }
  }
}


Which is clearer? Same goes for method names and type/class names.

Comments are important, but only for cases that the intent is not obvious or there's some complicated logic, for example. And if you write structured code with proper variable, method and class names, it should be.

Anyone else?

Jim
Thanks! The no-no I see I've been doing is the brackets thing. Rather than have them the way you did I was commenting on all of them to make sure I didn't get them mixed up. I'm making a little text based game for learning, and it has many ifs and loops, so knowing that will help a lot.
I like to do this:


1
2
3
4
5
6
7
8
if (condition) {
   //do stuff
}

for (init ; endcondition ; increment) {


}


It helps newbies because there is less temptation to do this:

1
2
3
4
5
6
7
8
9
10
if (condition) ;
{
   //do stuff
}

for (init ; endcondition ; increment) ;
 {
//do stuff

}


I mean, hopefully they are less likely to put the semicolon between the condition and the opening brace. Errors like that are hard to see. It also saves 1 line - not that that matters a damn.

I always use braces, even when there is only 1 statement following - this will save you one day when adding more code.

I generally dislike do loops, but if one insists on using them, put the while condition on the same line as the closing brace:

1
2
3
4
do {
//stuff

} while (condition);


I almost always find a way to write a do loop as a while or for loop.

If you find yourself with a while loop with a null statement - put it on it's own line with a comment:

1
2
while (condition)
          ;      //null statement 


Always make use of functions - there is a bit of a rule that functions should not be more than 80 lines - presumably so one can print it on 1 A4 page. Functions can be handy even if there are 3 or 4 lines long. Especially in the body of a for loop.

Prefer to use switch statements when the variable is a constant int or char, rather than a load of else if's. Always have a default: clause to catch bad input

Use arrays & for loops rather than lots & lots of if's.

I really hate these constructs - just UGLY:

1
2
3
if (a != b && a != c && a != d && a != d) {

}


Much better to use a switch for this.

Here is another handy loop:

1
2
3
4
5
6
7
8
9
10
bool Quit = false;

while(!Quit) {
//get input
//use the toupper function to make testing easier for input like y n or q
// stuff maybe a switch

//user wants to quit
Quit = true;
}


code continues after the while loop.

There you go hope that helps & have fun !!
Topic archived. No new replies allowed.