Scala style-guide

25
Scala Style Guide 27 Jul 2012 > Vikas Hazrati > [email protected] > @vhazrati

description

Writing good readable code in Scala. Guide followed at www.knoldus.com

Transcript of Scala style-guide

Page 1: Scala style-guide

Scala Style Guide

27 Jul 2012 > Vikas Hazrati > [email protected] > @vhazrati

Page 2: Scala style-guide

Indentation

2 spaces Line wrapping

Page 3: Scala style-guide

Indentation

Methods with several arguments

Page 4: Scala style-guide

Naming convention

Camel case

Class KnoldusKnowledgeSession

Object – same like class

Methods, Variables, values, Annotations

def myFairMethod = ...

Page 5: Scala style-guide

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

Page 6: Scala style-guide

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 (>>=)

Page 7: Scala style-guide

Type Parameters

Single

More than one

Page 8: Scala style-guide

Brevity

def add(a: Int, b: Int) = a + b Bad in java

Good in scalaReadableFew local fields involvedConcise code

Page 9: Scala style-guide

Void Methods

Page 10: Scala style-guide

Functions

Space between argument and return type

Page 11: Scala style-guide

Nested Blocks

def foo = {

}

Should define brace on line of declaration

Splitting parentheses have a space

Page 12: Scala style-guide

Classes

Single line

Four spaces

Two spaces

Page 13: Scala style-guide

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

}

Page 14: Scala style-guide

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 = { _ + _ }

Page 15: Scala style-guide

Control Structures

Page 16: Scala style-guide

Comprehensions

Page 17: Scala style-guide

Trivial Conditions

val res = if (foo) bar else baz

Page 18: Scala style-guide

Files

Single logical compilation unit

Inbox.scala

Page 19: Scala style-guide

Multiple files

Cohesive group

All multi-unit files should be given camelCase names with a lower-case first letter.

Help in identification

Page 20: Scala style-guide

ScalaDoc

It is important to provide documentation for all packages, classes, traits, methods, and other

members.

Page 21: Scala style-guide

ScalaDoc

Page 22: Scala style-guide

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

Page 23: Scala style-guide

Packages

Overview of mainclasses

Examples of how to Use class

Page 24: Scala style-guide

Classes, Objects & Traits

Page 25: Scala style-guide