Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Scala style-guide

2,333 views

Published on

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

Scala style-guide

  1. 1. Scala Style Guide27 Jul 2012 > Vikas Hazrati > vikas@knoldus.com > @vhazrati
  2. 2. Indentation2 spaces Line wrapping
  3. 3. IndentationMethods with several arguments
  4. 4. Naming conventionCamel caseClass KnoldusKnowledgeSessionObject – same like classMethods, Variables, values, Annotationsdef myFairMethod = ...
  5. 5. Parentheses for methodsfoo1 can be accessed with or without parenthesesfoo2 cannot be accessed with parenthesesAccessors do not define parenthesesCall site should follow the declaration conventionNo parentheses, when the method has no side effect
  6. 6. Symbolic method names ! :: => AvoidSymbolic method names should be reserved for1. DSL2. Logically mathematical operations (e.g. a + b or c :: d)3. Purely functional operations like joining Monads (>>=)
  7. 7. Type Parameters Single More than one
  8. 8. Brevitydef add(a: Int, b: Int) = a + b Bad in javaGood in scalaReadableFew local fields involvedConcise code
  9. 9. Void Methods
  10. 10. FunctionsSpace between argument and return type
  11. 11. Nested Blocksdef foo = { Should define brace on line of declaration} Splitting parentheses have a space
  12. 12. Classes Single line Four spaces Two spaces
  13. 13. Ordering in a class New line between declarations Fields before use in methodsdef foo(bar: Baz): Bin = exprdef foo(bar: Baz) { // return type is Unitexpr}
  14. 14. Functional Valuesval f1 = { (a: Int, b: Int) => a + b }val f2 = (a: Int, b: Int) => a + bval f3 = (_: Int) + (_: Int)val f4: (Int, Int) => Int = { _ + _ }
  15. 15. Control Structures
  16. 16. Comprehensions
  17. 17. Trivial Conditionsval res = if (foo) bar else baz
  18. 18. FilesSingle logical compilation unit Inbox.scala
  19. 19. Multiple files Cohesive groupAll multi-unit files should be given camelCase nameswith a lower-case first letter. Help in identification
  20. 20. ScalaDocIt is important to provide documentation for all packages, classes, traits, methods, and other members.
  21. 21. ScalaDoc
  22. 22. Avoid!Method Returns → returns <what>This class does XXX - > Does <what>Links to Scala lib classes as [[scala.option]]@returnDocument what method does not and should not dofirst sentence should be a summary of what the method does.Subsequent sentences explain in further detail
  23. 23. Packages Overview of main classes Examples of how to Use class
  24. 24. Classes, Objects & Traits

×