-3

I am start working on existing Scala and Akka project. I say Scala classes, some fields making them as private and providing getter and setter methods in different way. why like that you can make it as public also right.

I say in my project

Why this way:

class Person() {  
 // Private age variable, renamed to _age 
 private var _age = 0 
 var name = "" 

 // Getter 
 def age = _age 

 // Setter 
 def age_= (value:Int):Unit = _age = value 
} 

So you can get same feeling like public:

person.age = 99  

Why not this simple way?

class Person() {  
 var name = "" 
 var age = 0 
} 

// Instantiate a person object 
person = new Person() 

// Print the object's age and name properties 
println(person.age)  
println(person.name) 


// Set the properties to different values 
person.age = 34  
person.name = "Dustin Martin" 
halfer
  • 19,824
  • 17
  • 99
  • 186
Sun
  • 3,444
  • 7
  • 53
  • 83

4 Answers4

3

For this specific case there is no reason. If it's from a real project, this was probably copied from an example by someone who didn't understand its point. You only want to use explicit getter/setter methods in Scala when they do something other than just mutating a private variable.

(And as Dima's answer correctly says, you should try not to have a var in the first place.)

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
2

Both ways are bad. Avoid mutable members in classes unless you have a specific reason to use them in a given case.

 case class Person(name: String, age: Int) {
   def rename(newName: String) = copy(newName)
   def grow(numYears: Int) = copy(age = this.age + numYears)
 }

 val teen = Person("John", 15) 
 val adult = teen.grow(5)

etc.

Dima
  • 39,570
  • 6
  • 44
  • 70
1

Accessing and mutating data members of the class directly is the bad idea. Thats why its a good practice to to declare the mutable data member as private and provide public getter and setter to access and mutate it (data member).

1) Its a good practice because access to the mutable state should be controlled

2) You can add some validation in the getter and setter like this

for getter

def age = if (_age >= 18) _age else throw new Exception("minor")

for setter

def age_=(age: Int) = if (age < 18) throw new Exception("minor") else _age = age 
Nagarjuna Pamu
  • 14,737
  • 3
  • 22
  • 40
  • 3
    No, this isn't at all a good practice in Scala. The question http://stackoverflow.com/questions/1568091/why-use-getters-and-setters isn't relevant because `var` is already a getter/setter method pair (and a private field), not a public field. When you want e.g. validation, or to only provide a getter for a mutable member, then use explicit getters/setters, not before. – Alexey Romanov Oct 15 '16 at 20:52
  • 1
    "It's good practice" isn't really a reason without some sort of justification. – dhg Oct 15 '16 at 21:11
0

Well it mainly talks about encapsulation. According to Object world, You should achieve a state only through an action. For example I can't reach 100kmph with my car without pushing the accelerator . So the car speed 100kmph(state) is achieved only by acceleration (action).

scala style of getters and setters may give you little confusion but thats all syntactical confusion(during initial days) but Scala does no compromise on encapsulation. you can still use java style of getters and setters like below

class Car (@BeanProperty val model:String, @BeanProperty val  make:String) {

}

Java Bean style of getters and setters would be generated dynamically.But @BeanProperty is meant for Java interoperability.For more info

Community
  • 1
  • 1
Balaji Reddy
  • 5,576
  • 3
  • 36
  • 47