Why hide data?

Check the definition of data hiding by Search SQL Server.

Depending on what you want to program, you may encounter smalls problems that makes you to lose focus on your main problem. And if these problems are not solved wisely, it can make your code a bit messy and unfocused on a main goal.

Suppose I’m coding a program to add an item to the shopping cart.

Lower level of abstraction (code):

void main(){
	int quantity;
	int item_id;
	String name;
        DataBase db;

	quantity = 3;
	item_id = 22910;
	name = "Pen Drive";

	db.include(quantity, item_id, name);

	quantity = 1;
	item_id = 9027;
	name = "Headset";

	db.include(quantity, item_id, name);
}

Higher level of abstraction (code):

class ShoppingCart{
	private:
		DataBase db;
	public:
		void include(int quantity, int item_id, String name){
			db.include(quantity, item_id, name);
		}
};

void main(){
	ShoppingCart cart;

	cart.include(3, 22910, "Pen Drive");
	cart.include(1, 9027, "Headset");
}

Note that the problem of variables and database has been hidden (abstracted) in a class, then I can just worry about adding items to the shopping cart.

Thus, data hiding is important for you to get a better focus and so that others can understand the main goal of your code.


					
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s