Scala coding styles Guide:- InShort

Table of contents
Reading Time: 3 minutes

We all are using the Scala for a very long time now and sometimes we miss some guidelines for writing the Scala code, well this blog guide you through some common Scala coding styles. Lets get started.

  • Indentation:- Scala follows 2 space indentation instead of 4 spaces, well I guess there will be no fight over Tab and 4 Spaces.
    //WRONG                                //RIGHT              
    class Foo {                            class Foo {          
        def bar = ...                        def bar = ...      
    }                                      }                    
  • Line Wrapping:- There are times when a single expression reaches a length where it becomes unreadable to keep it confined to a single line. Scala coding style prefers if length of a line crosses 80 characters then, split the same in multiple lines i.e.
    val result = 1 + 2 + 3 + 4 + 5 + 6 +
      7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 +
      15 + 16 + 17 + 18 + 19 + 20
  • Methods with Numerous Arguments:- If a function has long or complex parameter lists, follow these rules:
    Put the first parameter on the same line as the function name.
    Put the rest of the parameters each on a new line, aligned with the first parameter.
    If the function has multiple parameter lists, align the opening parenthesis with the previous one and align parameters the same as #2. i.e.
    foo(                           val myLongFieldName =
      someVeryLongFieldName,         foo(
      andAnotherVeryLongFieldName,   someVeryLongFieldName,
      "this is a string",            "this is a string",
      3.1415)                        3.1415)
  • Naming Conventions:- Scala uses “camel case” naming. That is, each word is capitalized, except possibly the first word. Scala prefer to not to use the Underscore in names because Scala has different definition of underscore.
    1. Classes/Traits:- Classes should be named in upper camel case.
      class MyFairLady
    2. Objects:- Object names are like class names (upper camel case).
    3. Packages:- Similar to what Java offers i.e.
      // right! puts only coolness._ in scope
      package com.novell.coolness
      // right! puts both novell._ and coolness._ in scope
      package com.novell
      package coolness
    4. Methods:- Textual (alphabetic) names for methods should be in lower camel case. For getters method name should be as same as the field name and for setter method name should be field name followed by the underscore.If the variable is of type boolean then it can be appended before the field name to create method i.e.
      def myFairMethod = ...
      class Foo {
        def bar = ...
        def bar_=(bar: Bar) {
        def isBaz = ...
    5. Constants, Values, Variable and Methods:- Constant names should be in upper camel case but for variables and methods lower camel case is followed i.e.
      val myValue = ...
      val Pi = 3.14
      def myMethod = ...
      var myVariable = ...
    6. Parentheses:- The opening and closing parentheses should be unspaced and generally kept on the same lines as their content (Lisp-style):
      (this + is a very ++ long *
    7. Curly Braces:- Opening curly braces ({) must be on the same line as the declaration they represent:
      def foo = {
    8. Higher-Order Functions:- Scala Coding preferred style for higher order functions is the exact inverse i.e.
      def foldLeft[A, B](ls: List[A])(init: B)(f: (B, A) => B): B = ...
      foldLeft(List(1, 2, 3, 4))(0)(_ + _)

This blog is intended to summarize some basic Scala style guidelines which should be followed to write more readable code.We have tried to state explanation why a particular style is encouraged. In further blogs we will be discussing about more coding styles in depth till then Happy Coding.

References: Scala docs Style


Written by 

Rachel Jones is a Solutions Lead at Knoldus Inc. having more than 22 years of experience. Rachel likes to delve deeper into the field of AI(Artificial Intelligence) and deep learning. She loves challenges and motivating people, also loves to read novels by Dan Brown. Rachel has problem solving, management and leadership skills moreover, she is familiar with programming languages such as Java, Scala, C++ & Html.

2 thoughts on “Scala coding styles Guide:- InShort3 min read

Comments are closed.