views:

227

answers:

10

What special techniques you use when modifying existing code?

Eg: Say you modify a business rule inside a method. Do you mark the modified section with special comments?

Any coding/commenting standards you use when modifying code?

+12  A: 

You mean something like:

foo();  // changed by SecretWiz, 20090131

I wouldn't recommend this. It clutters the code files, and the version control system should handle that for you. It keeps track of who changed what. Use

svn blame
+1 Beat me by a second...
Outlaw Programmer
Indeed, this drives me nuts too.
DavGarcia
Do you remove stuff like that when you are working on a file that others decorated that way?
innaM
or if you're in a good mood, use `svn praise` instead (does the same thing) :)
Sune Rievers
+2  A: 

No, that's a really bad idea. Your source control keeps a history of all edits. If you want something else, make an entry in your bug tracking tool. There's no need to comment out old sections of code or litter it with stuff like:

// modified by A.B. on 11/23/99 to fix issue #123456

I've seen comments like this in our codebase and they make no sense years down the line. Who the heck is A.B., and what was issue #123456? If the code is still here, does this mean that someone plans on rolling these changes back in the future?

These comments have no value and only serve to clutter your code.

Outlaw Programmer
I think referring to the issue# is not so bad. We often have a situation where the rationale for some one-line change can be complex, and the discussion in the issue tracker is too long to copy-paste to code (now *that* would clutter it). Because the issue tracker - like version control - is not going anywhere, I find it ok to add the reference. Even after years, you see the full discussion which resulted in that specific line of code.
Pukku
+1  A: 

I would suggest creating a method & call that from the code that is being modified.
Also, name the method to suggest the purpose/intent of the method.

e.g. GiveRebateIfValidCoupon();

shahkalpesh
A: 

"Any coding/commenting standards you use when modifying code?"

Yes. Make new subclasses. Leave old code alone except in the rare case that you didn't test it properly and was actually wrong.

Changes to requirements mean adding subclasses and new tests to handle the new business rules.

S.Lott
Interesting idea but I don't know if it will work in all cases. Sometimes business rules change FOREVER and there's no need to keep this old code around.
Outlaw Programmer
This sounds like a recipe for rigidity
Joe Soul-bringer
Do I smell some C++ like technology in the background here ? If you insisted on working like that on my C#/java project I'd have you kicked out of the project. -1 from me.
krosenvold
A: 

The only time I add special comments is if the modification is intended to be temporary. In that situation I flag it with a standard keyword (eg, TEMPFIX) so that I can find it later. Of course, you then have to remember to come back and remove the code or make a permanent change but on some projects we've enforced that using a macro that allowed us to specify an expiry date after which the code stops compiling.

Other than that, we rely on source control.

Mendokusai
A: 

The code should conform whatever coding standards you have or your organization has.

So, No, there should not be any special comments that the code has been modified - all or at least most, code will modified sooner or later.

If there is code that you inherited that does not correspond to commenting standards, then by all means add comments as you refactor the code. If the code is really old and undocumented, naturally that means adding documentation.

It is good to understand code before you modify it (by-the-way).

Joe Soul-bringer
A: 

Normally I will just change the code and make my comments in my source control check in. In the task tracking tool of choice, you can reference the revision where the task was implemented.

Sometimes I know that certain functionality is going to change back and forth, move around, change names, etc. depending on how the user requirements are debated. In this special case I will keep the old version in there and just comment it out. Then it becomes trivial to just uncomment it later, rather than slogging through source control looking for the old version. This can also save someone's ass if they have to maintain your code later, because when the users change their mind AGAIN the requirement will already be in the code, waiting to be uncommented.

davogones
A: 

I have to agree with a lot of other people here on SO. "If you don't need something in your code, remove it". Especially in production code, the last thing you want is a lot of clutter. It could be far easier for someone to figure out how your change works than to read your maintenance comment and possibly get confused.

I used to keep old deprecated code in my projects, but over time a project that should have only been a few thousand lines ended up being over 10,000 and hard to manage.

David Anderson
+3  A: 

If I do something like fixing a relatively obscure bug, basically anything where it isn't fairly obvious why I wrote the code the way I did, I'll typically add a comment to explain it, so that I (or someone else, if anyone else ever modified my code ;-) won't accidentally remove it later.

David Zaslavsky
+3  A: 

One thing I always try to do is to put in the bug ID (or feature request ID) in bug tracking system in the code checkin comments I do for that change. I add something like "see the comments for this bug/feature in bugzilla for more details". There I can and usually explain the rationale for that code change. This means that all changes or atleast all important changes need to be tracked through a feature request/bug ID. I have many times created a bug just to explain in details the business reasons involved.

omermuhammed