Skip to content

Instantly share code, notes, and snippets.

@edefazio
Created February 29, 2016 01:42
Show Gist options
  • Save edefazio/426346bb8f64dd087850 to your computer and use it in GitHub Desktop.
Save edefazio/426346bb8f64dd087850 to your computer and use it in GitHub Desktop.
Eric's Coding Style trying to make Java code more readable at a glance, and in focussed detail
package codestyle;
public class EricSpacing_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
Author

Being a consultant and moving from Project to project among many companies I've looked at millions of lines of code,
After reading and trying coding styles from

  • the original Java coding styles [http://www.oracle.com/technetwork/java/codeconventions-150003.pdf]
  • googles Java coding guidelines [https://google.github.io/styleguide/javaguide.html]

...and getting input/inspiration from other languages
Doom 3 (C++) [http://fabiensanglard.net/doom3/]

After watching Kevlin Henney's Seven Ineffective Coding Habits of Many Programmers you probably want to start at 18:00 or so (I agree with some things, others not so much)

I thought it would be a good idea to formalize the coding guidelines I've found most effective, AND also explain WHY from a semi-scientific, layout, glance and readability perspective the rationale behind each aspect of how this Style is superior.

the overall rationale behind this style is

  1. make it easy to glance/get the gist of what code does quickly (structurally) as we scan through code
  2. make it easy to drill into details (i.e. when you have to troubleshoot down to the brass tacks, how easy is the code to read and understand?)

Supporting the rationale, I've studied:

  • how humans "actually" read (i.e. with saccades https://en.wikipedia.org/wiki/Eye_movement_in_reading) as apposed to the idea that we read in a sequential seamless fashion, we actually read in jerky back-and-forth horizontal and vertical jumps / movements.
  • Information On Perception and Design (i.e. http://www.amazon.com/Universal-Principles-Design-William-Lidwell/dp/1592530079)
    specifically the aspects of:
    Alignment (How does the alignment of code help with our structural understanding of code),
    Hierarchy (How do we represent Hierarchy/Encapsulation in code),
    Symmetry(How we perceive symmetric designs as inherently simpler and more superior to asymmetric designs)
    Closure, (How our mind can fill in/ compartmentalize a concept/ logic or an idea when we are only given "hints")
    Horror Vacui (the tendency to cramp as much detail/code it into as small place as possible, which makes things less readable and less valuable and more confusing for the sake of "efficiency")
  • Information on Design Usability (i.e. http://www.amazon.com/Things-Designer-People-Voices-Matter/dp/0321767535/ref=sr_1_1?s=books&ie=UTF8&qid=1456716693&sr=1-1&keywords=100+things+every+designer+needs+to+know+about+people)
    Peripheral Vision
    Pattern Recognition WE often don't "read" code but recognize what it is doing by the pattern/styructure of symbols)
    Scanning (Experience and Expectations)
    Locality (things close to each other belong together)
    People read faster with long line length, but prefer shorter line length
    recognition over recall
    "Chunking" (People Process things in chunks)
    mental models
    attention
    Clues, Nudging

@edefazio
Copy link
Author

The simple high level-rules (for consistency sake) are

  1. things that belong together are adjacent (not separated by any space)
  2. things that are different are separated ( by a space or line break )
  3. attempt to horizontally and vertically align to allow "structural scanning"
    (as much as possible have the "form follow the function")
  4. punctuation ( { ',.{";) exists for the sake of the reader as well as the compiler, add space/linebreaks where appropriate to emphasize punctuation and allow visual "cues" to the reader on how to quickly interpret code (and control flow).
  5. Information on the LEFT is "more important" that that on the RIGHT, (most/all programming languages use code interpreted left to right ) ...make it easy for a reader to scan down the left gutter to follow the control flow of the application.

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