[英]Is it OK to use multi line regular expression to comment out code in Ruby?
When commenting out codes in ruby we have 2 main options 当注释掉ruby中的代码时,我们有两个主要选项
# puts "somecode"
# puts "somecode"
开头添加Sharp, # puts "somecode"
=start puts "somecode" =end
=start puts "somecode" =end
之间, =start puts "somecode" =end
I am lazy and sometimes i put the code between slashes like the code below. 我很懒,有时我将代码放在斜线之间,例如下面的代码。 Its crates a multiple line regex expression.
它创建多行正则表达式表达式。 What kind of side effects this practice can cause?
这种做法会导致什么样的副作用?
CODE 码
puts "hello"
/
puts "world"
/
puts "peace"
OUTPUT 输出值
hello
peace
As Holger points out, most editors have "toggle comments" which makes for a much more reliable way to enable/disable chunks of code. 正如Holger指出的那样,大多数编辑器都有“切换注释”,这使启用/禁用代码块的方式更加可靠。 This is the recommended method.
这是推荐的方法。 Your regular expression approach is too lazy by half, it would be utterly baffling to anyone reading the code at a glance to verify what's going on.
您的正则表达式方法太懒了一半,这对那些一眼看懂代码来验证正在发生的事情的人来说是莫名其妙的。
Making commits with inadvertently disabled code is a thing that will happen, so you want your comments to be both obvious and easy to repair. 使用无意间禁用的代码进行提交是一件会发生的事情,因此您希望注释既清晰又易于修复。
Which one looks better: 哪个看起来更好:
def example
do_important_stuff!
/
other(thing: true)
do_super_important_stuff(factor: 9000)
/
do_other_stuff
end
Versus the more intuitive: 与更直观:
def example
do_important_stuff!
# other(thing: true)
# do_super_important_stuff(factor: 9000)
do_other_stuff
end
As with many editors, Stack Overflow's syntax highlighting immediately shows that there's code here that's commented out. 与许多编辑器一样,Stack Overflow的语法高亮立即显示出其中的代码已被注释掉。 The other version just looks bizarre .
另一个版本看起来很奇怪 。
If you find it too tedious to enable/disable commenting you probably need to let your editor help you out, or configure it to make it easier to do that. 如果您发现启用/禁用评论过于繁琐,则可能需要让您的编辑器来帮助您,或者对其进行配置以使其更容易实现。
Using Tadman's example 以塔德曼的例子
def example
do_important_stuff!
/
other(thing: true)
do_super_important_stuff(factor: 9000)
/
do_other_stuff
end
This creates a Regex object every time the method is called, which is not free. 每次调用该方法都会创建一个Regex对象,这不是免费的。 It takes time, memory and extra work for the garbage collection process.
垃圾收集过程需要时间,内存和额外的工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.