ScalaGettersSettersAntipattern.scala

package com.kornelcsaszar

object ScalaGettersSettersVsCaseClasses extends App{

  //GETTERS/SETTERS
  class Name{
    //Bad: look at all the boilerplate
    var _first: String = ""
    var _last: String = ""
    def first_= (f: String): Unit = _first = f
    def first = _first
    def last_= (l: String): Unit = _last = l
    def last = _last
    override def toString(): String = s"${_first} ${_last}"
  }
  val n = new Name()
  n.first = "joe"
  n.last = "smith"
  def doSomethingWithName(name: Name): Unit = {
    //Bad: now we have to synchronize if there are multiple threads at work
    name.synchronized{
      println(n)
    }
  }
  doSomethingWithName(n)

  //CASE CLASSES
  case class NameCC(first: String, last: String)
  val n2 = NameCC("joe", "smith")
  def doSomethingWithName2(name: NameCC) = {
    println(name)
  }
  doSomethingWithName2(n2)

}