Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A good programmer never copies and pastes (?)
Najdorf
Unregistered

 
Post: #1
A good programmer never copies and pastes (?)
I dunno... Sometimes (not too often actually), I have to do a procedure (function, you call them in C?) that is very very similar to another one I have already in the code... Now, if I have to do 3 or more very similar procedures I try to disassemble them into subprocedures like a good chap, but if I only have 2 I copy and paste the second one chenging the few things I need to change. Is it a good idea? Do you do the same? Or a good coder NEVER copies and pastes?
2003.06.16 04:48 PM
Quote this message in a reply
Zoldar256
Unregistered

 
Post: #2
A good programmer never copies and pastes (?)
I'd be lying if I said I *never* copy and paste. But I really believe it is a very bad idea to copy and paste. In general, if you want 2 functions to be very similiar, then you should break these functions up into smaller functions. So the only difference between the two functions is how they call the smaller functions.

So I generally code by the mantra that anything coded twice should be a function, or at least a macro.

While this seems like a large suck of time, I've made so many mistakes through copy and paste that I think it's a good idea.
2003.06.16 05:05 PM
Quote this message in a reply
OneSadCookie Offline
Genius Bar
*****

Posts: 2,154
Joined: Feb 2005
Post: #3
A good programmer never copies and pastes (?)
There are two views on the subject, both of which I think are equally valid, and which you subscribe to depends on your workflow, laziness and anality Wink

One view is, no duplicate code, period. If you write something similar in two places, you then figure out how to extract the similar bits.

Unfortunately, that's quite hard to stick to, particularly as the size of the project grows and you don't know any more if there's something like that in the code...

The other view is, Three strikes and you refactor. That is, if you do something similar twice, you shudder, copy, and paste. If you do it a third time, you extract the common bits like a good programmer.
2003.06.16 05:08 PM
Visit this user's website Find all posts by this user Quote this message in a reply
skyhawk Offline
Member
***

Posts: 213
Joined: May 2005
Post: #4
A good programmer never copies and pastes (?)
my argument:
switch statement for different weapons
each case makes calls to a function (function call was about 2 pages wide)
differences in the function calls varied.

I had no choice! [Image: sad-smiley-047.gif]

anyways, needless to say I got a couple of segfaults because of that copying and pasting.

Lesson learned, if you MUST copy and paste, at least read the line out loud or something and double check it.

on a side note about extracting, #define macros are your friends. I wrote an extremely complicated find function that took 7 parameters and it could return back an array of everything satisfying just about every condition. But sometimes I would call the same type of find (find enemies within this range) and I created a macro findenemies that only had 3 parameters instead of the 7, and that macro called the main find function with all the proper information.
2003.06.16 05:22 PM
Find all posts by this user Quote this message in a reply
OneSadCookie Offline
Genius Bar
*****

Posts: 2,154
Joined: Feb 2005
Post: #5
A good programmer never copies and pastes (?)
Inline functions are your friend, you mean...

Both GCC and CW will let you inline from C, so there's no excuse. Virtually everything that can be done from a macro can be done from an inline function, and they're typesafe, behave how you expect them to, and are just as fast.
2003.06.16 05:58 PM
Visit this user's website Find all posts by this user Quote this message in a reply
DoG Offline
Moderator
*****

Posts: 400
Joined: Mar 2005
Post: #6
A good programmer never copies and pastes (?)
Quote:Originally posted by OneSadCookie
Inline functions are your friend, you mean...

Both GCC and CW will let you inline from C, so there's no excuse. Virtually everything that can be done from a macro can be done from an inline function, and they're typesafe, behave how you expect them to, and are just as fast.


... unless you are riding one of the gcc altivec stack bugs Wink

Macro's have their places, though, like writing unit testing functions without passing a page worth of parameters 3 times during testing a single thing. And some things, like the ##stringifications, are just plain useful.

DON'T PANIC
2003.06.16 06:17 PM
Visit this user's website Find all posts by this user Quote this message in a reply
henryj
Unregistered

 
Post: #7
A good programmer never copies and pastes (?)
I subscribe to the 'Three strikes and you refactor' line usually. Or if I'm having a bad day (you know those, when no matter what you do you just can't get anything to work) I'll go over my code and refactor any messy stuff like duplicate code.

I know it is easier to leave the crap and some times it seems like a waste of time to clean it up but it WILL save you time later.

Unit tests are good for this too. If you have similar code the unit tests tend to be the same and that's a good indicator that the code should be the same too i.e. a function. Plus this eliminates the sort of problems that skyhawk mentioned.
2003.06.16 07:13 PM
Quote this message in a reply
henryj
Unregistered

 
Post: #8
A good programmer never copies and pastes (?)
Quote:each case makes calls to a function (function call was about 2 pages wide)


This is a bad sign. Any function that is more than half a screen in length should be refactored into smaller functions.
2003.06.16 07:29 PM
Quote this message in a reply
Mark Levin
Unregistered

 
Post: #9
A good programmer never copies and pastes (?)
I don't think there's anything that should never, ever be done (or anything that should always be done, for that matter). The secret is finding the best thing to do each time.

For example, I did a tremendous amount of copying and pasting to build S2's scripting API; there's no good way to iterate over an arbitrary subset of the methods of a class (or of many classes).
2003.06.16 08:06 PM
Quote this message in a reply
OneSadCookie Offline
Genius Bar
*****

Posts: 2,154
Joined: Feb 2005
Post: #10
A good programmer never copies and pastes (?)
Quote:Originally posted by Mark Levin
For example, I did a tremendous amount of copying and pasting to build S2's scripting API; there's no good way to iterate over an arbitrary subset of the methods of a class (or of many classes).

No good way in C++.

You're just using the wrong language for the job. Java, Ruby, ObjC, &c all let you find out what methods an object has, retrieve them and call them.

Choosing a Programming Language
2003.06.16 08:11 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)