Created
February 29, 2016 01:42
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) | |
{ | |
} | |
} |
The simple high level-rules (for consistency sake) are
- things that belong together are adjacent (not separated by any space)
- things that are different are separated ( by a space or line break )
- attempt to horizontally and vertically align to allow "structural scanning"
(as much as possible have the "form follow the function") - 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).
- 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
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
...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
Supporting the rationale, I've studied:
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")
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