| View previous topic :: View next topic |
| Author |
Message |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Thu Jan 06, 2011 4:34 am Post subject: [C++]get/set versus public fields |
|
|
I don't really have much need to check the input at all, just passing a class instance by reference, that's the extent of the work being done... at the moment, at least. Is it generally preferable to wrap things like this in getter / setters?
Also on the subject of style:
createBuffer(BUFFERTYPE type, Buffer& b);
vs
createVertexBuffer(...)
createIndexBuffer(...)
They're both just simple chunks of memory at their core, whatever information needed about them, like number of verticies, can be figured out easily anyway. It would be condensing two almost identical implementations down to one.
|
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Thu Jan 06, 2011 6:26 am Post subject: |
|
|
If this is for yourself, I would say go with solution 1 and document it well in code. But as far as standards are concerned, solution 2 is "best". I've talked with many people in industry who say that they have these ginourmous handbooks about readability of code, and that the code should usually speak for itself if written correctly. I think it's obvious that solution 2 is much clearer than 1.
_________________
|
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Thu Jan 06, 2011 9:02 am Post subject: |
|
|
| If you're not wanting to do anything else in the getter/setters then use public. You should use get/set instead of public when changing some property implicitly changes another and requires its update. At least, this is the rule I follow. However at the end, it is down to preference and no one is likely to berate you for using one over the other.
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Thu Jan 06, 2011 8:39 pm Post subject: |
|
|
#2 is my personal liking. Will look better as #2 vs. having #1 which will turn into some sort of if/else or switch case.
_________________
- Retired. |
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Fri Jan 07, 2011 7:24 am Post subject: |
|
|
| Wiccaan wrote: | | #2 is my personal liking. Will look better as #2 vs. having #1 which will turn into some sort of if/else or switch case. |
That's what it would boil down to internally, but I see why it's silly.
I guess it would have worked something like this, going the second route
(typed here so formating is hosed)
| Code: | struct BUFFERDESC
{
BUFFERTYPE type;
u32 length;
};
createBuffer(BUFFERDESC& bd, Buffer& b); |
In trying to make something less verbose I made it more so. Having to fill out a struct (simple, but nevertheless) for something like that doesn't seem to make much sense, maybe if there was more to track about what makes up the buffer, but there isn't.
It seemed more sensible last night before I went to sleep... :\
adding to the pile of sleep deprived brilliance.
|
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Fri Jan 07, 2011 10:56 pm Post subject: |
|
|
Never make variables in a class public. Ever. This is exposing the implementation of a class, when all you want to expose is the public interface that describes the behavior of the class.
It breaks:
- Class invariants, if they exist.
- Polymorphism. You don't have the ability to extend the class later.
Furthermore, if you find you're making a lot of getters/setters you're probably doing something wrong. Classes represent objects and methods are a way of interacting with an object of that type (think 'actions') - getting and setting private variables breaks this line of thinking (it also destroys any notion of abstraction).
With regards to the second question, present #2 to the users and use #1 internally - you get the best of both worlds.
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Sat Jan 08, 2011 4:56 am Post subject: |
|
|
I don't really have a lot of getters / setters, but hopefully I have somewhat of a grip design wise.
Right now I have a structure like:
The engine holds and manages the various interfaces. Being primarily a graphics engine though, the rendering device is the beefiest thing by far.
The renderer manages and creates just about everything drawing wise... like surfaces to draw on, which in turn, wrap basic pixel drawing stuff and various types of blits so you don't have to drop down to the actual block of memory yourself if you don't really have to. So to do 2d drawing, there's an aptly named getBackbuffer() function that returns a pointer to the primary surface. Not entirely sure if I like having a surface do its own blits (to another surface even), but surfaces can also load bitmaps, so drawing a picture is simply having it load a file then copy it to the backbuffer.
The renderer doesn't really implement much itself, just wraps most stuff up. Drawing 3d it just pulls data from the buffers and matricies it holds and other objects get to nom on that and do their own lifting, like the vector type implements the transformation to screen space, etc.
|
|
| Back to top |
|
 |
|