3 ms·
Eh, using protected/private members is one of those things that makes sense only insofar as you are paranoid that you can't just access internals directly. Tha
by angersock 15y ago
Eh, using protected/private members is one of those things that makes sense only insofar as you are paranoid that you can't just access internals directly.
That said, consider the following. I've got a renderer that accepts a sprite to draw.
Version 1: I pass the sprite object to the renderer, and the renderer gets the texture contained in the sprite and draws it, scaling it according to the sprite's size and position public members.
I then decide that all of my sprites draw slightly differently (different scales/glow effects/color tint/whatever).
Version 2: I pass the renderer my sprite, it gets the size and render settings members from the sprite, and then draws it.
Oops! I now decide I want a hierarchy of sprites, so that I can add hats.
Version 3: Same as version 2, but in the draw routine now the renderer asks the sprite for its parent to calculate its position and size, and now I've got more renderer code and more sprite code. I can't just use the exposed position member of the sprite--it has context and logic behind it that must be done.
Oh wait, I have a problem in which the sprite glow might change depending on how many times its been drawn.
Version 4: The renderer now has to keep track of how many times it's drawn a sprite. My renderer is getting a bit chunky now.
Did I mention scale should change according to time but also the sprite's velocity?
Version 5: Renderer grabs these variables directly, and does all that other stuff too.
...and that now I want to let that scale be overridden by a isConstantSize flag on the sprite?
Version 6: aaaaaargh
...and that now the sprite can decide it wants to defer to a parent sprite's settings sometimes?
Version 7: what did i do to deserve this?
~
Proper encapsulation and use of accessors is clearly a sign of paranoia. That said, as your code evolves, you might find that this paranoia is completely justified. Hiding queries behind interfaces lets you future-proof them and trivially add more advanced behavior and hooks.
Even when you are working by yourself on an internal project, you are never working alone. You are also working with your future self, and that person is guaranteed to want something different from you and to make assumptions that you are not.
- angersock 15y agoOh, and for good measure--everytime you force yourself to use an accessor, you are providing the opportunity for somebody down the line to add debug and validation code. Or to add locking and critical sections. Or replace a dumb get and recalculation with caching. Or replace caching with a dumb get. Or a database access. This flexibility is not something you might think you need. You might also never need to debug your own code, or check your assumptions about variables, or track who is accessing what when. (for what it's worth, I don't use protection for data members of Plain-Old-Data types [say, messages or vectors] very often--they're too dumb to deserve this sort of extensibility or cost)
- Deestan 15y ago> Oh, and for good measure--everytime you force yourself to use an accessor, you are providing the opportunity for somebody down the line to add debug and validation code. So add it then. Don't pre-generalize everything just in case.
- angersock 15y agoWhat, is there some kind of run on parens in your neck of the woods? Running such a lean startup you can't afford a function invocation? This isn't premature abstraction or architecture astronautics--this is just good practice. If you are in a language like Java or C, your compiler should optimize away the call if it doesn't do something clever. If you are in Ruby, this is so easy to do that it doesn't even need mentioning--you just call the variable directly and behind the scenes you have replaced the variable name to be a function doing your magic instead. If your fingers protest at the additional "get" in your function names, go and buy yourself a real IDE. EDIT: Parent thread added clarification worth noting here. I'm not saying you should add accessors for every protected member--that's just as bad. I mean to say you should only expose members that absolutely require it (the fewer the better, generally), and only do so through function interfaces you can hook. :)
- Dove 15y agoRunning such a lean startup you can't afford a function invocation? This actually isn't as wildly hypothetical a situation as one might guess. At least in the Android world, "Avoid getters and setters" is on the short list of performance suggestions. http://developer.android.com/guide/practices/design/performance.html#internal_get_set http://developer.android.com/guide/practices/design/performa...
- campnic 15y agoAvoid internal getters and setters. This is a different issue vs. object to object getters and setters.
- 15y ago
- calebmpeterson 15y agoWait... why wasn't the sprite responsible for rendering itself from the beginning? sprite.render(context); or some such...
- angersock 15y agossssshhhhhhh (contrived example for making a point... visiting a draw() method with a solid renderer interface is the answer we usually use)
- groby_b 15y agoBecause that's roughly the worst way to render sprites, in terms of performance :) You want sprites to be an array, and render that in one go.
- teyc 15y agoEngineering has been always about trade offs. Every time I'm raced to use reflection to get to private members, I'm reminded that these abstractions are never free.