Friday, June 8, 2012

About Coding Standards - C++

Whenever you write an article or an essay, you are conforming to language standards. Some are required, and other are preference. Using a semicolon is required. Choosing whether or not to split the sentence into two instead of conjoining them with a semicolon, however, is just your own opinion. You do it to fit in with a crowd. Some believe that the use of thee semicolon in the English language shows mastery and intelligence, but others may think that it is reckless for whatever reason. Coding standards are similar to this.

When you write a program you are, in essence, writing a very technical essay detailing how to do a specific task. This essay, like in the English language, have preferences you can choose from. You can choose to write a program like this:
int num1=0;int num2=0;for(int i=0;i<10;i++)cout<<num1+num2<<endl;
That could also be written like this:

int num1=0;
int num2=0;

for(int i=0; i<10; i++)
    cout << num1+num2 << endl;

This is exactly the first jumble of code, but more readable. We used a formatting style to make this example more readable than the first. We all know that the semicolon (in programming) should always be at the end of the line (except for the exception of initialization within loops). This is a major standard. What are some of the minor ones, though?

Group Types
I am the type of person that puts all of the variables at the top of a function. Some prefer to make them when they are needed, but I like seeing everything that this function will do before it does it, like an outline. I group variables based on their types,  and I encourage others to do so as well. They should also be related in function, so all numerical types come before text types.When you run out of a certain type, it is good practice to include a line of whitespace. I group pointers by being a pointer, not type.

int num1 =  0;
int num2 =  0;

double d1= 0;
double d2= 0;

char c1     = 'a';

char *pc1 =&c1;

This raises another very minor point. I put "*" near the variable name instead of anywhere else. This way, you can clearly see that it is a pointer.

Indent a space
Within nested loops, I indent one space for each level. Also, I ALWAYS put a bracket, even if it is a one-line loop. I begin the bracket on the same line as the loop and each on its own line, sometimes with comments.

for(;;){
 for(;;){
  for(;;){
   for(;;){
              }//END for
            }//END for
          }//END for
        } //END for

Operator Aligning
Whenever I get input with cin and use cout to output it, I always align the "<<" and ">>"'s. Sometimes I do this with semicolons, equal signs, and comments, but this is not always the case. This is more obvious if a block of code is designed for one task. After blocks of code, make a line of whitespace.

cout << "Enter Item: ";
cin    >> item                 ;
cout << item <<endl   ;

Putting it all together
Here is what a sample program should look like:

#include <iostream>
using namespace std;
int main(){

 int num1=0;
 int num2=0;

 char char='a'

 for(int index=0; index < 10; index++){
  int inlineInt       ;
  inlineInt=index;

  cout << "Enter a whole number: "<<endl;
  cin    >> inlineInt                                             ;
  cout << endl                                                      ;
 }//END for
}

There are many different coding standards out there. K&R, GNU, and Java are just a few. Over the course of your work you will develop your own style. What I have shown you today is my preferred style. You can adopt it if you wish, but it would be a more rewarding experience to write a few programs and see which works best for you. Lazy people and beginners do not follow any sort of standard, and that makes the code very unpleasant to see and work with later on just as much as not commenting it does.

No comments:

Post a Comment