Should I use `()` or not for the `getClients` method when the return value can be changed?

In Scala, I have a class serverthat has a method, say getClients, that returns the currently connected clients.

I am not sure how to determine this:

  • getClients
  • clients
  • getClients()
  • clients()

The return value of this method changes over time when new clients connect or disconnect.

Which one to choose?

+4
source share
3 answers

When it comes to parentheses, it is important whether the method has side effects or not:

From Scala styleguide ( http://docs.scala-lang.org/style/naming-conventions.html#parentheses )

, ( , ), , , .

, , . , accessor, arity-0 (arity-0 - !). , .

clients getClients - - , . get Scala, clients.

+8

Scala, :

, ( , ), , , .

( getClients, clients, , ), . , , , .

, , , - .

+6

The new standard that I saw around seems reasonable, is to define it only with () if it has side effects and not prefix it with get. If you follow the agreement, it would mean that you would use 2.

+2
source

Source: https://habr.com/ru/post/1589411/


All Articles