Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1. Matching braces always line up vertically in the same column as their construct.
  2. example:
  3.  
  4. void foo()
  5. {
  6.     while (bar > 0)
  7.     {
  8.         System.out.println();
  9.         bar--;
  10.     }
  11.  
  12.     if (oatmeal == tasty)
  13.     {
  14.         System.out.println("Oatmeal is good and good for you");
  15.     }
  16.     else if (oatmeal == yak)
  17.     {
  18.         System.out.println("Oatmeal tastes like sawdust");
  19.     }
  20.     else
  21.     {
  22.         System.out.println("tell me pleeze what iz dis 'oatmeal'");
  23.     }
  24.  
  25.     switch (suckFactor)
  26.     {
  27.         case 1:
  28.             System.out.println("This sucks");
  29.             break;
  30.         case 2:
  31.             System.out.println("This really sucks");
  32.             break;
  33.         case 3:
  34.             System.out.println("This seriously sucks");
  35.             break;
  36.         default:
  37.             System.out.println("whatever");
  38.             break;
  39.     }
  40. }
  41.  
  42. All if, while and for statements must use braces even if they control just one statement.
  43.  
  44. Reasoning: Consistency is easier to read. Plus, less editing is involved if lines of code are added or removed.
  45.  
  46.     if (superHero == theTick) System.out.println("Spoon!");  // NO!
  47.  
  48.     if (superHero == theTick)
  49.         System.out.println("Spoon!");  // NO!
  50.  
  51.     if (superHero == theTick) {
  52.         System.out.println("Spoon!");
  53.     }                                            // NO!
  54.  
  55.     if (superHero == theTick)                  
  56.     {
  57.         System.out.println("Spoon!");
  58.     }                                            // YES!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement