Worth Get() and Set() in games ?

Started by
30 comments, last by AndyTX 19 years, 9 months ago
Quote:You can turn a private function into a publicly callable delegate.


Thanks for your reply. I don't suppose you could give me an example?

Either way, this doesn't sound like a very satisfying solution. I think a good way of dealing with this is for C# to have the 'friend' key word.

The .net framework uses 'internal' for similar situations but that means creating seperate assemblies which is overkill in this case I think.

I'd have to put the DataHolder and the DAL in one assembly and then use DataHolder in another.

I guess for now I'm just going to leave my classes with their insides exposed. (via properties of course)
Advertisement
we use this system,


class temp
{
long m_variable;
void Variable(long set){m_variable=set};
long Variable(void){return m_variable};

};

Raymond Jacobs, Owner - Ethereal Darkness Interactive
www.EDIGames.com - EDIGamesCompany - @EDIGames

I believe Petewood is right in one way but he is completely wrong in another. As he describes it, he starts off by making this little class for time and suddenly his time class becomes a Swizz Army knife with the Gregorian calender and everything. A small time class is a small time class or place it in a struct if that feels better. The time can be public or use getTime/setTime if that makes it better for the case. A small timer class is more of a wrapper than a true class.

The worst thing about OO is that you are always tempted to write classes that solves more than your problem. It only leads to wasted time and untested code.
get/sets are a waste of time and returning a reference to a private is an even bigger waste of time. If it's public, it's public and it's not a contract nor component oriented design for that class.
It's OK to make utility classes for convience sake that are not 100% to the letter modern OO designs; xsp when you waste time writing useless code to make the class less intuitive to use.

And I've always said lots of get/set tuples are a good indication of a poor design. 99% of the time they don't abstraction anything, so they do no useful work. The other 1% of the time they are poorly named mutators (methods). If it does something, give it a real function name that tells you what it does.

TCPSocket.ServerAddress = 192.168.0.1;
cout << TCPSocket.ServerAddress;
- or -
tcp_socket.connect(192.168.0.1);
cout << tcp_socket.server_address();

To me, the first way is retarded, but this is how several connection abstraction layers work (ADO comes to mind).

...
Quote:Original post by thedevdan
Wait... VC++2005 has properties?


MSVC5 did (MSVC2005 is MSVC7.2 I think) even 4 might have.
C++ Builder has had them since v1 or 2.
I think gcc even has an extention for them.
The only place they are typically used is in generated code, such as MS COM or from Borland's UI builder (and sometimes in Borland's VCL).
You can write them yourself, if you dig deep in Software Engineering I think I posted the syntax of them all about two years ago.


PS Friends are generally worse than accessors. Only useful/needed in a few case, e.g. operator overloading or extremely tightly coupled classes, such as DirectShow filters & pins.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
This is all kind of confusing. On one hand you have people who say always use properties and then on the other you have people who say don't bother.

Perhaps either way it doesn't really matter because both work and if you want what propeties give then you can always add them later and simple properties will probably be inlined anyway.

Quote:
PS Friends are generally worse than accessors. Only useful/needed in a few case, e.g. operator overloading or extremely tightly coupled classes, such as DirectShow filters & pins.


Why are they generally worse?

I think that a DAL that returns objects is an excellent place for friends. Because the DAL needs access to the object in order to set its state but you don't then want the comsumers of the object to be able to access its state.
Thanks for all replying.

I think friend classes are good when a class has members that are classes. And these classes are friends to the owner.

What do you think?
Often, an inner class is tightly coupled, and that means that often, inner classes are friends.

Or you can design your program (in C++) to not expose the implementation at all. Define abstract base classes as interfaces in the public headers, declare a factory for your abstract interface, and implement a concrete class in a .cpp file. That way, the concrete implementation is only in a single file, and can make everything public to the implementation in that file. This also loosens coupling; changing the implementation doesn't mean re-compiling all users.


Regarding delegates in C#, if you haven't learned delegates yet, run, don't walk, to the help file, and figure it out. A delegate is a way to bind a function prototype and an object instance into a single data item. It looks like a function pointer, but when someone invokes the delegate, the specific object it delegates to is invoked. (All the WinForms events are delegates)

This is IDEAL for a narrow interface such as a DAL.
enum Bool { True, False, FileNotFound };
I do know about delegates. I'm just not sure what is to be plugged into what.

Also, if this idea enables the DAL class to modify the object to be populated, what is there to stop any other object using this method too?

Granted it sounds like more work than a simple property access but if the access permissions can be circumnavigated then what has it bought you?

Another gripe, .net event handling is apparently relatively slow.

Anyhow, sounds like an interesting idea.
Quote:Original post by __fold
I believe Petewood is right in one way but he is completely wrong in another. As he describes it, he starts off by making this little class for time and suddenly his time class becomes a Swizz Army knife with the Gregorian calender and everything. A small time class is a small time class or place it in a struct if that feels better.

The worst thing about OO is that you are always tempted to write classes that solves more than your problem. It only leads to wasted time and untested code.


You could to do this:
struct Time {    int m_time;};


Then you will at least have type safety. People will have an indication of what a number means and what it is expected to be used for.

But any code to manipulate that integer will be outside of the class. If you don't put it in one place it will be repeated throughout your code.

You could write non member functions so that you can just call them to manipulate it for you:
void addWeek(Time& time) {    //calculate week in seconds    //...    time.m_time += week;}

Then in your program you can use that instead of having the code repeated everywhere.

I chose to make it a member function instead. It doesn't make the struct weigh any more; the data is still the size of an integer.

Remember: the functionality has to go somewhere.

Also remember: Exageration isn't useful in discussions. Words which describe extremes usually aren't helpful. "The worst thing", "you are always tempted", "It only leads to". You're likely to get into an arguement using language like that.

[Edited by - petewood on July 16, 2004 6:37:02 AM]
Quote:Original post by Magmai Kai Holmlor
You can write them yourself, if you dig deep in Software Engineering I think I posted the syntax of them all about two years ago.


Is this the link? Not sure.

This topic is closed to new replies.

Advertisement