White Space [Edit]
- Spaces over Tabs
- Trailing whitespace
- Spaces around arguments and operators
Spaces over Tabs
No tabs, ever.
If you need to clean up a codebase with tabs, you can type the following which converts all tab occurences to 2 spaces:
% find . -name "*.scala" | xargs sed -i 's/\t/ /'
Trailing whitespace
Try to leave no trailing whitespace behind.
If you need help cleaning a codebase, type the following which strips all trailing whitespace (spaces, tabs):
% find . -name "*.scala" | xargs sed -i 's/[ \t]*$//'
Spaces around arguments and operators
One space after commas
// wrong: missing space after comma
def foo(a:Any,b:Ref) = ...
// wrong: extra space before comma
def foo(a:Any ,b:Ref) = ...
// right
def foo(a: Any, b: Ref) = ...
One space after colon
// wrong: missing space after colon
def foo(a:Any,b:Ref) = ...
// wrong: extra space before colon
def foo(a:Any,b :Ref) = ...
// right
def foo(a: Any, b: Ref) = ...
Spaces around operators
// wrong: missing spaces around operator
val x = 1+1
// right
val x = 1 + 1
No spaces after parenthesis
// wrong
val threads = 10 + ( Runtime.getRuntime.availableProcessors * 4 )
// right
val threads = 10 + (Runtime.getRuntime.availableProcessors * 4)
// right
if (options.has("?") || options.has("h")) {
parser.printHelpOn(System.out)
System.exit(-1)
}