views:

496

answers:

18

You when you spend all day getting a function to work just right, but then an architecture change causes the function to no longer be used? But you know that the code is useful enough that if the architecture ever sways back, that function will be again useful?

It seems wrong to delete the function before you do the check in but then again it takes up space as a unused function if you check it in.

+9  A: 

I keep a record of all useful code in an offline code snippet database.

Do not check it in - the centralised repository is a place only for working code that is used in the application.

Galwegian
+2  A: 

Check it in and comment it out. IMHO, The cost in space is well worth the time you spent on getting it right (and may need to spend again should the wind change). Maybe you can check it in as part of a "bits and pieces" file so that it does not clutter your active code.

Yuval F
+4  A: 

I would definitely delete it. You can put it in a special place that members of the project can see but get it out of the project. It makes maintenance hard when developers can't figure out why a particular function is there when its not being called.

Phil Bennett
+6  A: 
MadKeithV
+10  A: 

As an answer, please consider this short story:

The dead code Collector: Bring out yer dead code.

Man with dead code : Here's one.

The dead code Collector: That'll be ninepence.

The dead code: I'm not dead code.

The dead code Collector: What?

Man with dead code : Nothing. There's your ninepence.

The dead code: I'm not dead code.

The dead code Collector: 'Ere, he says he's not dead code.

Man with dead code : Yes he is.

The dead code: I'm not.

The dead code Collector: He isn't.

Man with dead code : Well, he will be soon, he's very ill.

The dead code: I'm getting better.

Man with dead code : No you're not, you'll be stone dead code in a moment.

The dead code Collector: Well, I can't take him like that. It's against regulations.

The dead code: I don't want to go on the cart.

Man with dead code : Oh, don't be such a baby.

The dead code Collector: I can't take him.

The dead code: I feel fine.

Man with dead code : Oh, do me a favor.

The dead code Collector: I can't.

Man with dead code : Well, can you hang around for a couple of minutes? He won't be long.

The dead code Collector: I promised I'd be at the Robinsons'. They've lost nine today.

Man with dead code : Well, when's your next round?

The dead code Collector: Thursday.

The dead code: I think I'll go for a walk.

Man with dead code : You're not fooling anyone, you know. Isn't there anything you could do?

The dead code: I feel happy. I feel happy.

[the dead code Collector glances up and down the street furtively, then silences the dead code with his a whack of his Ctrl-X]

Man with dead code : Ah, thank you very much.

BraveSirFoobar
Nice :-) This is from a Monty Python episode, right?
Daan
Indeed! I found it very appropriate.
BraveSirFoobar
+1 for use of Monty Python and code at the same
Nathan W
+3  A: 

Delete the code. Write a good check-in comment, which explains what you deleted and why. This should be clear enough so that someone who laters runs a history command on that file will understand it, and can retrieve your function from the versioning repository.

Nicolai
A: 

Since I started working at my current employer some two years ago I have created quite a large repository of utility code. It’s divided up in several categories and used in several projects. Code that I create that I don’t need immediately goes in there and hibernates until it is used (or forever, whichever is earlier).

Bombe
+3  A: 

In that case I always Use a WIKI where the piece of software is described. A well labeled subitem and a good description to that function does it then.

In my opinion it is not always useful to reuse this whole function, but to look at nice, functionable code that once did make sense.

joki
+1  A: 

Post your code to: http://snipplr.com/ http://refactormycode.com/

let other people rate and maybe use it too :)

melaos
A: 

Open source it! If it's not useful to you, maybe it'll be useful for someone else.

The usual argument against this is that the company isn't paying you to write free code, but then, they have you working against an unstable architecture, so they're not getting the benefit of your code anyway.

Ryan Fox
A: 

Dead code is no longer being maintained. When you need it again you will need to spend time to get it working again. If it is truly useful and you can still remember about it you can always get via your version control.

At work some people have code that has been commented out 4 years ago comprising 70% of the source file. Just let it Go

Gerhard
+2  A: 

Delete it with joy.

Code is a static promise made against changing requirements in a dynamic world.

My point today is that, if we wish to count lines of code, we should not regard them as “lines produced” but as “lines spent”: the current conventional wisdom is so foolish as to book that count on the wrong side of the ledger.

Dijkstra 1036-11

Jason Watkins
A: 

I have a class file in a copy of my project in which i keep storing useful unused functions.

Another way : since we keep our project in SVN repository so when i replace my useful function i always have dat function in my log files so i can access it any time in future.

Samiksha
+1  A: 

Check it in, with a comment (in code and check in) saying you're about to delete it.

Then delete it and check-in the change immediately, with a check-in comment explaining why.

That way you don't have cruft in your codebase, but you've got a permanent record of the code.

Jon Skeet
+1  A: 

Just keep it in whatever snippet collector you have devised for yourself or is available in your system. The problem isnt' so much where to put it (mostly we're talking about just text, aren't we?), but how to find it again. The issue is how to find it again, so some sort of tagging/searching method needs to be used.

I keep my code snippets in Code Collector Pro (for the Mac).

This way it isn't in any project, but I can reuse it whenever I need to.

disclaimer: I'm just a satisfied customer.

Abizern
A: 

system with less code is better one your commented code will be of no use anyway when (if) you uncomment it later. all the environment the code used to be ok in can be changed or more dangerously - some small invisible part that came with new business logic might be changed.

old code is a perfect opportunity to introduce new bugs. uncommenting old code is the same as programming by guessing (look ma, it works!) which is evil by definition.

miceuz
A: 

If its a substantial amount (and is generally useful anyway), why not consider open sourcing it? I like the idea of recycling.

Chii
A: 

Many suggestions in How to Delete Code but I keep a plain text file which I dump occassional tested but unused functions which might come in handy some day. I can't bear to throw them away completely, but then again, I have never needed to go to back to any of them either.

One more rule about such files/collections of code snippets: only have one, in one place.

Martin