4

A lot of the code I've been working on recently, both professionally (read: at work) and in other spheres (read: at home, for friends/family/etc, or NOT FOR WORK), has been worked on, redesigned and re-implemented several times - where possible/required. This has been in an effort to make things smaller, faster more efficient, better and closer to spec (when requirements have changed).

A down side to this is that I now have several code bases that have deprecated method blocks (and in some places small objects). I'm looking at making this code maintainable and easy to roll back on changes. I'm already using version control software in both instances, but I'm left wondering if there are any specific techniques that have been used by others for keeping the superseded methods without increasing the size of compiled outputs?

At the minute, I'm simply wrapping the old code in C style multi line comments.

Here's an example of what I mean (C style, psuedo-code):

void main () { //Do some work //Foo(); //Deprecated method call Bar(); //New method } /***** Deprecated code ***** /// Summary of Method void Foo() { //Do some work } ***** Deprecated Code *****/ /// Summary of method void Bar() { //Do some work } 

I've added a C style example, simply because I'm more confident with the C style languages. I'm trying to put this question across as language agnostic (hence the tag), and would prefer language agnostic answers, if possible - since I see this question as more of a techniques and design question.

I'd like to keep the old methods and blocks for a bunch of reasons, chief amongst them being the ability to quickly restore an older working method in the case of some tests failing, or some unforeseen circumstance.

Is there a better way to do this (that multi line comments)? Are there any tools that will allow me to store these old methods in separate files? Is that even a good idea?

2
  • .NET languages have the ObsoleteAttribute for this exact functionality - the compiler will output warnings for any code using an obsolete method.
    – Oded
    CommentedApr 3, 2012 at 15:18
  • I dislike what you did with your deprecated method foo. You let it live without doing anything. So code which still uses your method foo will now silently do nothing. Rename or delete it to be safe at the very least or raise in exception in that part of the code.
    – Pieter B
    CommentedMay 28, 2017 at 8:39

2 Answers 2

9

Is there a better way to do this (that multi line comments)? Are there any tools that will allow me to store these old methods in separate files? Is that even a good idea?

You should track and remove all commented code block. You are already using versioning, so the old code will be available whenever you feel nostalgic or if you have broken something while refactoring.

Commented code is just useless clutter.

4
  • The author stated I'm already using version control software in both instances, so setting up another won't help much. I agree though. Keep them in previous revisions and don't worry about the current one.CommentedApr 3, 2012 at 15:23
  • @DylanYaga yes, I have edited my answer. Good news is: he just has to start using it!CommentedApr 3, 2012 at 15:24
  • I thought that this was the way to go, but I wasn't sure if there where any (for want of a better phrase) other ways to do this.CommentedApr 3, 2012 at 15:25
  • There's no benefit to keeping it, and no cost to removing it (if it really is obsolete). So, why keep it? To paraphrase some artist dude whose name I can never remember: your program is done not when there is no more code to add, but when there is no more code to take away.
    – Tacroy
    CommentedApr 3, 2012 at 16:45
1

Source control is fine for this. You don't need to leave your code base cluttered up with old methods/classes that shouldn't be used.

Tag or branch at any milestone.

You can get back to any "last working copy" you like this way.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.