Is there a method for differentiating informative comments from commented out code?Is commented out code really always bad?Where should I include comments in my “self-documenting code”?Code maintenance: To add comments in code or to just leave it to the version control?Can commented-out code be valuable documentation?Why are we supposed to use short functions to sectionalize our code?What's wrong with comments that explain complex code?Clean Code comments vs class documentationIs a JS Boolean having custom properties a bad practice?Is the following comment clear: “Until contested”Alternative for commented out code that will be used later
How do ballistic trajectories work in a ring world?
What was the profession 芸者 (female entertainer) called in Russia?
Draw a diagram with rectangles
Is it okay to use open source code to do an interview task?
Sense of humor in your sci-fi stories
When do flights get cancelled due to fog?
My previous employer committed a severe violation of the law and is also being sued by me. How do I explain the situation to future employers?
Strong Password Detection in Python
Is this really the Saturn V computer only, or are there other systems here as well?
Was it ever illegal to name a pig "Napoleon" in France?
Is it possible to complete a PhD in CS in 3 years?
How to understand flavors and when to use combination of them?
How to slice a string input at a certain unknown index
Can Jimmy hang on his rope?
Need a non-volatile memory IC with near unlimited read/write operations capability
Is homosexuality or bisexuality allowed for women?
What does the multimeter dial do internally?
What factors could lead to bishops establishing monastic armies?
Who goes first? Person disembarking bus or the bicycle?
Is it possible for a character at any level to cast all 44 Cantrips in one week without Magic Items?
How do I separate enchants from items?
A ring of generalized power series
Why is a semiprime used as the modulus in RSA?
Moving millions of files to a different directory with specfic name patterns
Is there a method for differentiating informative comments from commented out code?
Is commented out code really always bad?Where should I include comments in my “self-documenting code”?Code maintenance: To add comments in code or to just leave it to the version control?Can commented-out code be valuable documentation?Why are we supposed to use short functions to sectionalize our code?What's wrong with comments that explain complex code?Clean Code comments vs class documentationIs a JS Boolean having custom properties a bad practice?Is the following comment clear: “Until contested”Alternative for commented out code that will be used later
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Throughout the course of programming, you will end up with some comments that explain code and some comments that are removing code:
// A concise description
const a = Boolean(obj);
//b = false;
Is there a good method to quickly parse which is which?
I've played around with using 3 /
's and /** */
for descriptive comments.
javascript comments
New contributor
add a comment |
Throughout the course of programming, you will end up with some comments that explain code and some comments that are removing code:
// A concise description
const a = Boolean(obj);
//b = false;
Is there a good method to quickly parse which is which?
I've played around with using 3 /
's and /** */
for descriptive comments.
javascript comments
New contributor
add a comment |
Throughout the course of programming, you will end up with some comments that explain code and some comments that are removing code:
// A concise description
const a = Boolean(obj);
//b = false;
Is there a good method to quickly parse which is which?
I've played around with using 3 /
's and /** */
for descriptive comments.
javascript comments
New contributor
Throughout the course of programming, you will end up with some comments that explain code and some comments that are removing code:
// A concise description
const a = Boolean(obj);
//b = false;
Is there a good method to quickly parse which is which?
I've played around with using 3 /
's and /** */
for descriptive comments.
javascript comments
javascript comments
New contributor
New contributor
edited 1 hour ago
Peter Mortensen
1,1152 gold badges11 silver badges14 bronze badges
1,1152 gold badges11 silver badges14 bronze badges
New contributor
asked yesterday
AceAce
1595 bronze badges
1595 bronze badges
New contributor
New contributor
add a comment |
add a comment |
6 Answers
6
active
oldest
votes
There is a very simple solution to this: remove the commented-out code.
Really, there are only two good reasons to comment out code: to test something/make a fix, or to save code you might use later. If you're testing or fixing something, remove the commented out code as soon as you're done with the test or fix. If you're saving code you might use later, make it first-class code and put it somewhere such as a library where it can be put to good use.
50
Also, if the code has been checked in: just remove it. If you ever need it back, source control has got you covered
– marstato
yesterday
9
When code is removed nobody notices it ever existed. That makes it harder to recover. There is value in leaving commented code, especially if it seems likely that it will be used in the future.
– usr
16 hours ago
21
@usr: When code is removed nobody notices it ever existed - and to my experience, in 99% of all real-world cases that is the right thing. In 1%, there may be some value in the outcommented lines, however, if commented-out code stays in for several weeks or months (or longer), chances are high it won't even compile any more due to refactorings of the active code lines. The "pontential value for future uses" argument is often used as a wrong excuse by people who have an emotional problem of stripping things out of the codebase for which they had invested some hours of brainwork.
– Doc Brown
15 hours ago
5
I never commit commented code without additional explanatory comments. There are rare situations where you might want the code back in the near term, but every single one of those is exceptional and requires an explanation for future developers (or future you). 90% of my comments are "Removed because it appears to be unused. Delete after Nov 2021 if there are no problems"
– James Beninger
13 hours ago
3
My colleague once put "There was code here that did X but I removed it" when we removed some feature for the time being. That worked really well; you knew it was in source history for that file, but it wasn't bothering you.
– Erik
4 hours ago
|
show 5 more comments
Hmm, I read this question slightly differently from Robert who correctly asserts that commented out code should be removed.
If, however, you are looking for a convention to mark code for later removal, an old favorite of mine is //b = false; //TODO: remove
Some IDE's flag //TODO:
comments or can be taught to. If not, it's usually a searchable string. It's best to follow whatever convention your shop has established because this can be done several ways. Every code base should do this one way. Keeps it searchable.
quickly parse which is which?
Without that mark the automated way to do this is with the compiler. If stripping the comment off produces code that compiles, it must have been commented code. Writing an IDE plugin that checks that wouldn't be hard. But it will leave buggy commented code behind.
This is why it's better to simply mark commented out code as code the moment you comment it out. This lets you work non-destructively while you decide if you really want it gone. Since we all get interrupted, and are somewhat forgetful, don't be surprised if some lines get checked in while in that state. If they do it's nice that they're at least clearly marked and searchable. Keyboard macros have helped me with this in the past. It's hard to get interrupted in the middle of this if you can do it with a single keystroke.
You can take this as far as enshrining the mark in your continuous integration tests. Oops, I'm trying to check in with outstanding TODO's again.
Instead of seeing if comments compile to label them as code, you could run comments through a natural language processor and label the ones that parse as a sentence or noun phrase in the language your team speaks.
– TheHansinator
4 hours ago
@TheHansinator that sounds good but my experience with my cellphones auto correct relationship with my coder jargon makes me cautious.
– candied_orange
1 hour ago
add a comment |
Adding to @RobertHarvey's excellent answer I believe there is only one legitimate reason I've ever encountered for saving commented code to source control, even temporarily: in case of a non-obvious improvement which depends on some third party change. This could be a bug or a feature of the language which is not yet considered stable. It might look something like this:
# TODO: Replace with `foo = frobnicate(bar)` once <link.to/bug> is fixed
foo = [some complex workaround]
Basically, the work has already been done, but you can't yet take advantage of it, so deleting it means someone would have to rediscover it later.
add a comment |
I agree with the answer stating that old code should be removed rather than commented out where possible, however I have observed a convention for those few occasions when commented-out code is needed.
(My basis is C# but this can be applied to any C-syntax language eg java)
// An explanatory comment has a space between the comment marker and the content.
// The following lines are commented out code so do not have the space (except where indented).
//var a = something();
//if(a==2)
// doSomethingElse();
//
New contributor
1
This depends totally on style: When I comment out code, I generally add the//
to the first column, and since virtually all code is indented, the result is virtually always that the comment starts with some tabs. Normal comments don't get a leading space from me, unless there are already other comments with a leading space in the vicinity. So, your method would fail abysmally on comments that I produced, and any method designed to recognize my comment patterns would fail abysmally on yours.
– cmaster
8 hours ago
@cmaster Ah I see, I think I misunderstood the question. I provided a simple way of formatting the comments in such a way that they can be easily parsed for type, which isn't what was asked for.
– IanF1
6 hours ago
add a comment |
If you use an editor with a compiler running in the background (like Xcode and Clang), you can just try to compile the text of the comment. For example”a concise description” gives errors, “b = false;” doesn’t. You could then use different syntax highlighting.
A simpler method would be an IDE plugin that uses some heuristics, like multiple words in a row other than keywords points to comments, matched curly braced point to code etc.
add a comment |
I am interpreting the question different still, thinking you want to find commented out code.
C-style code is bound to have semi-colons in it while comment is unlikely to have semi-colons in it. So for single line commented-out code you could use this regular expression:
s*//[sS]*;
For multi-line commented-out code it could be
/*[^;]*;[^;]**/
Note Visual Studio is a bit peculiar about line breaks in regular expressions, they do not count as whitespace, you need to specify an explicit n.
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "131"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: false,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Ace is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsoftwareengineering.stackexchange.com%2fquestions%2f394287%2fis-there-a-method-for-differentiating-informative-comments-from-commented-out-co%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
StackExchange.ready(function ()
$("#show-editor-button input, #show-editor-button button").click(function ()
var showEditor = function()
$("#show-editor-button").hide();
$("#post-form").removeClass("dno");
StackExchange.editor.finallyInit();
;
var useFancy = $(this).data('confirm-use-fancy');
if(useFancy == 'True')
var popupTitle = $(this).data('confirm-fancy-title');
var popupBody = $(this).data('confirm-fancy-body');
var popupAccept = $(this).data('confirm-fancy-accept-button');
$(this).loadPopup(
url: '/post/self-answer-popup',
loaded: function(popup)
var pTitle = $(popup).find('h2');
var pBody = $(popup).find('.popup-body');
var pSubmit = $(popup).find('.popup-submit');
pTitle.text(popupTitle);
pBody.html(popupBody);
pSubmit.val(popupAccept).click(showEditor);
)
else
var confirmText = $(this).data('confirm-text');
if (confirmText ? confirm(confirmText) : true)
showEditor();
);
);
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
There is a very simple solution to this: remove the commented-out code.
Really, there are only two good reasons to comment out code: to test something/make a fix, or to save code you might use later. If you're testing or fixing something, remove the commented out code as soon as you're done with the test or fix. If you're saving code you might use later, make it first-class code and put it somewhere such as a library where it can be put to good use.
50
Also, if the code has been checked in: just remove it. If you ever need it back, source control has got you covered
– marstato
yesterday
9
When code is removed nobody notices it ever existed. That makes it harder to recover. There is value in leaving commented code, especially if it seems likely that it will be used in the future.
– usr
16 hours ago
21
@usr: When code is removed nobody notices it ever existed - and to my experience, in 99% of all real-world cases that is the right thing. In 1%, there may be some value in the outcommented lines, however, if commented-out code stays in for several weeks or months (or longer), chances are high it won't even compile any more due to refactorings of the active code lines. The "pontential value for future uses" argument is often used as a wrong excuse by people who have an emotional problem of stripping things out of the codebase for which they had invested some hours of brainwork.
– Doc Brown
15 hours ago
5
I never commit commented code without additional explanatory comments. There are rare situations where you might want the code back in the near term, but every single one of those is exceptional and requires an explanation for future developers (or future you). 90% of my comments are "Removed because it appears to be unused. Delete after Nov 2021 if there are no problems"
– James Beninger
13 hours ago
3
My colleague once put "There was code here that did X but I removed it" when we removed some feature for the time being. That worked really well; you knew it was in source history for that file, but it wasn't bothering you.
– Erik
4 hours ago
|
show 5 more comments
There is a very simple solution to this: remove the commented-out code.
Really, there are only two good reasons to comment out code: to test something/make a fix, or to save code you might use later. If you're testing or fixing something, remove the commented out code as soon as you're done with the test or fix. If you're saving code you might use later, make it first-class code and put it somewhere such as a library where it can be put to good use.
50
Also, if the code has been checked in: just remove it. If you ever need it back, source control has got you covered
– marstato
yesterday
9
When code is removed nobody notices it ever existed. That makes it harder to recover. There is value in leaving commented code, especially if it seems likely that it will be used in the future.
– usr
16 hours ago
21
@usr: When code is removed nobody notices it ever existed - and to my experience, in 99% of all real-world cases that is the right thing. In 1%, there may be some value in the outcommented lines, however, if commented-out code stays in for several weeks or months (or longer), chances are high it won't even compile any more due to refactorings of the active code lines. The "pontential value for future uses" argument is often used as a wrong excuse by people who have an emotional problem of stripping things out of the codebase for which they had invested some hours of brainwork.
– Doc Brown
15 hours ago
5
I never commit commented code without additional explanatory comments. There are rare situations where you might want the code back in the near term, but every single one of those is exceptional and requires an explanation for future developers (or future you). 90% of my comments are "Removed because it appears to be unused. Delete after Nov 2021 if there are no problems"
– James Beninger
13 hours ago
3
My colleague once put "There was code here that did X but I removed it" when we removed some feature for the time being. That worked really well; you knew it was in source history for that file, but it wasn't bothering you.
– Erik
4 hours ago
|
show 5 more comments
There is a very simple solution to this: remove the commented-out code.
Really, there are only two good reasons to comment out code: to test something/make a fix, or to save code you might use later. If you're testing or fixing something, remove the commented out code as soon as you're done with the test or fix. If you're saving code you might use later, make it first-class code and put it somewhere such as a library where it can be put to good use.
There is a very simple solution to this: remove the commented-out code.
Really, there are only two good reasons to comment out code: to test something/make a fix, or to save code you might use later. If you're testing or fixing something, remove the commented out code as soon as you're done with the test or fix. If you're saving code you might use later, make it first-class code and put it somewhere such as a library where it can be put to good use.
edited yesterday
answered yesterday
Robert Harvey♦Robert Harvey
170k46 gold badges402 silver badges609 bronze badges
170k46 gold badges402 silver badges609 bronze badges
50
Also, if the code has been checked in: just remove it. If you ever need it back, source control has got you covered
– marstato
yesterday
9
When code is removed nobody notices it ever existed. That makes it harder to recover. There is value in leaving commented code, especially if it seems likely that it will be used in the future.
– usr
16 hours ago
21
@usr: When code is removed nobody notices it ever existed - and to my experience, in 99% of all real-world cases that is the right thing. In 1%, there may be some value in the outcommented lines, however, if commented-out code stays in for several weeks or months (or longer), chances are high it won't even compile any more due to refactorings of the active code lines. The "pontential value for future uses" argument is often used as a wrong excuse by people who have an emotional problem of stripping things out of the codebase for which they had invested some hours of brainwork.
– Doc Brown
15 hours ago
5
I never commit commented code without additional explanatory comments. There are rare situations where you might want the code back in the near term, but every single one of those is exceptional and requires an explanation for future developers (or future you). 90% of my comments are "Removed because it appears to be unused. Delete after Nov 2021 if there are no problems"
– James Beninger
13 hours ago
3
My colleague once put "There was code here that did X but I removed it" when we removed some feature for the time being. That worked really well; you knew it was in source history for that file, but it wasn't bothering you.
– Erik
4 hours ago
|
show 5 more comments
50
Also, if the code has been checked in: just remove it. If you ever need it back, source control has got you covered
– marstato
yesterday
9
When code is removed nobody notices it ever existed. That makes it harder to recover. There is value in leaving commented code, especially if it seems likely that it will be used in the future.
– usr
16 hours ago
21
@usr: When code is removed nobody notices it ever existed - and to my experience, in 99% of all real-world cases that is the right thing. In 1%, there may be some value in the outcommented lines, however, if commented-out code stays in for several weeks or months (or longer), chances are high it won't even compile any more due to refactorings of the active code lines. The "pontential value for future uses" argument is often used as a wrong excuse by people who have an emotional problem of stripping things out of the codebase for which they had invested some hours of brainwork.
– Doc Brown
15 hours ago
5
I never commit commented code without additional explanatory comments. There are rare situations where you might want the code back in the near term, but every single one of those is exceptional and requires an explanation for future developers (or future you). 90% of my comments are "Removed because it appears to be unused. Delete after Nov 2021 if there are no problems"
– James Beninger
13 hours ago
3
My colleague once put "There was code here that did X but I removed it" when we removed some feature for the time being. That worked really well; you knew it was in source history for that file, but it wasn't bothering you.
– Erik
4 hours ago
50
50
Also, if the code has been checked in: just remove it. If you ever need it back, source control has got you covered
– marstato
yesterday
Also, if the code has been checked in: just remove it. If you ever need it back, source control has got you covered
– marstato
yesterday
9
9
When code is removed nobody notices it ever existed. That makes it harder to recover. There is value in leaving commented code, especially if it seems likely that it will be used in the future.
– usr
16 hours ago
When code is removed nobody notices it ever existed. That makes it harder to recover. There is value in leaving commented code, especially if it seems likely that it will be used in the future.
– usr
16 hours ago
21
21
@usr: When code is removed nobody notices it ever existed - and to my experience, in 99% of all real-world cases that is the right thing. In 1%, there may be some value in the outcommented lines, however, if commented-out code stays in for several weeks or months (or longer), chances are high it won't even compile any more due to refactorings of the active code lines. The "pontential value for future uses" argument is often used as a wrong excuse by people who have an emotional problem of stripping things out of the codebase for which they had invested some hours of brainwork.
– Doc Brown
15 hours ago
@usr: When code is removed nobody notices it ever existed - and to my experience, in 99% of all real-world cases that is the right thing. In 1%, there may be some value in the outcommented lines, however, if commented-out code stays in for several weeks or months (or longer), chances are high it won't even compile any more due to refactorings of the active code lines. The "pontential value for future uses" argument is often used as a wrong excuse by people who have an emotional problem of stripping things out of the codebase for which they had invested some hours of brainwork.
– Doc Brown
15 hours ago
5
5
I never commit commented code without additional explanatory comments. There are rare situations where you might want the code back in the near term, but every single one of those is exceptional and requires an explanation for future developers (or future you). 90% of my comments are "Removed because it appears to be unused. Delete after Nov 2021 if there are no problems"
– James Beninger
13 hours ago
I never commit commented code without additional explanatory comments. There are rare situations where you might want the code back in the near term, but every single one of those is exceptional and requires an explanation for future developers (or future you). 90% of my comments are "Removed because it appears to be unused. Delete after Nov 2021 if there are no problems"
– James Beninger
13 hours ago
3
3
My colleague once put "There was code here that did X but I removed it" when we removed some feature for the time being. That worked really well; you knew it was in source history for that file, but it wasn't bothering you.
– Erik
4 hours ago
My colleague once put "There was code here that did X but I removed it" when we removed some feature for the time being. That worked really well; you knew it was in source history for that file, but it wasn't bothering you.
– Erik
4 hours ago
|
show 5 more comments
Hmm, I read this question slightly differently from Robert who correctly asserts that commented out code should be removed.
If, however, you are looking for a convention to mark code for later removal, an old favorite of mine is //b = false; //TODO: remove
Some IDE's flag //TODO:
comments or can be taught to. If not, it's usually a searchable string. It's best to follow whatever convention your shop has established because this can be done several ways. Every code base should do this one way. Keeps it searchable.
quickly parse which is which?
Without that mark the automated way to do this is with the compiler. If stripping the comment off produces code that compiles, it must have been commented code. Writing an IDE plugin that checks that wouldn't be hard. But it will leave buggy commented code behind.
This is why it's better to simply mark commented out code as code the moment you comment it out. This lets you work non-destructively while you decide if you really want it gone. Since we all get interrupted, and are somewhat forgetful, don't be surprised if some lines get checked in while in that state. If they do it's nice that they're at least clearly marked and searchable. Keyboard macros have helped me with this in the past. It's hard to get interrupted in the middle of this if you can do it with a single keystroke.
You can take this as far as enshrining the mark in your continuous integration tests. Oops, I'm trying to check in with outstanding TODO's again.
Instead of seeing if comments compile to label them as code, you could run comments through a natural language processor and label the ones that parse as a sentence or noun phrase in the language your team speaks.
– TheHansinator
4 hours ago
@TheHansinator that sounds good but my experience with my cellphones auto correct relationship with my coder jargon makes me cautious.
– candied_orange
1 hour ago
add a comment |
Hmm, I read this question slightly differently from Robert who correctly asserts that commented out code should be removed.
If, however, you are looking for a convention to mark code for later removal, an old favorite of mine is //b = false; //TODO: remove
Some IDE's flag //TODO:
comments or can be taught to. If not, it's usually a searchable string. It's best to follow whatever convention your shop has established because this can be done several ways. Every code base should do this one way. Keeps it searchable.
quickly parse which is which?
Without that mark the automated way to do this is with the compiler. If stripping the comment off produces code that compiles, it must have been commented code. Writing an IDE plugin that checks that wouldn't be hard. But it will leave buggy commented code behind.
This is why it's better to simply mark commented out code as code the moment you comment it out. This lets you work non-destructively while you decide if you really want it gone. Since we all get interrupted, and are somewhat forgetful, don't be surprised if some lines get checked in while in that state. If they do it's nice that they're at least clearly marked and searchable. Keyboard macros have helped me with this in the past. It's hard to get interrupted in the middle of this if you can do it with a single keystroke.
You can take this as far as enshrining the mark in your continuous integration tests. Oops, I'm trying to check in with outstanding TODO's again.
Instead of seeing if comments compile to label them as code, you could run comments through a natural language processor and label the ones that parse as a sentence or noun phrase in the language your team speaks.
– TheHansinator
4 hours ago
@TheHansinator that sounds good but my experience with my cellphones auto correct relationship with my coder jargon makes me cautious.
– candied_orange
1 hour ago
add a comment |
Hmm, I read this question slightly differently from Robert who correctly asserts that commented out code should be removed.
If, however, you are looking for a convention to mark code for later removal, an old favorite of mine is //b = false; //TODO: remove
Some IDE's flag //TODO:
comments or can be taught to. If not, it's usually a searchable string. It's best to follow whatever convention your shop has established because this can be done several ways. Every code base should do this one way. Keeps it searchable.
quickly parse which is which?
Without that mark the automated way to do this is with the compiler. If stripping the comment off produces code that compiles, it must have been commented code. Writing an IDE plugin that checks that wouldn't be hard. But it will leave buggy commented code behind.
This is why it's better to simply mark commented out code as code the moment you comment it out. This lets you work non-destructively while you decide if you really want it gone. Since we all get interrupted, and are somewhat forgetful, don't be surprised if some lines get checked in while in that state. If they do it's nice that they're at least clearly marked and searchable. Keyboard macros have helped me with this in the past. It's hard to get interrupted in the middle of this if you can do it with a single keystroke.
You can take this as far as enshrining the mark in your continuous integration tests. Oops, I'm trying to check in with outstanding TODO's again.
Hmm, I read this question slightly differently from Robert who correctly asserts that commented out code should be removed.
If, however, you are looking for a convention to mark code for later removal, an old favorite of mine is //b = false; //TODO: remove
Some IDE's flag //TODO:
comments or can be taught to. If not, it's usually a searchable string. It's best to follow whatever convention your shop has established because this can be done several ways. Every code base should do this one way. Keeps it searchable.
quickly parse which is which?
Without that mark the automated way to do this is with the compiler. If stripping the comment off produces code that compiles, it must have been commented code. Writing an IDE plugin that checks that wouldn't be hard. But it will leave buggy commented code behind.
This is why it's better to simply mark commented out code as code the moment you comment it out. This lets you work non-destructively while you decide if you really want it gone. Since we all get interrupted, and are somewhat forgetful, don't be surprised if some lines get checked in while in that state. If they do it's nice that they're at least clearly marked and searchable. Keyboard macros have helped me with this in the past. It's hard to get interrupted in the middle of this if you can do it with a single keystroke.
You can take this as far as enshrining the mark in your continuous integration tests. Oops, I'm trying to check in with outstanding TODO's again.
edited 9 hours ago
answered yesterday
candied_orangecandied_orange
58.8k18 gold badges117 silver badges203 bronze badges
58.8k18 gold badges117 silver badges203 bronze badges
Instead of seeing if comments compile to label them as code, you could run comments through a natural language processor and label the ones that parse as a sentence or noun phrase in the language your team speaks.
– TheHansinator
4 hours ago
@TheHansinator that sounds good but my experience with my cellphones auto correct relationship with my coder jargon makes me cautious.
– candied_orange
1 hour ago
add a comment |
Instead of seeing if comments compile to label them as code, you could run comments through a natural language processor and label the ones that parse as a sentence or noun phrase in the language your team speaks.
– TheHansinator
4 hours ago
@TheHansinator that sounds good but my experience with my cellphones auto correct relationship with my coder jargon makes me cautious.
– candied_orange
1 hour ago
Instead of seeing if comments compile to label them as code, you could run comments through a natural language processor and label the ones that parse as a sentence or noun phrase in the language your team speaks.
– TheHansinator
4 hours ago
Instead of seeing if comments compile to label them as code, you could run comments through a natural language processor and label the ones that parse as a sentence or noun phrase in the language your team speaks.
– TheHansinator
4 hours ago
@TheHansinator that sounds good but my experience with my cellphones auto correct relationship with my coder jargon makes me cautious.
– candied_orange
1 hour ago
@TheHansinator that sounds good but my experience with my cellphones auto correct relationship with my coder jargon makes me cautious.
– candied_orange
1 hour ago
add a comment |
Adding to @RobertHarvey's excellent answer I believe there is only one legitimate reason I've ever encountered for saving commented code to source control, even temporarily: in case of a non-obvious improvement which depends on some third party change. This could be a bug or a feature of the language which is not yet considered stable. It might look something like this:
# TODO: Replace with `foo = frobnicate(bar)` once <link.to/bug> is fixed
foo = [some complex workaround]
Basically, the work has already been done, but you can't yet take advantage of it, so deleting it means someone would have to rediscover it later.
add a comment |
Adding to @RobertHarvey's excellent answer I believe there is only one legitimate reason I've ever encountered for saving commented code to source control, even temporarily: in case of a non-obvious improvement which depends on some third party change. This could be a bug or a feature of the language which is not yet considered stable. It might look something like this:
# TODO: Replace with `foo = frobnicate(bar)` once <link.to/bug> is fixed
foo = [some complex workaround]
Basically, the work has already been done, but you can't yet take advantage of it, so deleting it means someone would have to rediscover it later.
add a comment |
Adding to @RobertHarvey's excellent answer I believe there is only one legitimate reason I've ever encountered for saving commented code to source control, even temporarily: in case of a non-obvious improvement which depends on some third party change. This could be a bug or a feature of the language which is not yet considered stable. It might look something like this:
# TODO: Replace with `foo = frobnicate(bar)` once <link.to/bug> is fixed
foo = [some complex workaround]
Basically, the work has already been done, but you can't yet take advantage of it, so deleting it means someone would have to rediscover it later.
Adding to @RobertHarvey's excellent answer I believe there is only one legitimate reason I've ever encountered for saving commented code to source control, even temporarily: in case of a non-obvious improvement which depends on some third party change. This could be a bug or a feature of the language which is not yet considered stable. It might look something like this:
# TODO: Replace with `foo = frobnicate(bar)` once <link.to/bug> is fixed
foo = [some complex workaround]
Basically, the work has already been done, but you can't yet take advantage of it, so deleting it means someone would have to rediscover it later.
edited 3 hours ago
answered 14 hours ago
l0b0l0b0
7,5712 gold badges28 silver badges38 bronze badges
7,5712 gold badges28 silver badges38 bronze badges
add a comment |
add a comment |
I agree with the answer stating that old code should be removed rather than commented out where possible, however I have observed a convention for those few occasions when commented-out code is needed.
(My basis is C# but this can be applied to any C-syntax language eg java)
// An explanatory comment has a space between the comment marker and the content.
// The following lines are commented out code so do not have the space (except where indented).
//var a = something();
//if(a==2)
// doSomethingElse();
//
New contributor
1
This depends totally on style: When I comment out code, I generally add the//
to the first column, and since virtually all code is indented, the result is virtually always that the comment starts with some tabs. Normal comments don't get a leading space from me, unless there are already other comments with a leading space in the vicinity. So, your method would fail abysmally on comments that I produced, and any method designed to recognize my comment patterns would fail abysmally on yours.
– cmaster
8 hours ago
@cmaster Ah I see, I think I misunderstood the question. I provided a simple way of formatting the comments in such a way that they can be easily parsed for type, which isn't what was asked for.
– IanF1
6 hours ago
add a comment |
I agree with the answer stating that old code should be removed rather than commented out where possible, however I have observed a convention for those few occasions when commented-out code is needed.
(My basis is C# but this can be applied to any C-syntax language eg java)
// An explanatory comment has a space between the comment marker and the content.
// The following lines are commented out code so do not have the space (except where indented).
//var a = something();
//if(a==2)
// doSomethingElse();
//
New contributor
1
This depends totally on style: When I comment out code, I generally add the//
to the first column, and since virtually all code is indented, the result is virtually always that the comment starts with some tabs. Normal comments don't get a leading space from me, unless there are already other comments with a leading space in the vicinity. So, your method would fail abysmally on comments that I produced, and any method designed to recognize my comment patterns would fail abysmally on yours.
– cmaster
8 hours ago
@cmaster Ah I see, I think I misunderstood the question. I provided a simple way of formatting the comments in such a way that they can be easily parsed for type, which isn't what was asked for.
– IanF1
6 hours ago
add a comment |
I agree with the answer stating that old code should be removed rather than commented out where possible, however I have observed a convention for those few occasions when commented-out code is needed.
(My basis is C# but this can be applied to any C-syntax language eg java)
// An explanatory comment has a space between the comment marker and the content.
// The following lines are commented out code so do not have the space (except where indented).
//var a = something();
//if(a==2)
// doSomethingElse();
//
New contributor
I agree with the answer stating that old code should be removed rather than commented out where possible, however I have observed a convention for those few occasions when commented-out code is needed.
(My basis is C# but this can be applied to any C-syntax language eg java)
// An explanatory comment has a space between the comment marker and the content.
// The following lines are commented out code so do not have the space (except where indented).
//var a = something();
//if(a==2)
// doSomethingElse();
//
New contributor
New contributor
answered 13 hours ago
IanF1IanF1
1312 bronze badges
1312 bronze badges
New contributor
New contributor
1
This depends totally on style: When I comment out code, I generally add the//
to the first column, and since virtually all code is indented, the result is virtually always that the comment starts with some tabs. Normal comments don't get a leading space from me, unless there are already other comments with a leading space in the vicinity. So, your method would fail abysmally on comments that I produced, and any method designed to recognize my comment patterns would fail abysmally on yours.
– cmaster
8 hours ago
@cmaster Ah I see, I think I misunderstood the question. I provided a simple way of formatting the comments in such a way that they can be easily parsed for type, which isn't what was asked for.
– IanF1
6 hours ago
add a comment |
1
This depends totally on style: When I comment out code, I generally add the//
to the first column, and since virtually all code is indented, the result is virtually always that the comment starts with some tabs. Normal comments don't get a leading space from me, unless there are already other comments with a leading space in the vicinity. So, your method would fail abysmally on comments that I produced, and any method designed to recognize my comment patterns would fail abysmally on yours.
– cmaster
8 hours ago
@cmaster Ah I see, I think I misunderstood the question. I provided a simple way of formatting the comments in such a way that they can be easily parsed for type, which isn't what was asked for.
– IanF1
6 hours ago
1
1
This depends totally on style: When I comment out code, I generally add the
//
to the first column, and since virtually all code is indented, the result is virtually always that the comment starts with some tabs. Normal comments don't get a leading space from me, unless there are already other comments with a leading space in the vicinity. So, your method would fail abysmally on comments that I produced, and any method designed to recognize my comment patterns would fail abysmally on yours.– cmaster
8 hours ago
This depends totally on style: When I comment out code, I generally add the
//
to the first column, and since virtually all code is indented, the result is virtually always that the comment starts with some tabs. Normal comments don't get a leading space from me, unless there are already other comments with a leading space in the vicinity. So, your method would fail abysmally on comments that I produced, and any method designed to recognize my comment patterns would fail abysmally on yours.– cmaster
8 hours ago
@cmaster Ah I see, I think I misunderstood the question. I provided a simple way of formatting the comments in such a way that they can be easily parsed for type, which isn't what was asked for.
– IanF1
6 hours ago
@cmaster Ah I see, I think I misunderstood the question. I provided a simple way of formatting the comments in such a way that they can be easily parsed for type, which isn't what was asked for.
– IanF1
6 hours ago
add a comment |
If you use an editor with a compiler running in the background (like Xcode and Clang), you can just try to compile the text of the comment. For example”a concise description” gives errors, “b = false;” doesn’t. You could then use different syntax highlighting.
A simpler method would be an IDE plugin that uses some heuristics, like multiple words in a row other than keywords points to comments, matched curly braced point to code etc.
add a comment |
If you use an editor with a compiler running in the background (like Xcode and Clang), you can just try to compile the text of the comment. For example”a concise description” gives errors, “b = false;” doesn’t. You could then use different syntax highlighting.
A simpler method would be an IDE plugin that uses some heuristics, like multiple words in a row other than keywords points to comments, matched curly braced point to code etc.
add a comment |
If you use an editor with a compiler running in the background (like Xcode and Clang), you can just try to compile the text of the comment. For example”a concise description” gives errors, “b = false;” doesn’t. You could then use different syntax highlighting.
A simpler method would be an IDE plugin that uses some heuristics, like multiple words in a row other than keywords points to comments, matched curly braced point to code etc.
If you use an editor with a compiler running in the background (like Xcode and Clang), you can just try to compile the text of the comment. For example”a concise description” gives errors, “b = false;” doesn’t. You could then use different syntax highlighting.
A simpler method would be an IDE plugin that uses some heuristics, like multiple words in a row other than keywords points to comments, matched curly braced point to code etc.
answered 13 hours ago
gnasher729gnasher729
21.5k2 gold badges32 silver badges65 bronze badges
21.5k2 gold badges32 silver badges65 bronze badges
add a comment |
add a comment |
I am interpreting the question different still, thinking you want to find commented out code.
C-style code is bound to have semi-colons in it while comment is unlikely to have semi-colons in it. So for single line commented-out code you could use this regular expression:
s*//[sS]*;
For multi-line commented-out code it could be
/*[^;]*;[^;]**/
Note Visual Studio is a bit peculiar about line breaks in regular expressions, they do not count as whitespace, you need to specify an explicit n.
add a comment |
I am interpreting the question different still, thinking you want to find commented out code.
C-style code is bound to have semi-colons in it while comment is unlikely to have semi-colons in it. So for single line commented-out code you could use this regular expression:
s*//[sS]*;
For multi-line commented-out code it could be
/*[^;]*;[^;]**/
Note Visual Studio is a bit peculiar about line breaks in regular expressions, they do not count as whitespace, you need to specify an explicit n.
add a comment |
I am interpreting the question different still, thinking you want to find commented out code.
C-style code is bound to have semi-colons in it while comment is unlikely to have semi-colons in it. So for single line commented-out code you could use this regular expression:
s*//[sS]*;
For multi-line commented-out code it could be
/*[^;]*;[^;]**/
Note Visual Studio is a bit peculiar about line breaks in regular expressions, they do not count as whitespace, you need to specify an explicit n.
I am interpreting the question different still, thinking you want to find commented out code.
C-style code is bound to have semi-colons in it while comment is unlikely to have semi-colons in it. So for single line commented-out code you could use this regular expression:
s*//[sS]*;
For multi-line commented-out code it could be
/*[^;]*;[^;]**/
Note Visual Studio is a bit peculiar about line breaks in regular expressions, they do not count as whitespace, you need to specify an explicit n.
answered yesterday
Martin MaatMartin Maat
8,7992 gold badges11 silver badges35 bronze badges
8,7992 gold badges11 silver badges35 bronze badges
add a comment |
add a comment |
Ace is a new contributor. Be nice, and check out our Code of Conduct.
Ace is a new contributor. Be nice, and check out our Code of Conduct.
Ace is a new contributor. Be nice, and check out our Code of Conduct.
Ace is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Software Engineering Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsoftwareengineering.stackexchange.com%2fquestions%2f394287%2fis-there-a-method-for-differentiating-informative-comments-from-commented-out-co%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown