Java Coding Style Rules

Examples of statments

simple if/else

     if ( x < y )
         y = 0;
     else
         x = 1;

cascaded if/else

     if ( var1 < var2 )
     {    
         var1 = ...;
         var2 = ...;
           ...
     }
     else if ( var1 == var2 )
     {
         var1 = ...;
         var2 = ...;
         ...
     }
     else
     {        
         var1 = ...;
         var2 = ...;
           ...
     }
  

loop statements


     for ( int i = 0; i < MAX_VAL; i++ )
     {
         ...
         ...
     }

     while ( !cin.eof() )
     {
         cin.get( ch );
         cout.put( ch );
     }

     do // read the first line (including the newline) only
     {
         cin.get( ch );
         cout.put( ch );
     } while ( ch != '\n' );
  

switch statements


     switch ( factorial( i ) )
     {
         case 1:
         case 3:
         case 5:
             cout << "It's odd!\n";
             break;
         case 2:
         case 4:
         case 6:
             cout << "It's even!\n";
             break;
         default:
             cout << "It's less than 1 or greater than 6!\n";
             break;
      }

Examples of Class Definitions


     import java.awt.*;

     /*
         class Triangle defines a two dimmensional Triangle
     */

     class Triangle
     {

         // the x,y coordinates of the origin of this Triangle

         private int x, y;

         // draws this Triangle on the display

         public void draw()
         {
            // ...
         }

         // inverts this Triangle along a horizontal line

         public void flip()
         {
            // ...
         }

         // rotates this Triangle by number of degrees specified by angle

         public void rotate( int angle )
         {
            // ...
         }

     }

Examples of REALLY BAD commenting

All the following comments are poor. Either they state the obvious about the programming language (typical from programmers just learning the language), or they don't add any information about the item they are commenting and the program would actually be more readable if they were deleted. The final offense is pointing out that a particular close brace matches some open brace. If you find yourself needing this, either you aren't aligning your braces properly, or your code is getting too complex.

     /*
        This is a class defintion for Shape.
     */
     class Shape
     {
     public:
         // These are abstract methods
         // draws shape
         abstract void draw();
         // flips shape
         abstract void flip();
         // rotates shape
         abstract void rotate( int angle );
     } // end of Shape

     // A Point class
     class Point
     {
         // Two integers
         private int x, y;
         // a constructor
         public Point( int newX, int newY ) 
         {
             // initialize x and y
             x = newX;
             y = newY;
         } // end of Point
     }
    
     class Rectangle
         extends Shape // Rectangle extends a Shape
     {
         // some private members
         private Point tl;
         private Point br;
         private int angle;
     
          // the public members
         public Rectangle() // constructor
         {
         }
         // ...
         // some member functions
         void draw()
         {
            // ...
         }
         void flip()
         {
            if ( x != y )
            {
                swap( x, y ); // swap the values of x and y 
                flip(); // flip again
            } // end if
         } // end flip()
         void rotate( int angle )
         {
         }
         float area()
         {
         }
     } // end Rectangle