Scala style-guide
-
Upload
knoldus-software-llp -
Category
Technology
-
view
1.687 -
download
2
description
Transcript of Scala style-guide
Scala Style Guide
27 Jul 2012 > Vikas Hazrati > [email protected] > @vhazrati
Indentation
2 spaces Line wrapping
Indentation
Methods with several arguments
Naming convention
Camel case
Class KnoldusKnowledgeSession
Object – same like class
Methods, Variables, values, Annotations
def myFairMethod = ...
Parentheses for methods
foo1 can be accessed with or without parenthesesfoo2 cannot be accessed with parentheses
Accessors do not define parenthesesCall site should follow the declaration conventionNo parentheses, when the method has no side effect
Symbolic method names
! :: => Avoid
Symbolic method names should be reserved for
1. DSL2. Logically mathematical operations (e.g. a + b
or c :: d)3. Purely functional operations like joining
Monads (>>=)
Type Parameters
Single
More than one
Brevity
def add(a: Int, b: Int) = a + b Bad in java
Good in scalaReadableFew local fields involvedConcise code
Void Methods
Functions
Space between argument and return type
Nested Blocks
def foo = {
}
Should define brace on line of declaration
Splitting parentheses have a space
Classes
Single line
Four spaces
Two spaces
Ordering in a class
New line between declarations
Fields before use inmethods
def foo(bar: Baz): Bin = expr
def foo(bar: Baz) { // return type is Unit
expr
}
Functional Values
val f1 = { (a: Int, b: Int) => a + b }
val f2 = (a: Int, b: Int) => a + b
val f3 = (_: Int) + (_: Int)
val f4: (Int, Int) => Int = { _ + _ }
Control Structures
Comprehensions
Trivial Conditions
val res = if (foo) bar else baz
Files
Single logical compilation unit
Inbox.scala
Multiple files
Cohesive group
All multi-unit files should be given camelCase names with a lower-case first letter.
Help in identification
ScalaDoc
It is important to provide documentation for all packages, classes, traits, methods, and other
members.
ScalaDoc
Avoid!Method Returns → returns <what>This class does XXX - > Does <what>
Links to Scala lib classes as [[scala.option]]@return
Document what method 'does not' and 'should not' do
first sentence should be a summary of what the method does.Subsequent sentences explain in further detail
Packages
Overview of mainclasses
Examples of how to Use class
Classes, Objects & Traits