Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is encapsulation violated, if I use a global variable in a class member function's definition?

I've been asked to explain what encapsulation is and I replied "bundling of data and functions that modify this data, is called encapsulation."

The answer was followed by another question—"So, by your definition if I modify a global variable from a member function of a class then the encapsulation is violated."

It made sense to answer YES.

I am not sure whether my explanation is wrong or following question is valid and my answer to it as YES is correct.

Can somebody help.

like image 907
pasha Avatar asked Mar 05 '16 18:03

pasha


People also ask

What variables violate encapsulation?

1 Answer. Explanation: Global variables almost always violates the principles of encapsulation. Encapsulation says the data should be accessed only by required set of elements. But global variable is accessible everywhere, also it is most prone to changes.

Is it bad to use global variables in Java?

Using global variables means they are visible to many classes who can manipulate the data then. So you will have to take care of your data is it is widely visible. And if you are using multithreading then you are in trouble as anybody can modify that data, so lots of scope for data getting corrupted.

Why global variables should be avoided?

Using global variables causes very tight coupling of code. Using global variables causes namespace pollution. This may lead to unnecessarily reassigning a global value. Testing in programs using global variables can be a huge pain as it is difficult to decouple them when testing.

Are global variables unsafe?

Non-const global variables are evil because their value can be changed by any function. Using global variables reduces the modularity and flexibility of the program. It is suggested not to use global variables in the program. Instead of using global variables, use local variables in the program.


2 Answers

Quoting from wikipedia:

In programming languages, encapsulation is used to refer to one of two related but distinct notions, and sometimes to the combination thereof:

  • A language mechanism for restricting access to some of the object's components.
  • A language construct that facilitates the bundling of data with the methods (or other functions) operating on that data

In my humble opinion the answer to the follow up question is subjective and it depends on the interpretation of the notion of encapsulation.

For example it's not a violation if the encapsulating data are limited to be the member variables of classes. A global variable that doesn't belong to an object is accessible by everyone and thus, accessing it via a member function doesn't consist any encapsulation violation.

On the other hand if you consider that encapsulation should be applied to your entire program then this global variable should have been bundled to an object and thus, raw access to it constitutes an encapsulation violation.

The bottom line is that the answer lies in the realms of theology, meaning that it depends on how encapsulation is interpreted by the different programming dogmas.

like image 180
101010 Avatar answered Oct 25 '22 09:10

101010


This depends on how global variable defined and accessed.

Imagine header file containing declaration, but not definitions of member functions, and corresponding implementation file containing class members implementation.

Now consider global variable defined in this header file as internal linkage one (static). Or placed in unnamed namespace. It is a global variable, but functionally it does not differ from private static class member.

It is smelly code, but, I say, that variable is encapsulated properly:

like image 41
Revolver_Ocelot Avatar answered Oct 25 '22 09:10

Revolver_Ocelot