Work At Home Business Website
...Making an Internet Based, Home Business Income, Easy for Everyone Worldwide...


Add To Favorites


 

Font Size

Translate To English Translate To German Translate To Spanish Translate To French Translate To Italian Translate To Russian Translate To Portuguese Translate To Japanese Translate To Korean Translate To Chinese

Translate To Arabic


Search For:   In: 
Using 'Get' And 'set'? Might Be Something You'Ll Regret
Submitted By: V. Berba Velasco Jr. <--More?
Category: Software | Date Posted: 2006-07-31
Page Views: 7 | Rating: (?) Not Yet Rated | Wordcount: 667


Its an all-too-common pitfall. Programmers who attempt to write object-oriented code decide to make all of their data variables private, while creating public get() and set() function pairs for each one. SSee? they declare, as they pat themselves on their backs. SThe data is private, but it can be accessed through these functions. I"ve encapsulated my data. That"s what object-oriented design requires.

I think this betrays a fundamental (and exceedingl.common) misunderstanding of what constitutes encapsulation and information hiding. Ideally, in an object-oriented design, the user of a software class should have no idea what type of data the class contains. As far as he"s concerned, the class should simply provide the required services, and its internal details should be invisible. However, when we provide get() and set() function pairs for each member variable, we are basically announcing the existence of this data to the world. The information hiding provided by this class is weak and arguably non-existent. One might change the name, data type or implementation of this member variable, but its existence remains painfully obvious to everyone.

(Note that I say that the user should Sideally be oblivious to the type of data contained within the class. In practice, this is not always feasible. If we were to implement a BankAccount class, for example, most programmers would immediately guess that this class would have the account balance as one of its data members. They would also guess rightfully so that the owner of this account is likely to be data member as well. Nevertheless, we should avoid needlessly divulging such details, especially when the contents of a class are not so easily discerned.)

In effect, these get() and set() member functions are almost as bad as having public data. Sure, they may provide some useful services, for example, the set() function could validate the data, ensuring that it is within the proper range of values. Nevertheless, they tend to violate the spirit of information hiding, at least to some degree.

Using get() and set() functions is not always a bad idea, in fact, sometimes they make perfect sense. Their frequent use, however, is a pretty good indication of a poorly abstracted software design one that does not adhere well to the principle of information hiding. When designing software classes, we need to think primarily in terms of the services they provide (i.e. their public functions), rather than the data they contain. Ideally, their data implementations should proceed from the required services, rather than other way around.

To illustrate this concept, let us revisit the BankAccount class mentioned earlier. It is clear that the BankAccount object will need to keep track of its balance, so let"s make that a data member. A novice programmer (or one with a poor grasp of object orientation) may choose to provide both GetBalance() and SetBalance() functions for this class but does this make sense? What happens when a user attempts to make a deposit? The user would have to call GetBalance(), add the deposit amount, and then set the new value using GetBalance(). That"s not how real users operate, though, in the real world, there is no need for the depositor to know how much money is in the account. This class design gives the user too much information and requires too many steps.

Similarly, to perform a withdrawal, the user would need to call GetBalance(), subtract the withdrawal amount, and then call SetBalance() to update the account. This also requires too much work on the user"s part. In the real world, the user should not always be required to know exactly how much money is in the account. This person might want to know that there are sufficient funds, but he wouldn"t necessarily need to know the precise balance, and this information should not be a constant requirement.

A sensibly written class should provide the following services instead: DepositAmount(), WithdrawAmount() ahref= elispot-analyzers.de ELISPOT analysers, elispot.cn cryopreserved PBMCs and elispot.co.jp serum-free cell culture media.


Bookmark This Article
Click Here To Post a Comment

Article Tags:

data    class    provide    functions    member    account    getbalance    design    object-oriented    function    required    bankaccount    balance    software    type    example    rather    ideally    guess    programmers    setbalance    deposit    
  Sponsored Listings

Article Comments: 0


Place Your Comments Below
Enter links to your site, resources, or e-mail like this below
and we will make them active. No HTML allowed.
http://www.YourSite.com/      mailto:You@YourSite.com
NOTE: No e-mail harvester can spider your address from this site!

Title:     Date: 2008/12/01/    
Log in to post or
Sign Up

Home Page or

Rating: (?) Not Yet Rated
Please Rate this Article:
 
Click the XML Icon Above to Receive Software Articles Via RSS!
Click Here to copy our own RSS reader you can load on your site.
Click Here to see how this category looks.

HomeAdd To Favorites | Internet Based Business | Home Based Jobs | Home Based Business | Website Marketing | Article Library
Coastal Vacations | Site Build It | WAHBWS Blog | Forum | Free Biz Books |
Classifieds | Business Opportunity Classifieds
ebay Secrets | Blogging For Dollars | Entrepreneur Club | Internet Biz Bootcamp | Email Marketing | Search Marketing Lab

 
A Portion Of Our Business Profits Help Support The San Diego Rescue Mission. Please Consider Donating As Well.

Privacy Policy | Earnings Disclaimer | Contact Us | Tell A Friend | Link To Us | Search Site | RSS Free Content
Domain Registration | Website Hosting | Search Engine Optimization | Free Recipes | Free e-Greetings

Cigars Tobacco

Work At Home Business Website
9518 Mission Gorge Road Box 711116
Santee, California 92072
(801) 992-2110
Contact Us