What is this called? And is it a good idea?

Started by
36 comments, last by Shannon Barber 18 years, 3 months ago
Quote:Original post by etothex
I say, encapsulation is where the user of code only needs to know the interface, not the internals. So in my reading, there isn't any problem at all with public member variables, because where does it say "interface" means "function"?


A very good point! I really like this thinking that varaibles can be part of the interface. Just like how you can change the 'time' variable on a microwave directly!

I am now going to go and change a whole lot of my code...
Advertisement
Quote:Original post by Ezbez
Just like how you can change the 'time' variable on a microwave directly!


No, you can't. Try telling your microwave to cook that potato for -5:79.
[size=2]
Quote:Original post by wild_pointer
Quote:Original post by Dmytry
Well, in practically all cases of use of getters and setters that only get and set value (but do nothing else), them is used only because "this way i place variable that is de-facto public into 'private' and so i can pretend that i'm are encapsulating something".


The use of accessors/mutators has nothing to do with encapsulation. They can break encapsulation if used sloppily. Public data doesn't even necessarily violate encapsulation though it's bad for other reasons (reasons accessors/mutators and properties exist).

my point is that usually get and set is used to achieve nice warm feeling of encapsulation, whereas it practically doesn't encapsulate anything. Like in OP's code.
Or like GetX GetY GetZ SetX SetY SetZ in 3d vector. Totally useless. Doesn't encapsulate anything at all. In case of vector, it isn't bad though as having x,y,z it is part of what we want from 3D vectors... "encapsulating" x,y,z it's like placing dot product and cross product in the private.

Some people learn how to write well-designed code(with encapsulation where necessary and everything), some people learn how to pretend for themselves that they're writing well designed code (with encapsulation and everything), and it is usually achieved by mimicking little details but not large details, or not seeing trees for forest. So they must unlearn what they have learned if they want to really do it. It's same with any other learning.

IMO _ideally_ code "inside" the class should deal with low level concepts and provide higher level concept(s), and code outside should deal with said higher hevel concept(s). The get and set doesn't give much higher level concept than just variable.

[Edited by - Dmytry on December 30, 2005 9:05:51 AM]
Quote:Original post by wild_pointer
Quote:Original post by Ezbez
Just like how you can change the 'time' variable on a microwave directly!


No, you can't. Try telling your microwave to cook that potato for -5:79.

unsigned int cooking_time_in_seconds;
Quote:Original post by Dmytry
Or like GetX GetY GetZ SetX SetY SetZ in 3d vector. Totally useless. Doesn't encapsulate anything at all. In case of vector, it isn't bad though as having x,y,z it is part of what we want from 3D vectors... "encapsulating" x,y,z it's like placing dot product and cross product in the private.


Sure it does. You now have the option of storing the coordinates as 3 ints, 3 floats, an array, a bit vector, etc. Hell, you could use polar coordinates if you wanted.

Quote:Original post by Dmytry
Quote:Original post by wild_pointer
Quote:Original post by Ezbez
Just like how you can change the 'time' variable on a microwave directly!


No, you can't. Try telling your microwave to cook that potato for -5:79.

unsigned int cooking_time_in_seconds;


I don't know of any microwave where you input the cooking times of greater than one minute in seconds. On mine, in fact, you press a number 1-9 and it puts that many minutes up and begins, then you can press a +30 seconds button. Internally it may very well be stored in seconds, the point is that we don't know.
[size=2]
Quote:Original post by wild_pointer
Quote:Original post by Dmytry
Quote:Original post by wild_pointer
Quote:Original post by Ezbez
Just like how you can change the 'time' variable on a microwave directly!


No, you can't. Try telling your microwave to cook that potato for -5:79.

unsigned int cooking_time_in_seconds;


I don't know of any microwave where you input the cooking times of greater than one minute in seconds. On mine, in fact, you press a number 1-9 and it puts that many minutes up and begins, then you can press a +30 seconds button.

Well, with mirowave of course you have complicated display function, and complicated setting interface (buttons). That's because details inside is charges on the memory chip; and to manipulate directly you would need to open chip and put charges on it, say with charged pin and microscope.

But the getter and setter is very different from complicated display interface and complicated manipulation interface. It would be analogous to making microwave that as user interface has two other open chips exposed to outside (for getting and setting using pin and microscope). It is not much better than manipulating with chip directly, the only thing that it can give is possibility to do something when value is modified, say beep when bits is changed, and possibility to replace chip inside with other model but leave the chips outside unchanged.
Quote:Original post by wild_pointer
I don't know of any microwave where you input the cooking times of greater than one minute in seconds. On mine, in fact, you press a number 1-9 and it puts that many minutes up and begins, then you can press a +30 seconds button.

My microwave only inputs seconds, but that's not the point. *If* you are considering replacing the public variable with a handful of naive mutators, then there's a resonably good chance LengthOfTime CookingTime; will work rather well. However, since setting the cooking time is coupled with an action, both methods are probably shitty solutions to the question of how to design a Microwave class.

We can bicker about this all day, but that doesn't address the OP in the slightest. What are people's thoughts on the BBBs scheme?

CM
Quote:
hat are people's thoughts on the BBBs scheme?

bad. and pointless. Don't do it. My suggestion for BBB is to unlearn "public members is bad" and learn why public members can be bad and when.
I have a few get/set methods around in my code. However none look like this:
void setData(const Data& p_data) {  m_data = p_data;}

Most getData's look something like this:
void setData(const Data& p_data) {  // assert on data  m_data = p_data;  // send data to hardware}

roughly the same for getData's:
Data& getData() const {  // assert on m_data  // assert on m_data != hardware data  return m_data;}
Quote:Original post by wild_pointer
Quote:Original post by Dmytry
Or like GetX GetY GetZ SetX SetY SetZ in 3d vector. Totally useless. Doesn't encapsulate anything at all. In case of vector, it isn't bad though as having x,y,z it is part of what we want from 3D vectors... "encapsulating" x,y,z it's like placing dot product and cross product in the private.


Sure it does. You now have the option of storing the coordinates as 3 ints, 3 floats, an array, a bit vector, etc. Hell, you could use polar coordinates if you wanted.

Changing your internal representation to int or float or a bit vector all fundementally change the behavior of your class. All you've done by hiding them behind mutators is allow the resulting bugs to compile. Storing them as an array is rather pointless, and if need be it is possible to create public x,y,and z variables that reference the array anyhow . <br><br>I agree that the polar coordinates point is a valid &#111;ne, but trust me when I say you've already lost that argument.<br><br>CM

This topic is closed to new replies.

Advertisement