using enums to generalize set and get functions

Started by
14 comments, last by David Neubelt 16 years, 10 months ago
Quote:Original post by T1Oracle
I don't understand why you didn't just do this:
*** Source Snippet Removed ***


Read my last sentence - you don't know my requirements.

What if Vector3D is internal format, which isn't available to the user and is merely provided for the renderer.

Also, your example no longer fires the event.

And reset position doesn't use Vector's default position, but position defined by particular map.

I also don't need to put inline in front of methods since I use "any suitable" for inlining, and "optimize for speed".

This also demonstrates nicely why it's somewhat pointless to ask others advice on design, without providing exact and detailed use-case.

The enum solution might work - I don't know. It's just uncommon. A map solution might work as well. Or overriding subscript operator. Or auto-generating accessors from IDL. Or providing dynamic type system. Or component oriented object composition.

There are no absolutely "good" or "bad", "right" or "wrong" ways. There is just suitable and unsuitable design. Yes, even singletons have their rightful place in design. They are just frequently unsuitable.
Advertisement
Quote:Original post by Antheus
What if Vector3D is internal format, which isn't available to the user and is merely provided for the renderer.

Then make one that is (you could do a simpler one), it will most certainly be useful. 3D vectors do not need to be limited to the renderer, especially when you have a clear need for it elsewhere. Why not make things easier?
Quote:Original post by Antheus
Also, your example no longer fires the event.

Trivial, you can add two more lines and fix that.
Quote:Original post by Antheus
And reset position doesn't use Vector's default position, but position defined by particular map.

Why the vague terminology? Your example does nothing mysterious. Surely a vector isn't going to add more than 10 seconds of work to get your "particular map" to play nice with it. Meanwhile you get the benefit of only having to pass one variable, (a vector) and the utility of using the same pattern (which surely has use through out your code if you need coordinates in the first place) in other places.
Quote:Original post by Antheus
This also demonstrates nicely why it's somewhat pointless to ask others advice on design, without providing exact and detailed use-case.

I agree although statistics are still a viable tool for reason. The less common is less common for a reason. Therefore, the reason for choosing the less common should be scrutinized more carefully.
Programming since 1995.
The only benefit that I can see is with regards to animation systems.

I have done a similar thing before, property names and types are mapped to integer property IDs, which can then be animated via keyframes or other methods. It is no more fun than writing out all the getters and setters. Especially when you can't use switch since the IDs aren't defined at compile time. I would do it differently if I were to redesign it.
The problem with using many getters/setters OR the approach you propose is that you seperate the data from the algorithms, as the code that deals with the variables get put in a different class.

Objects are supposed to contain both algorithms and data. You either have to ask yourself "Why is this variable in here when the only stuff that touches it is over in that?", or "Why are the functions that touch this variable over in that?".

That said, there are certain Design patterns where this kind of thing makes sense. In that case you should know what the name of the design pattern is, and why you are using it.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
I think it could make sense if you had a massive library that your programs depended on which had dozens of configuration options whose state would need changed at unpredictable intervals.

For instance, with OpenGL you have things like glLightfv(GL_LIGHT5,GL_POSITION,l5pos); which may or may not be easier than glLights[5].position.set(l5pos); or something similar. I suppose it depends on the scope. I certainly wouldn't do it for something like entity health updates, although I could see enumeration for states eg. entity[23].setState(ES_SCARED_SILLY);

But then again, that's an entirely different situation.

If you have many, many variables that need updating, then it might be time for a reorganization. If you have something like:

class Squirrel {  float ambient_r;  float ambient_g;  float ambient_b;  float ambient_a;  float diffuse_r;  ...  float position_x;  float position_y;  float position_z;  float velocity_x;  float velocity_y;  ...  void setHungerLevel(float);  float getHungerLevel();  void setFavoriteFoodisNuts();  void setFavoriteFoodisBeans();  int getFavoriteFood();  ...};

then I would say it's time for reorganization. Can you be more specific in what kinds of variables you have such that you need to completely rearrange how you get and set them?

Note: I'm don't really think you've made anything as bad as that squirrel :)
We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)
Original post by Antheus
I also don't need to put inline in front of methods since I use "any suitable" for inlining, and "optimize for speed".[\quote]
In practice, on most compilers, whole program optimizations aren't that great. I'd be explicit always.

As for the OP question:

That isn't a bad idea for writing interfaces that can be easily extended to support new data without changing the interface to the class. To support a new data type you simply need to supply a new ID (whether it be a enum or a hashed integer).

I also think associative containers like std::map and using a string for the key is very bad in practice. Doing string compares to find data isn't effecient and doesn't scale well with lots of data.

Though, this pattern is used frequently but not really in the context you are describing.

-= Dave



Graphics Programmer - Ready At Dawn Studios

This topic is closed to new replies.

Advertisement