Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
raj malhotra
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic