![]() |
Don't Repeat Yourself principle for Initialization lists.
Hi, I've a question for the gurus here, I'm hoping there is a simple answer which I just keep missing. The "Don't Repeat Yourself" principle, or DRY, is a fairly standard rule you should usually follow. But I'm really not sure what the best way is to apply it to constructor initialization lists short of using a common base member.
Here is what I'm currently using: :
Would anyone know of a better way to do this? |
I'm not sure what you're getting at. Suppose you need to use an algorithm lebenty-four times. You don't copy and paste the algorithm, but write a function. You still need to repeat the call.
Is your concern with footprint, execution speed, or satisfying some dictum proposed by some phoney high-priest looking for acolytes? Incidentally, a #define provides little beyond readability and a single site of modification. The definition is substituted everywhere it's encountered, before the compiler sees the file. |
Of your three options I think satisfying a phoney high-priest is the best fit.
In some cases, say if you have a member which is a reference, then it needs to be set in an initialization list. But even then it is hard to come up with an example (even a very contrived one) where what I was thinking about would really be beneficial. Perhaps this thread is best written off as a misguided academic exercise. :) |
Proposal for C++0x Delegating Constructors:
http://www.open-std.org/JTC1/SC22/WG...2006/n1986.pdf (pdf) You might find that interesting. I wouldn't take it as a justification for your macro hack job, though. |
I too am trying to figure out weather its better to use the Macro here or not. When a class gathers members (up to around 10) the list can get hard to maintain and using something like the macro can help. Of course there is an argument to be made the class is getting large enough that this becomes and issue you to take another look at the class.
|
Well, I don't know if this really applies (or even if it was a good idea), but in my tic-tac-toe game (with some code inspired by DaWei's contributions) there was some really repetitive stuff. Mostly it was "move x to y" for 8-9 unrelated values. Well, actually, there was a pattern, but the work in abstracting it would have been more than just doing it the easy way and with very little return.
Instead of :
fill( Spot( 0, 1, src.spotAt( 2, 1 ).icon() ) )9 times, I wrote a macro: :
#define FILL_AT_FROM( src, x1, y1, x2, y2 ) \And now it lookes like this: :
void Board::rotateLeft() {I think it's better. :) |
On the other hand, my rotate left looked like this, not a macro in sight:
:
def rotateL (p): |
| All times are GMT -5. The time now is 2:51 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC