User Tools

Site Tools


guides:programstyle:whitespace

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
guides:programstyle:whitespace [2020/08/04 16:57] – [Use blank lines to separate sections of code.] wikiadminguides:programstyle:whitespace [2024/07/25 15:01] (current) – external edit 127.0.0.1
Line 26: Line 26:
    * **vi**    * **vi**
      * In vi, add //set expandtab// to your .vi, .exrc, vimrc or other       * In vi, add //set expandtab// to your .vi, .exrc, vimrc or other 
 +   * **nano**
 +     * In nano, add //set tabstospaces// to your .nanorc file in your home directory. 
 +     * You can also set the size of the indentation with //set tabsize 4// (or whatever size you wish).
 +
  
  
Line 75: Line 79:
 The following is an unacceptable use of blank lines The following is an unacceptable use of blank lines
 <code c++> <code c++>
 +int max = 0, 
 +    min = 0, 
 +    num,
 +    sum = 0,
 +    i, 
 +    count;
 +bool first = true;
 +cin >> count;
 +for(i = 0; i < count; i++) {
 +    cin >> num;
 +    if(first) {
 +       min = num;
 +       max=num;
 +       first = false;
 +    }
 +    sum += num;
 +    if (num > max) {
 +       max = num;
 +    }
 +    if (num < min) {
 +        num = min;
 +    }
 +}
 +</code> 
  
-int max=0,  +The following is a more acceptable use of blank lines. 
-    min=0, +<code c++> 
 +int max = 0,  
 +    min = 0, 
     num,     num,
-    sum,+    sum = 0,
     i,      i, 
     count;     count;
 +    
 bool first = true; bool first = true;
 +
 cin >> count; cin >> count;
 for(i = 0; i < count; i++) { for(i = 0; i < count; i++) {
     cin >> num;     cin >> num;
 +    
     if(first) {     if(first) {
        min = num;        min = num;
Line 91: Line 124:
        first = false;        first = false;
     }     }
 +    
     sum += num;     sum += num;
 +    
     if (num > max) {     if (num > max) {
        max = num;        max = num;
     }     }
 +    
     if (num < min) {     if (num < min) {
         num = min;         num = min;
Line 101: Line 137:
 </code>  </code> 
 ==== Consistently use a single style for brace placement. ==== ==== Consistently use a single style for brace placement. ====
 +
 +Braces are used to mark the body of control structures, type definitions and other related blocks of code.  A consistent use of bracket placement provides readers with a visual guide to the structure of a program.  There have been many styles ( see [[http://example.com|External Link|Wikipedia Article on Indentation Style]]) developed.
 +
 +Several styles are approved in the local style guide.
 +
 +=== Allman ===
 +
 +In the Allman style, braces associated with the start of a control statement begin on the next line, indented at the same level.
 +
 +<code c++>
 +int main() 
 +{
 +   int i;
 +   bool printed = false;
 +   
 +   for(i = 1; i < 100; i++)
 +   {
 +      printed = false;
 +      
 +      if (i % 3 == 0) 
 +      {
 +           cout << "fizz";
 +           printed = true;
 +      } 
 +      
 +      if (i % 5 == 0)  
 +      {
 +           cout << "buzz";
 +           printed = true;
 +      }
 +      
 +      if (not printed) 
 +      {
 +           cout << i;
 +      }
 +      cout << endl;
 +   }
 +   
 +   return 0;
 +}
 +</code>
 +=== K&R OTBS ===
 +
 +In this style, the opening brace is placed at the end of the beginning of a control structure.
 +<code c++>
 +int main() {
 +   int i;
 +   bool printed = false;
 +   
 +   for(i = 1; i < 100; i++) {
 +      printed = false;
 +      
 +      if (i % 3 == 0) {
 +           cout << "fizz";
 +           printed = true;
 +      } 
 +      
 +      if (i % 5 == 0) {
 +           cout << "buzz";
 +           printed = true;
 +      }
 +      
 +      if (not printed) {
 +           cout << i;
 +      }
 +      cout << endl;
 +   }
 +   
 +   return 0;
 +}
 +</code>
 +
 +While either style is acceptable, programmers should select a single style and employ it constantly. 
 +
 +
 ==== Place spaces between operators in statements. ==== ==== Place spaces between operators in statements. ====
-==== Do not exceed 80 characters for any line. ====+ 
 +Proper spacing in statements, especially mathematical calculations make it easier to read these statements.   
 + 
 +Unacceptable example: 
 +<code c++> 
 +for(i=0;i<20;i++) { 
 +    ... 
 +
 + 
 +r*cos(theta)*sin(phi); 
 + 
 +r1 (-b+sqrt(4*a*c))/(2*a); 
 +</code> 
 + 
 +Acceptable example: 
 +<code c++> 
 +for(i 0; i < 20; i++) { 
 +    ... 
 +
 + 
 +r * cos(theta) * sin(phi); 
 + 
 +r1 (-b + sqrt(4*a*c)) / (2*a); 
 +</code> 
guides/programstyle/whitespace.1596560231.txt.gz · Last modified: 2024/07/25 15:01 (external edit)