• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

get and set functions are evil.

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Allen Holub says that get/set (They're just elaborate ways to make the data public.)
I would like comment from you all
thanks
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My position is very well presented by http://c2.com/cgi/wiki?TellDontAsk
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To me, the main benefit of using get/set is that you can decouple calling code from knowing about how a particular value is stored or calculated. Consider the following simple contrived example:

Note how the external get/set API for euros and dollars is the same, but one is converted at "set" time, and the other at "get" time. using this sort of get/set tricks removes the need for client code to care about such subtle distinctions.
Please don't take this as aimplying that I love get/set pairs and the whole "make every class a JavaBean, just in case..." thing. Wherever possible, I use the same assignment of responsibilities as advocated by Ilja. You should remain aware though, that every rule has its exceptions.
 
author
Posts: 3892
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Absolutely. You've rediscovered Reusability through self-encapsulation.
Kyle
 
vishal sodani
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A snapshot from an article--
http://www.pragmaticprogrammer.com/ppllc/papers/1998_05.html
Tell,Dont Ask
Sure, you may say, that's obvious. I'd never write code like that. Still, it's very easy to get lulled into examining some referenced object and then calling different methods based on the results. But that may not be the best way to go about doing it. Tell the object what you want. Let it figure out how to do it.

Think declaratively instead of procedurally!
----------------
Now,in a case where employee can be either bargainable or officer,and depending on his type his salary is caculated.So,i should tell an employee object to calculate its own salary.So,employee object will create a salary object.right?

Another situation---
Now,I have an entry form to update employee information-before updation I need to check that EmployeeID entered by user exists.What kind of object should have this responsibility,not an employee object,I assume.If employeeid exists,then I create an employee object with the EmployeeID entered,and call the update method.Am I thinking right?
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by vishal sodani:
What kind of object should have this responsibility,not an employee object,I assume.


Depends on the rest of the system - could be a Company, EmployeeRegistry or something else.

If employeeid exists,then I create an employee object with the EmployeeID entered,and call the update method.Am I thinking right?


Sounds reasonable.
 
Those cherries would go best on cherry cheesecake. Don't put those cherries on this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic