GeeCON Prague 2014*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Does the given code violate any OOP principle or Is there any code smell in it? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Does the given code violate any OOP principle or Is there any code smell in it?" Watch "Does the given code violate any OOP principle or Is there any code smell in it?" New topic
Author

Does the given code violate any OOP principle or Is there any code smell in it?

raj malhotra
Ranch Hand

Joined: Feb 22, 2007
Posts: 285
HI,
I have a sample code below.In which i fetch collection of half filled PrefixBeans from database.Then i use a service class to derive the remaining properties of the prefixbean and update the bean.My doubt is since the remaining bean properties are dervied from one or tow bean properties.Should i write the derivation logic in the bean getters or is it ok to calculate the remaining properties in the service class.?If i do in the service class, is it violating any OOP principle?Do you find any code smell here?

The PefixBean will contain the organizationPrefix and existingItemCount already filled by the DAO.



The below service method is used to calculate the remaing fields of the PrefixBean.

James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1025
    
    5

Hi

I think your question is answered when you consider another class/service wanting to call the same DAO method and expecting the derived properties to be set.
raj malhotra
Ranch Hand

Joined: Feb 22, 2007
Posts: 285
Hi James,

Thanks for reply.Yes you are correct.Then i have to recalculate the properties for other service.I noticed one more thing , even if only one service method is going to use this DAO but still the current code couples the userid with the derived properties.So unless you supply userid you cannot get the derived properties.

Thanks,
Raj
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1025
    
    5

Raj

In terms of OOP, if you have derived fields, then you shouldn't expose setter methods for them. Your current code allows any calling party to set the value of prefixCapacity to whatever they like.
raj malhotra
Ranch Hand

Joined: Feb 22, 2007
Posts: 285
Yes there should not be setter methods for a derived field.This was just a dummy code since i didn't want to paste original source code from my project. And in a hurry i generated all setter and getters.
Thanks for looking into the code.

-Raj
 
GeeCON Prague 2014
 
subject: Does the given code violate any OOP principle or Is there any code smell in it?