-
Notifications
You must be signed in to change notification settings - Fork 99
Code Style
noodlenode edited this page Mar 8, 2012
·
12 revisions
Consistent coding style makes code easier to maintain. When submitting pull requests containing new code or modifications to existing code, please try to stick to these guidelines. If ever in doubt about anything here or in the code, simply ask about it on the forums or IRC.
Not all files currently follow these guidelines. If you find yourself modifying one of these, it is more important to be consistent within the file than to be consistent with the rest of the project, e.g. don't mix different line endings within a file. This also ensures that your diffs clearly show only relevant changes.
TODO: It's always a mess to do it (makes blame and diff less useful across the update), but some cleanup may be needed for particularly bad files.
- Use Unix-style line-endings, i.e. LF (
\n
) instead of CRLF(\r\n
). More details can be found in the GitHub Help on Line Endings. Use the following command to correctly set thecore.autocrlf
option.
$ git config --global core.autocrlf input
$ git config --global core.autocrlf true
- Indent blocks with tabs. Do not use spaces.
- Use the 1TBS style. Statements such as
if
,else
, orwhile
should include their opening brace on the same line. Always use braces, even for single-line conditional statements.
if (condition) { // Opening brace on same line as statement
doSomething();
} else { // else on same line as closing brace
doSomethingElse();
} // Final closing brace on a line by itself
if (condition)
{
doSomething();
}
if (condition)
doSomethingElse();
if (condition) doSomething();
- Stick to US English spellings, e.g. use
color
rather thancolour
,couleur
, orカラー
. - Use the standard Java naming conventions.
- Classes and Interfaces should be nouns in capitalized CamelCase, e.g.
MojamComponent
,TitleMenu
- Methods should be verbs in camelCase with the first letter lowercase, e.g.
run
,scaleBitmap
- Variables should also be in camelCase with the first letter lowercase, e.g.
color
,blitWidth
- Constants should be in UPPERCASE, with words separated by underscores ('_'), e.g.
SCALE
,GAME_VERSION
- Classes and Interfaces should be nouns in capitalized CamelCase, e.g.
- Try to avoid excessively shortening variable names. Don't use single letters except when their meaning is clear, e.g.
i
as an index in afor
loop. Also common arex
,y
,w
, andh
, but use longer names when appropriate, e.g.xOffset
instead ofxx
,x2
, or other equally obscure transformations.
- Try to keep the width of comment blocks within 80 characters.
- Use multi-line
/*
*/
comments for comments spanning multiple lines, rather than repeated single-line//
comments.
/*
* An asterix also begins each
* line in the comment block.
*/
// Don't use these for
// multi-line comments.
- There is no hard limit on the length of lines containing only code, but try to split longer lines (over 120 characters) if possible.
- Do not leave spaces at the end of a line.
- TODO: Splitting conditional and arithmetic expressions
- TODO: Splitting a long function definition or call
- TODO: Probably just for the truly pedantic, but include some examples of spacing around parameters in function calls, array indicies, and around statements.