Wednesday, October 9, 2013

Byte Code Enhancement: Eliminates noise for you

In one of our pairing session, I wrote couple of Model classes and public fields. You guessed it right my pair started lecturing me on encapsulation and why it's important to hide the internal representation of an object.  :D 

I am not going into the details of why and what was wrong with exposing the public field. We all know it and pretty much following the javaBean specification all the while for a reason. In gist, we want to save our internal state and don't want anybody to destroy it unintentionally or on purpose.

I assured him that Playframework will takecare of his concerns and will replace the public fields reference with getters and setters right after your original code is compiled by slightly rewriting the byte code.

Playframework helps you eliminate all these boilerplate code where you’d normally write a simple getter and setter, and allows you to just use public fields whenever you DON'T require special logic in a getter and/or setter.

You don’t have to use it, if you don’t want to, you can just create your own getters and setters and Playframework won’t touch your classes.

I would recommend to think little longer if you decide to export or re-use your class outside your application because Playframework does it's magic after class is complied therefore if you ever change your mind to use setters and getters, you have to rewrite your client code. 

Have fun
Manisha

No comments:

Post a Comment