views:

139

answers:

5

This is (pehaps bad) habit I have formed and I am wondering if others have experienced the same thing and strategies for managing it.

A lot of times I will write a fair amount of experimental throw away code in the course of coming to the most effective solution. Sometimes complex loops and arrays and various functions that have a fairly involved internal logic. For me coding is an iterative process so I like to keep old partially working code around commented out so that I can refer to it as I am working on a slight variation of the function. Or in some cases quickly go back if a particular change doesn't work. But sometimes the commented code really gets in the way of being able to read through the overall flow of a particular code segment in a specific file. And too much of it becomes a real distraction. I realize I can do a lot of check in check outs to a repository (subversion) etc and delete the temp code knowing it is backed up. But I hate to check in broken or partially completed code.

What I really need is a highly functional "scratch pad" while working on code. Easy to hide expose. Maybe I haven't mastered the fine art of folding comments in my IDE. But I was wondering what people do to deal with this kind of problem?

Perhaps a very focused scratch pad app would be useful (preferably OS X). Something that integrates unit test production as well. Something where you could focus on a very narrow scope but still have some interpreter built in to actually "run" the function independent of a larger project. Add in a quick way to run or test the function, object or code segment in the scratch pad and something that would encourage the writing of units tests as you write the temporary code. And when you get the code segment working you can quickly merge back into your main project.

Does such an app exist? Does anyone else have this problem? Strategies for dealing with it?

I know that XCode has a nice "snapshot" feature that would help in this problem. I am doing a lot of work in Flex Builder these days. Wondering what the actionscript junkies out there do. Sometimes I go back and forth between flex builder and textmate. Any other strategies?

Update: To the people proposing git. What about browsing and keeping the code visually available? I am kind of partial to the Cornerstone app on OS X for subversion access. Are there any nice GUI frontends to git? More specifically, how can you keep the temp code segments a single command tab or icon click away while working within your IDE?

+2  A: 

Why not use source control? In fact some source control programs have a "shelve" feature that lets you store your current version of code on the server without actually checking it in, which sounds like exactly the feature you're looking for.

Scott Whitlock
Which ones ? I haven't seen that.
Thomas Geritzma
I'm thinking of Team Foundation Server.
Scott Whitlock
+3  A: 

I would suggest using git. You can commit broken code locally and only push good code to the "outside world".

Or you can use the stash which fits the scratch pad metaphor like a glove...

Or (local) branches.

Or all three options combined.

And if your code is already under subversion, you can interact with it and have all the benefits of git with git svn.

I don't use Mac OS X, so I can't recommend from experience, but try GitX and/or GitNub if you want a GUI.

Martinho Fernandes
+2  A: 

Try a distributed version control system such as git or mercurial. You can check in in your own repository as often and as broken as you like, and if you reach a somewhat stable version, transmit it to another (probably central) repository.

Malte Clasen
+1  A: 

There are two approaches I take to this problem.

The first, if I have totally working code that I want to keep around for reference, is to comment it all out within its routine, then add a comment line above it to visually separate things like so

/////////////////////////Old Code below/////////////////////////
/*
  ...
*/

The second thing I do is that I always edit with emacs set to keep my latest 9 versions of a file (and I generally refuse to let it "clean up" old versions when it asks). That allows me to go back and look at all the old approaches I took if I get in a bind.

I don't use revision control for this, except in the very unusual situation where I have a quick-and-dirty approach that I know works that I want to keep as a fall-back. Revision control is for keeping around old working versions of the system, not for keeping snapshots of any old crap that strikes my fancy.

T.E.D.
LOL! I have used pretty much verbatim the same comment structure before. Great minds think alike?Will have to consider the emacs last revisions feature. Thanks for the response.
Gordon Potter
+1  A: 

In VS you can use regions, I have a very strict structure that I maintain, which usually has these general regions:

Main Load Region

Main SQL Database Interactions

Main Class Code

Main Control Events

Main Clean Up

Old Depreciated Cold

I have just typed that off memory, because I've found almost all projects (web or desktop) almost always falls into those regions (plus or minus specialized classes that have a few different regions)

The Last region is always used to tuck away old code, or testing code, or code that I may reference in the future, but these days I find its not always used (until I get around to refactoring.)

I find once you take a very strict and structured approach, maintaining code, weeks, or months afterwards becomes very easy (in my experience anyway)

I don't know if that helps

Darknight
For throw away code, I'm creating "#region unused" at the bottom of file.
Arnis L.