Alex Rivera | Logout

Use of public fields in Play Framework 2.0

Asked 2012-03-30T02:52:14.923
8

In Play Framework 1.x, the convention is to use public fields on Java classes. The justification for this is because of how the Play Properties Enhancers work as described here: http://www.playframework.org/documentation/1.2.4/model

In a nutshell, public fields are 'ok' because Play auto-generates setters and getters at runtime. That makes sense to me and there are other questions that cover that.

Play Framework 2.0 works very differently. There is no "Properties Simulation" capability. Maybe they are looking at adding this later but I could not find anything to suggest this. Without the properties simulation, the original justification for using all public fields is gone. The Play Framework 2.0 samples still use public fields though: http://www.playframework.org/documentation/2.0/JavaEbean

Why are public fields still recommended for playframework 2.0? Is this just a habit of developers on the old version of play who created the samples or is there another reason why the use of public fields is still recommended in Play 2.0?

Edit
Report

1 Answer

0

I guess it's because private fields with public getters and setters only generate line noise and don't add any real value. In Scala, getters and setters are not visible either, they too are auto-generated. Ex.:

class Person(var name: String)
val a = new Person("John Smith")
a.name = "Henry Smith"

Play was inspired by Rails, and Ruby also has a syntax for auto-generating getters and setters:

class Person
  attr_accessor :name
end

person = Person.new
person.name = "John Henry"
answered 2012-03-30T11:01:12.833

Your Answer