beverlyslis.com beverlyslis.com
Main Page >> About Us >> Add Your Link >> Privacy of Info >> Terms & Conditions >> Add Your Article
Search:   
Add Url
 

Investment & Finance

Property & Agents

Self Help

Children

Lifestyle & Fashion

Food & Recipe

Automotive

News & Media

Health & Hygiene

Travel & Vacation

Politics & Government

Society & Issues

Healthcare & Medicine

Science & Research

Sports

Online & Indoor Games

Internet & Computers

Culture & Art

Music & Entertainment

Careers & Employment

Garden & Home

Education & Reference

Companies & Business

Shopping & Auction

 

Main Page » Internet & Computers » Computer Software
 

Using 'Get' and 'Set' Might Be Something You'll Regret

 
Author: V. Berba Velasco
 

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. See? they declare, as they pat themselves on their backs. The data is private, but it can be accessed through these functions. Ive encapsulated my data. Thats what object-oriented design requires.

I think this betrays a fundamental (and exceedingly 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 hes 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 weakand 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 ideally 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 guessrightfully sothat 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 designone 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 lets 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(). Thats 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 users 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 wouldnt 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() and GetBalance(). With this design, the data memberthe account balanceis logically implied by the services provided, rather than the other way around. (We could define other services, such as AccrueInterest() and TransferFunds(), but lets keep it simple for now. Note that these services might suggest additional data members; for example, AccrueInterest() would suggest that a data member would be needed for the interest rate.)

In summary, get() and set() function pairs are not inherently bad, but their preponderance could suggest a poorly abstracted software design. Theres nothing inherently wrong with using get() and set() functions when they form part of a sensibly designed set of services. Nor is there anything wrong with using a data member to reflect these functions. When one proceeds in the opposite direction, thoughwhen one starts with the data members, then adds corresponding get() and set() pairsthats a strong indication of a poorly conceived software design.

 
 
 

Related Articles

 
All The Way to The Bank With Clickbank
 
How Not To Get Web Design Work
 
Protecting Your Website From Online Thieves - Part 2
 
The Most Profitable Product In The World You Can Ever Sell!
 
Get Targeted Traffic to Your Website!
 
Cisco Routing: "ip Default-network" Vs. Default Static Routes
 
Magnetic Stripe Reader ? A Popular POS Component
 
Strategies To Fight Email Spam
 
Your Hard Drive is Going to Explode - Why a UPS is Essential
 
Computer Memory And The Future
 
 
 
 
 

Are You "Shooting" Your Marketing Dollars with a Shotgun or a Rifle and WHAT's the Difference?

In marketing your services or widgets, what approach do you use? Do you scatter your marketing effor ... - Sandra P. Martini
 

Setting Up Your Own Affiliate Program (Part Two)

This is the second of two articles on setting up an affiliate program. It gives an overview on testi ... - Christopher Ruane
 

Are You A Senior Citizen Afraid Of The Internet? Don't Be - Get In On The Exciting Adventure Now!

There are too many senior citizens who think that the internet is for "The Young & Beautiful." T ... - Wallace Johnson
 
 

Bursting forth from the sandbox to top of the google's rankings

For a considerably long period, Google happened to provide new sites with a temporary boost, known a ... - Deepak Sharma
 

10 Cool Ways To Attract People To Your Website

There are hundreds, if not thousands, of ways to drive traffic to your website; some are free while ... - Chris Brown
 
 
Main Page >> Privacy of Info >> Terms & Conditions
© 2006-2008 www.beverlyslist.com All Rights Reserved Worldwide.