Skip to content

Instantly share code, notes, and snippets.

@edefazio
Last active February 29, 2016 02:31
Show Gist options
  • Select an option

  • Save edefazio/c8bc734cd743d24a2e44 to your computer and use it in GitHub Desktop.

Select an option

Save edefazio/c8bc734cd743d24a2e44 to your computer and use it in GitHub Desktop.
Baseline Code Style Using Googles code conventions
package codestyle;
public class GoogleSpacing_No_Comments {
public static int a = 0;
public static int b = 0;
public static int c = 0;
public static int d = 0;
public static int e = 0;
public static int s = 0;
public static int n = 0;
public static void main (String[] args) {
boolean val = true;
while (val) {
}
a += c + d;
a = (a + b) / (c * d);
while (d++ == s++) {
--n;
}
System.out.println ("size is " + n + "\n");
myMethod ((byte) a, (short) (c + d));
for (int j = 0; j < 100; j++) {
}
}
public static final void myMethodWithNoArguments () {
}
public static final void myMethod (byte b, short s) {
}
}
@edefazio

Copy link
Copy Markdown
Author

Here's my critique of this style

  1. I don't find 2 spaces to be sufficient (for indentation) (it's especially bad when you have the bottom of a class containing an inner class with its methods and a loop (you have this cascading }
      }//end of loop
     }//end of method
   }//end of static inner class
}// end of outer public class
  1. Although I've used "Egyptian Braces" or the Kernigan and Ritchie style for most of my career,
    (for example:
public someClass {
}

...where "open" brace is on the same line as the specification and the "close" brace is in the same vertical space as the specification...

After switching to the symmetrical brace style of:

public someClass
{
}

I find the later superior in many ways, but like everything, there is a tradeoff

undoubtedly, the egyptian brace style wins on the space "economy" ... but if and when code gets complicated it becomes not only harder to understand the nested structures and blocks (because the braces exist only to help the compiler, but not the reader of code). Also there are no visual guides aligned on the left gutter of the code to help your eye scan/ pattern match and easily recognize a nested for loop inside an else if block within a switch statement. (This doesnt show in "simple code" with no nesting, but in more complex code, it is harder for our eyes to parse, and harder for our minds to reason about)

  1. I also believe that putting a space between the name of a method and the parethesis when we declare it or call it_:
    for example:
//declaring a method
public synchronized myMethod (byte a, short b) {
}
...and
//calling a method
myMethod (3, 6);

is inconsistent...
if we define an array ...we do this:
public int[] xs;
not this:
public int [] xs;
when we access an array member we do this:
xs[4];
not this:
xs [4];
if we access a member of a class we do this
c.member
not this
c .member

so it follows we should do this (no space):

//declaring a method
public synchronized myMethod( byte a, short b ) {
}
...and
//calling a method
myMethod( 3, 6 );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment