score:46
It's mostly a question of convention. Methods with empty parameter lists are, by convention, evaluated for their side-effects. Methods without parameters are assumed to be side-effect free. That's the convention.
score:0
I have another light to bring to the usefulness of the convention encouraging an empty parentheses block in the declaration of functions (and thus later in calls to them) with side effects.
It is with the debugger.
If one add a watch in a debugger, such as, say, process
referring for the example to a boolean in the focused debug context, either as a variable view, or as a pure side-effect free function evaluation, it creates a nasty risk for your later troubleshooting.
Indeed, if the debugger keeps that watch as a try-to-evaluate thing whenever you change the context (change thread, move in the call stack, reach another breakpoint...), which I found to be at least the case with IntelliJ IDEA, or Visual Studio for other languages, then the side-effects of any other process
function possibly found in any browsed scope would be triggered...
Just imagine the kind of puzzling troubleshooting this could lead to if you do not have that warning just in mind, because of some innocent regular naming. If the convention were enforced, with my example, the process
boolean evaluation would never fall back to a process()
function call in the debugger watches; it might just be allowed in your debugger to explicitly access the () function putting process()
in the watches, but then it would be clear you are not directly accessing any attribute or local variables, and fallbacks to other process()
functions in other browsed scopes, if maybe unlucky, would at the very least be very less surprising.
score:1
Other answers are great, but I also think it's worth mentioning that no-param methods allow for nice access to a classes fields, like so:
person.name
Because of parameterless methods, you could easily write a method to intercept reads (or writes) to the 'name' field without breaking calling code, like so
def name = { log("Accessing name!"); _name }
This is called the Uniform Access Principal
score:15
Scala Style Guide says to omit parentheses only when the method being called has no side-effects: http://docs.scala-lang.org/style/method-invocation.html
Source: stackoverflow.com
Related Query
- Why to use empty parentheses in Scala if we can just use no parentheses to define a function which does not need any arguments?
- Why does Scala use a reversed shebang (!#) instead of just setting interpreter to scala
- Why can I use a Java lib from the Scala REPL but not from a script?
- Can you define a value (in a if) in a for comprehension in Scala for use in yield
- Can we define a set of DSL operation in Scala that perform parallelly with each other just like using pipe-line processing in Linux
- Why in Scala function defined without empty parentheses doesn't behave like function?
- Using Scala Toolbox eval, how do I define I value I can use in later evals?
- Why can i not use greater than '>=' in Partition Parentheses in Spark SQL
- What are Scala continuations and why use them?
- When and why should one use Applicative Functors in Scala
- Why can a Scala trait extend a class?
- How to use Scala in IntelliJ IDEA (or: why is it so difficult to get a working IDE for Scala)?
- How can I use a Scala singleton object in Java?
- Why does Scala use a ForkJoinPool to back its default ExecutionContext?
- Why (copy) appending to Seq in Scala is defined as :+ and not just + as in Set and Map?
- How can I define an anonymous generic Scala function?
- Can I use a scala class which implements a java interface from Java?
- Spark: Why does Python significantly outperform Scala in my use case?
- Can I define and use functions outside classes and objects in Scala?
- How to define and use custom annotations in Scala
- Can a Scala "extractor" use generics on unapply?
- How can I use Gson in Scala to serialize a List?
- Why can auxiliary constructors in Scala only consist of a single call to another constructor?
- why use def and val in Scala or vice versa
- Can I use the Scala lift-json library to parse a JSON into a Map?
- Why in Scala Long cannot in initialized to null whear as Integer can
- How to use just one scala library for maven/eclipse/scala
- How can I use JMH for Scala benchmarks together with sbt?
- Why is the Empty input case needed in Scala Iteratees?
- Is it correct to define all classes as cases in Scala just to have all their arguments made properties automatically?
More Query from same tag
- Combinatorics in Scala: How to iterate/enumerate all possibilities to merge multiple sequences/lists (riffle shuffle permutations)
- error: value map is not a member of (sbt.TaskKey[sbt.File], sbt.TaskKey[sbt.Keys.TaskStreams])
- scala implementing binary tree end nodes using Option
- Sorting multiple fields in an object using scala sortWith function
- Nashorn and Scala future to JS Promise conversion
- "val a:A = new B ", what's the point?
- How to represent contravariant type parameter using bounds
- Not able to mock the getTimestamp method on mocked Row
- ArrayBuffer ++= operation with Array for val type attribute
- Is it possible to limit a generic parameter to a subset of a super type?
- How to make a List[String] to be a list of strings
- View SQL query in Slick
- How to connect a PCIe device to a chipyard design
- wrapping an asynchronous process in an akka actor
- flatten Vs flatMap with def method and val function
- Object life cycle within a closure
- Is there some reason to avoid return statements
- How to handle cross cutting concerns the Scala way
- Spark "ERROR EndpointWriter: AssociationError" when driver's port and address are set manually
- foldLeft, foldRight difference on List Accumulator using Scala
- Declaring Actor state variables as mutable ones
- NullPointerException when testing controller in Play 2.6
- Upgrade ReactiveMongo Play 2.3 app from 2.6 to 3.0
- Spark SQL average non zero values
- How can I set the heap size of spray-can in Sbt's Build.scala?
- Filter using Nested Struct Field
- How to make IntelliJ Scala plugin run the unit test, or how to teach IntelliJ that a file is a unit test code?
- How to handle session using Scala Play framework?
- Generic method convertible to structural type in scala
- Scala Infix Type Puzzle