![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Jun 2006
Location: Ireland
Posts: 152
Rep Power: 3
![]() |
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: cpp Syntax (Toggle Plain Text)
Would anyone know of a better way to do this?
__________________
Visit my website BinaryNotions. |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Jun 2006
Location: Ireland
Posts: 152
Rep Power: 3
![]() |
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. ![]()
__________________
Visit my website BinaryNotions. |
|
|
|
|
|
#4 |
|
Hobbyist
Join Date: Sep 2005
Posts: 261
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3
![]() |
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.
__________________
Robotics @ Maryland AUV Team - Software Lead |
|
|
|
|
|
#6 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 641
Rep Power: 4
![]() |
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 ) \
fill( Spot( (x1), (y1), src.spotAt( (x2), (y2) ).icon() ) )And now it lookes like this: void Board::rotateLeft() {
Board b( *this );
clear();
FILL_AT_FROM( b, 0, 0, 2, 0 );
FILL_AT_FROM( b, 1, 0, 2, 1 );
FILL_AT_FROM( b, 2, 0, 2, 2 );
FILL_AT_FROM( b, 0, 1, 1, 0 );
FILL_AT_FROM( b, 2, 1, 1, 2 );
FILL_AT_FROM( b, 0, 2, 0, 0 );
FILL_AT_FROM( b, 1, 2, 0, 1 );
FILL_AT_FROM( b, 2, 2, 0, 2 );
}I think it's better. ![]()
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
On the other hand, my rotate left looked like this, not a macro in sight:
def rotateL (p):
'''
Rotate a pattern counterclockwise by 90 degrees
'''
a = p [:]
a [6] = p [0]
a [0] = p [2]
a [2] = p [8]
a [8] = p [6]
a [3] = p [1]
a [1] = p [5]
a [5] = p [7]
a [7] = p [3]
return a
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Assigning an array of lists | deanosrs | C | 42 | Apr 13th, 2006 1:35 PM |
| Linked Lists | Eric the Red | C++ | 6 | Mar 19th, 2006 12:47 AM |
| Nested Lists | Mjordan2nd | Python | 2 | Oct 22nd, 2005 3:41 PM |