将代码语句放在大括号中是一种好的编程习惯吗?
在我一生中读过的所有源代码中,我从未见过这样做的。如果它被认为是不好的编程实践,那么一定有一个我无法理解的原因。另外,我认为它有时会提高可读性而不是恶化它。以下是我在 ruby 代码中完成的几个地方。
@pushButton.connect(SIGNAL :clicked) do (@select_file ||= Qt::FileDialog.new).show end
和
(tmr=Qt::Timer.new).connect SIGNAL :timeout do
@label.text = Qt::Application.translate("MainWindow", "The time right now is #{Time.now}", nil, Qt::Application::UnicodeUTF8)
end
tmr.start(1000)
I have never seen this being done anywhere in all the source code i've read in my life. If it is considered bad programming practice, there has to be a reason for it which i fail to understand. Also, I think it sometimes improves readability rather than worsening it. Here are a few places i've done it in my ruby code.
@pushButton.connect(SIGNAL :clicked) do (@select_file ||= Qt::FileDialog.new).show end
and
(tmr=Qt::Timer.new).connect SIGNAL :timeout do
@label.text = Qt::Application.translate("MainWindow", "The time right now is #{Time.now}", nil, Qt::Application::UnicodeUTF8)
end
tmr.start(1000)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尽可能力求简单总是一个好主意,为此,最好以直截了当的方式陈述事情。像这样的声明使得很难确定变量的来源,因为它们相当彻底地嵌入到语句中。
在括号内声明作用域变量通常被认为是可以接受的:
更详细的版本实际上具有含义:
能够扫描整个程序并非常清楚地看到声明的所有变量总是很好。隐藏事情除了让人们感到沮丧之外没有任何目的。
就你可以逃脱的程度而言,Ruby 比许多其他语言要轻松得多。有些语言会因为使事情复杂化而严厉惩罚你,因为声明或转换中的微小错误可能会产生巨大的后果。这并不意味着您应该抓住每一个机会充分利用这一点。
以下是我建议如何实现您的第一个示例:
第二个:
我发现最复杂的程序通常看起来最简单,因为它们是由经验丰富的人编写的,他们知道如何以简单的方式表达事物。
有趣的是,一些最简单的程序往往是最复杂的,因为它们是由新手编写的,这些新手要么是在哗众取宠、炫耀,要么是自掘深渊,不断地向问题扔代码以期解决问题。
It's always a good idea to strive for simplicity wherever possible, and to that end it's best to state things in a straightforward manner. Declarations like that make it hard to determine where variables originate as they're embedded rather thoroughly inside the statement.
Declaring scoped variables within brackets is usually considered acceptable:
A more verbose version actually has implications:
It's always nice to be able to scan up through the program and see, quite clearly, all the variables as they're declared. Hiding things serves no purpose other than to frustrate people.
Ruby is a lot more relaxed than many other languages in terms of how much you can get away with. Some languages will punish you severely for complicating things because a tiny mistake in declaration or casting can have enormous ramifications. That doesn't mean you should take full advantage of that at every opportunity.
Here's how I'd advocate implementing your first example:
The second:
I've found that the most complicated programs often look the simplest, as they're written by people with lots of experience who know how to express things in a straightforward manner.
Interestingly enough, some of the simplest programs are often the most complicated as they're written by novices who are either grandstanding and showing off or are digging themselves into a deep ditch and keep throwing code at the problem in the hopes of fixing it.
我觉得读起来比较困难。我希望它稍微详细一些,但更清晰:
I think it's rather hard to read. I would prefer it slightly more verbose, but clearer:
一行中塞满太多逻辑语句的代码很难阅读,因为读者本质上必须在大脑中将其展开为类似于 troelskn 的答案的语句。
它的可读性较差的其他原因是:
Code with too many logical statements crammed into 1 line is hard to read, because the reader essentially has to unwind it in their brain into a statement similar to troelskn's answer.
Other reasons it's less readabale:
我想你的意思是父母?
Brace: {
Paren: (
如果您使用大括号而不是 do/end,您的第一个示例会变得更具可读性——按照惯例,如果块位于单行上,则使用大括号,而 do/end 用于多行块。
第二个我在 java/c 中见过很多例子,但实际上我在 ruby 中看到了它的更多功能:
但是在启动它之后你就没有对该对象的引用。
I think you mean parens?
Brace: {
Paren: (
Your first example becomes slightly more readable if you use braces instead of do/end---conventionally you use braces if a block is on a single line, with do/end for multi-line blocks.
The second example I've seen a lot of in java/c, but I've actually seen it done more functionally in ruby:
But you then don't have a reference to the object after starting it.