Difference between revisions of "Tmp2"

From assela Pathirana
Jump to navigationJump to search
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Good practice in Coding (in C++/C)=
=Good practice in Coding (in C++/C)=
==Keep complications to minimum==
==Keep complications to minimum==
* C++ (and many other language) have complicated rules on operator precedence. Remember: YOU DON'T NEED 95% of these rules. Unless it is obvious, use parenthesis, to separeate operations.
<source lang=cpp>
a=b/4*x+1 //will give you an answer.
//If you know what you are doing, fine.
//But at least for other's sake it is better to write
a=(b/4)*x+1 // which is the equivalent of above, but much easier to understand.
a=b/(4*x)+1 // if you want to say THAT
</source>
* Use pointers sparingly, there are many situations where they have good alternatives. Make a concious effort to find these alternatives if at all possible. One good example is use of vectors instead of dynamic arrays.  
* Use pointers sparingly, there are many situations where they have good alternatives. Make a concious effort to find these alternatives if at all possible. One good example is use of vectors instead of dynamic arrays.  
{|
{|
Line 31: Line 39:
</source>
</source>
|}
|}
==Variables==
==Variables==
* Define variables with minimum scope. If you just need a variable to use for a counter in a loop, don't define it as a global variable. In <tt>for</tt> loops define the counter within the <tt>for</tt> statement.  
* Define variables with minimum scope. If you just need a variable to use for a counter in a loop, don't define it as a global variable. In <tt>for</tt> loops define the counter within the <tt>for</tt> statement. And when you have to define a global variable, use a good descriptive name.  
<source lang=cpp>
<source lang=cpp>
for(unsigned int i=0;i<vect.size();i++){ // much better than a globally defined i.  
for(unsigned int i=0;i<vect.size();i++){ // much better than a globally defined i.  
   ...
   ...
}
}
</source>
</source>


Line 60: Line 70:
..
..
</source>
</source>
* Use indenting and comments to make multiple loops easy to read. In long loops like the code shown below it really helps to have the comments like <tt>end of loop i</tt> at the place where the loop is closed.  
* Use indenting and comments to make multiple loops easy to read. In long loops like the code shown below it really helps to have the comments like <tt>end of loop cti</tt> at the place where the loop is closed. And the use of 'funny' counters like <tt>cti</tt> and <tt>jj</tt> is not an accident. In long code, avoid using one letter variable names, unless the scope of these variables are very short -- this make search and find easy.  
<source lang=cpp>
<source lang=cpp>
for (int i=0;i<n;i++){
for (int cti=0;cti<n;cti++){
   for (int j=0;j<m;j++){
   for (int jj=0;jj<m;jj++){
       a=b[i];
       a=b[cti];
       //... 559 lines of code -- we don't print them here
       //... 559 lines of code -- we don't print them here
       ...
       ...
   } // end of loop j
   } // end of loop jj
   //... another 400 lines of code -- again we skip.  
   //... another 400 lines of code -- again we skip.  
   ...
   ...
} // end of loop i
} // end of loop cti
</source>
</source>
==Structure==
* Use functions to modularize your code.
* Do not re-invent the wheel. First see whether there is a standard function to do something (for many simple calculations there is), before writing one yourself.
*

Latest revision as of 18:03, 9 April 2007

Good practice in Coding (in C++/C)

Keep complications to minimum

  • C++ (and many other language) have complicated rules on operator precedence. Remember: YOU DON'T NEED 95% of these rules. Unless it is obvious, use parenthesis, to separeate operations.
a=b/4*x+1 //will give you an answer. 
//If you know what you are doing, fine. 
//But at least for other's sake it is better to write
a=(b/4)*x+1 // which is the equivalent of above, but much easier to understand. 
a=b/(4*x)+1 // if you want to say THAT
  • Use pointers sparingly, there are many situations where they have good alternatives. Make a concious effort to find these alternatives if at all possible. One good example is use of vectors instead of dynamic arrays.
int* a = NULL;   // Pointer to int, initialize to nothing.
int n;           // Size needed for array
cin >> n;        // Read in the size
a = new int[n];  // Allocate n ints and save ptr in a.
for (int i=0;i<n;i++){
    cin >> a[i];
}
...
... 
delete [] a;  // When done, free memory pointed to by a.
a = NULL;     // Clear a to prevent using invalid memory reference.
vector <int> a;
int n;           // Size needed for array
cin >> n;        // Read in the size
for (int i=0;i<n;i++){
    int tmp;
    cin >> tmp;
    a.push_back(tmp);
|}
...
...
a.erase();  // When done, free memory pointed to by a.

Variables

  • Define variables with minimum scope. If you just need a variable to use for a counter in a loop, don't define it as a global variable. In for loops define the counter within the for statement. And when you have to define a global variable, use a good descriptive name.
for(unsigned int i=0;i<vect.size();i++){ // much better than a globally defined i. 
   ...
}

Loops

  • For loops without braces are sometimes possible. But do a favor, don't do it!
for (int i=0;i<n;i++)
    // foo
    // bar
    cout << str[i]; // perfrectly correct, loop ends here. BUT CONFUSING. 
c=a+b
..
..
for (int i=0;i<n;i++){
    // foo
    // bar
    cout << str[i];
} //much better way. 
c=a+b
..
..
  • Use indenting and comments to make multiple loops easy to read. In long loops like the code shown below it really helps to have the comments like end of loop cti at the place where the loop is closed. And the use of 'funny' counters like cti and jj is not an accident. In long code, avoid using one letter variable names, unless the scope of these variables are very short -- this make search and find easy.
for (int cti=0;cti<n;cti++){
   for (int jj=0;jj<m;jj++){
       a=b[cti];
       //... 559 lines of code -- we don't print them here
       ...
   } // end of loop jj
   //... another 400 lines of code -- again we skip. 
   ...
} // end of loop cti

Structure

  • Use functions to modularize your code.
  • Do not re-invent the wheel. First see whether there is a standard function to do something (for many simple calculations there is), before writing one yourself.