Thursday, March 21, 2013

Does Java lack auto generated properties?

Is Java that bad in compliance to OOP pillars?
I had a few conversations with OOP developers in my community just recently and often it sounds like people want to have some new features as a part of the programming language they are using.
So we have auto properties as a part of C# 3.0, attribute accessors in Ruby and simple old getAttribute() methods in Java.
Now, we often expose internal state of the object to the outside world which I don't think is a good pattern for most cases. And as far as we're tired of writing those getMethods and setMethods all over we're trying to increase the productivity while writing those. Without consideration that most business actions affect a few properties at a time.
My take is we should expose methods that represent a business action like add two bags of potatoes to my shopping cart. The number of items, item type, SKU is integral for the action. 
So what if we change the process of adding an item to the shopping cart? For instance now we need to change the last update date of the shopping cart object every time we add something to it. If we were to encapsulate the change logic around the shopping cart object we would get a one line code change. Otherwise we are to change all the consumers of the object. Which I consider is a code duplication. Bad smell it is.
In this case I think languages such as Ruby and C# provide you both the toolset and the responsibility to make decisions on how to implement the solution. Java on the other hand gives you the only way to implement it property. But relies on the tools (all Java IDEs can generate property stubs) to make the development productive.

1 comment:

  1. "Now, we often expose internal state of the object to the outside world which I don't think is a good pattern for most cases."

    Have to disagree, in "some cases", domain modelling is just one tiny case for that. Don't forget about DTO's and ton's of other cases when properties simplifies your life. As a language feature - yes, Java lacks auto generated properties.

    ReplyDelete