快速、简单地删除“死”的方法(注释掉)代码

发布于 2024-08-24 15:37:25 字数 220 浏览 4 评论 0原文

我正在使用继承的代码库,其中包含数千行注释掉的代码。我知道以前的编码员打算为后代保存他所有的辛勤工作,而不是简单地删除它,但是:我永远不会阅读它,它只会妨碍我。一个问题示例是,当我对某些代码段执行文本搜索时,我在注释代码中得到了数十个“错误”命中。皮塔饼。

有没有一种快速/简单的方法来检测大块注释掉的代码?也许是一个聪明的正则表达式?

我此时恰好在 VB.NET 中工作,注释字符是一个撇号。

I'm working with an inherited code base which contains thousands of lines of commented out code. I know the previous coder meant to save all his hard work for posterity rather than simply deleting it but: I will never read it and it just gets in the way. One problem example is that when I perform text searches for certain code segments I gets dozens of "false" hits in the commented code. PITA.

Is there a quick/easy way to detect large blocks of commented out code? A clever RegEx perhaps?

I happen to be working in VB.NET at this time and comment character is a single apostrophe.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

昇り龍 2024-08-31 15:37:25

您可以使用正则表达式搜索。搜索

^.*'.*$

查找带有注释的单行。您可能需要找到至少 3 行以注释开头的行:

^.*'.*\n.*'.*\n.*'.*$

让猫远离您的键盘。

You can use a Regular Expression search. Search for

^.*'.*$

To find a single line with a comment. You'll probably want to find at least 3 lines that start with a comment:

^.*'.*\n.*'.*\n.*'.*$

Keep the cat away from your keyboard.

撑一把青伞 2024-08-31 15:37:25
(^\s*//.*\n)^10

找到 10 个并发注释掉 // 样式的行。

注释掉测试:

\s*//\s*\[Test\].*\n
(^\s*//.*\n)^10

Find 10 concurrent commented out lines of the // style.

Commented out tests:

\s*//\s*\[Test\].*\n
〗斷ホ乔殘χμё〖 2024-08-31 15:37:25

恐怕我同意达菲莫的观点。我认为您不会找到可靠的自动方法来删除注释掉的代码。我确信,如果您足够努力地搜索,您会找到一个,但您的时间最好花在工作上。

我过去曾经遇到过这种情况(太频繁了),我最终要做的是在处理各种模块时删除注释掉的代码。

例如,我打开 Person 类进行更改,然后我看到尚未删除的已注释掉的代码。我检查代码(我们使用 VSS),删除错误代码,将其签入,最后检查它以完成我的工作。

这一切都需要时间才能消失,但我认为这是解决问题的有效利用时间。

I'm afraid I agree with duffymo. I don't think you'll find a reliable automatic way to remove the commented out code. I'm sure if you search hard enough, you'll find one but your time would be better spent on your work.

I've been in this situation in the past (far too often) and what I end up doing is removing the commented out code as I work on various modules.

As an example, I open class Person to make a change and I see commented out code that has yet to be removed. I checkout the code (we use VSS), remove the bad code, check it in and finally, check it out to do my work.

It takes time before it all goes away, but I feel it is an effective use of time to resolve the issue.

葬花如无物 2024-08-31 15:37:25

对于最终到达这里的任何 C# 人员,请使用以下正则表达式来查找注释掉的行。

(^|[^/])//[^/]

For any C# people who end up here, use the following regex to find commented out lines.

(^|[^/])//[^/]
紅太極 2024-08-31 15:37:25

这就是版本控制系统的用途。

我会确保它处于版本控制之下(希望不是 Visual Source Safe),检查它,删除所有带注释的代码,然后重新检查它。

我也不鼓励您的开发团队将来这样做。

That's what version control systems are for.

I'd make sure it was under version control (hopefully not Visual Source Safe), check it out, remove all the commented code, and check it back in.

I'd also discourage the practice in your development team for the future.

风吹雨成花 2024-08-31 15:37:25

我建议为 Visual Studio 编写或查找一个宏来帮助删除注释。

一些伪逻辑:

  • 从行号开始,读取第一个字符。记住这个行号。
  • 如果是VB注释字符',则继续
  • 读取下一行的第一个字符。如果是注释字符,则继续。
  • 当找到不是注释字符的行时,分析遍历的行数。
  • 如果遍历的行数与您的阈值n匹配,则删除它们。

I'd suggest writing or finding a macro for Visual Studio that will help delete comments.

Some pseudo-logic:

  • start at a line number, read first character. remember this line number.
  • if is the VB comment character ', then continue
  • read next line's first character. if is comment character, continue.
  • when finding a line that isn't a comment character, analyze the number of lines traversed.
  • if the number of lines traversed matches your threshold of n, then delete them.
奢望 2024-08-31 15:37:25

我知道这是一个旧线程,但没有提到对我有帮助的技巧。

我搜索这个正则表达式:

(^\s*'.=.\n)^

它找到包含等号的注释行。我的经验是,大多数注释掉的代码块至少包含一行具有赋值的行,并且等号在实际注释中很少见。

我不会自动进行替换,我只需按 F3,如果它与一行匹配,我只需按 Enter 键即可用新行替换突出显示的文本。如果它落在属于注释掉的行块的一部分的行上,那么我只需手动选择,然后再次按 Enter 和 F3。

I know that this is an old thread but the trick that helps me hasn't been mentioned.

I search for this regex:

(^\s*'.=.\n)^

It finds commented out lines that contain an equals sign. My experience is that most blocks of commented out code contain at least one line that has an assignment and that equals signs are quite rare in real comments.

I don't automate the replacement, I just press F3 and if it matches a single line I just press the enter key to replace the highlighted text with a new line. If it lands on a line that is part of a block of commented out lines then I just manually select, press enter and the F3 again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文