-2

I am familiar with "Scala coding styles and conventions?"

I also have read this article which states that a programmer is able to produce only so many lines of code per day and therefore a language with a "better" syntax can only help in this direction.

As there is nothing perfect in the world, the syntax of Scala has its disadvantages (of course someone can see these as pure advantages). I am talking about stuff like:

def someMethod(someParameter: Int): Boolean = {

    // import here in the middle of nowhere
    import com.example.bad.VeryBad._

    // semicolon here? maybe?
    val two = 1+1;

    // nested function here
    def boo(a: Int) = a+1;

    // now do something useful
    println(boo(two))

    // for God's sake don't use return!!!
    two == 2
}

If one has Java background (or even C++, C#, etc.) it is kind of weird to have import statements in the middle of a function, to have nested functions and I am not even going into other language constructs.

I am a bit sceptical about the real productivity gain coming with Scala. How do you experience this? Do you apply all (as much as possible) of the coding conventions and is this a real benefit? Is there a better way to wrap ones head around this style of code?

P.S. maybe this whole post can be moved to Programmers or similar...

Community
  • 1
  • 1
Anton Sarov
  • 3,712
  • 3
  • 31
  • 48

3 Answers3

1

I'll go step by step, although some points could be subject to personal opinion:

  • Imports: there are some cases where this is pretty useful, for example to shorten you code in case of constants:

    object Constants { val someConstant = "123" }
    
    def someMethod(someParameter: Int): Boolean = {
      import Constants._
      doSomething(someConstant)
    }
    

    this also gives the reader a hint on where is that someConstant coming from, having it in the top declaration can be not so explicit.

  • Semicolon: nobody uses them in scala, the only useful application which comes to mind is if you want to write multiple statement in one line

    val a = 1; val b= 2
    

    Not that useful in the end.

  • Nested methods: these are perfectly fine, wether you should use it or not vary on your use case, also there are around many example of recursive or tail recursive functions which use nested methods.

  • There's no need to use return, I've never seen it used in my experience plus it can be quite confusing in some cases.

Ende Neu
  • 15,581
  • 5
  • 57
  • 68
1

I can share only my experience and I don't know whether other people's experiences are similar. It certainly depends on personal preferences.

Imports in blocks

I came to Scala from Java, so my preference in general is to have all imports at the top of the file. This makes it easier for me to faster see the dependencies of the files. Sometimes however I do use local import in places where I want to import a name only locally and not to pollute the namespace of the whole file.

Semicolons

After few weeks in Scala I really enjoy the the lack of mandatory semicolons. In Scala they are mostly not needed, but they can be helpful when you want to write more short statements on one line. Scala actually changes my attitude towards semicolons and now in new projects I don't use semicolons even in JavaScript.

Nested functions

This is one of the best features of Scala: Functional programming. It allows to write much simpler code consisting of small easily understandable functions working with immutable data. This decreases the complexity of the code, reduces bugs and increases the productivity.

Returns

Again, lack of return statements feels just natural. The function returns the value of its expression, where the value of a sequence of statements/expressions is the last one of them. Return statements just feel very unnatural in Scala.

Productivity

Scala has a learning curve and of course when I have started my productivity had a dent caused by my lack of familiarity with the language and the standard library. Now, after few months of Java and Scala work, I can say that I am in more productive in Scala than in Java. Even though I consider myself a Java expert but only a Scala novice.

Gregor Raýman
  • 3,051
  • 13
  • 19
0

Using such imports for implicits is useful in many cases.

For this usage, importing makes code safer and more readable.

Second case would be ambiguous class and package names (can be solved via aliases). Showing explicite which one is using can be helpful.

In Scala you can do many things in many ways, a little bit like with C++. You have more responsibility for your coding style. Watch this: https://www.youtube.com/watch?v=kkTFx3-duc8&feature=share

Nested methods are great for recurrence and when such method should not be used even as private at class level. With nesting you can also reduce amount of passed arguments.

If you can you then you should place you nested methods on the top of method body. Giving comments on where is constructor body at big class is also helpful.

Scala gives to community the ability to develop the language, scala can change in fundamental ways due to our experiments, good practices.

Waldemar Wosiński
  • 1,490
  • 17
  • 28