将代码语句放在大括号中是一种好的编程习惯吗?

发布于 2024-08-15 02:37:50 字数 455 浏览 6 评论 0原文

在我一生中读过的所有源代码中,我从未见过这样做的。如果它被认为是不好的编程实践,那么一定有一个我无法理解的原因。另外,我认为它有时会提高可读性而不是恶化它。以下是我在 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 技术交流群。

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

发布评论

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

评论(4

三生池水覆流年 2024-08-22 02:37:50

尽可能力求简单总是一个好主意,为此,最好以直截了当的方式陈述事情。像这样的声明使得很难确定变量的来源,因为它们相当彻底地嵌入到语句中。

在括号内声明作用域变量通常被认为是可以接受的:

if (found = MyModel.find_by_pigeon_id(params[:pigeon_id]))
  # Variable 'found' used only within this block
end

# Ruby variables will persist here, but in many languages they are out of scope

更详细的版本实际上具有含义:

found = MyModel.find_by_pigeon_id(params[:pigeon_id])
if (found)
  # Variable 'found' can be used here
end

# Implies 'found' may be used here for whatever reason

能够扫描整个程序并非常清楚地看到声明的所有变量总是很好。隐藏事情除了让人们感到沮丧之外没有任何目的。

就你可以逃脱的程度而言,Ruby 比许多其他语言要轻松得多。有些语言会因为使事情复杂化而严厉惩罚你,因为声明或转换中的微小错误可能会产生巨大的后果。这并不意味着您应该抓住每一个机会充分利用这一点。

以下是我建议如何实现您的第一个示例:

# Ensure that @select_file is defined
@select_file ||= Qt::FileDialog.new

@pushButton.connect(SIGNAL(:clicked)) do
  # Block is split out into multiple lines for clarity
  @select_file.show
end

第二个:

# Simple declaration, variable name inherited from class name, not truncated
timer = Qt::Timer.new

timer.connect(SIGNAL(:timeout)) do 
  # Long parameter list is broken out into separate lines to make it clear
  # what the ordering is. Useful for identifying accidentally missing parameters.
  @label.text = Qt::Application.translate(
    "MainWindow",
    "The time right now is #{Time.now}",
    nil,
    Qt::Application::UnicodeUTF8
  )
end

timer.start(1000)

我发现最复杂的程序通常看起来最简单,因为它们是由经验丰富的人编写的,他们知道如何以简单的方式表达事物。

有趣的是,一些最简单的程序往往是最复杂的,因为它们是由新手编写的,这些新手要么是在哗众取宠、炫耀,要么是自掘深渊,不断地向问题扔代码以期解决问题。

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:

if (found = MyModel.find_by_pigeon_id(params[:pigeon_id]))
  # Variable 'found' used only within this block
end

# Ruby variables will persist here, but in many languages they are out of scope

A more verbose version actually has implications:

found = MyModel.find_by_pigeon_id(params[:pigeon_id])
if (found)
  # Variable 'found' can be used here
end

# Implies 'found' may be used here for whatever reason

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:

# Ensure that @select_file is defined
@select_file ||= Qt::FileDialog.new

@pushButton.connect(SIGNAL(:clicked)) do
  # Block is split out into multiple lines for clarity
  @select_file.show
end

The second:

# Simple declaration, variable name inherited from class name, not truncated
timer = Qt::Timer.new

timer.connect(SIGNAL(:timeout)) do 
  # Long parameter list is broken out into separate lines to make it clear
  # what the ordering is. Useful for identifying accidentally missing parameters.
  @label.text = Qt::Application.translate(
    "MainWindow",
    "The time right now is #{Time.now}",
    nil,
    Qt::Application::UnicodeUTF8
  )
end

timer.start(1000)

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.

守护在此方 2024-08-22 02:37:50

我觉得读起来比较困难。我希望它稍微详细一些,但更清晰:

tmr = Qt::Timer.new
tmr.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 think it's rather hard to read. I would prefer it slightly more verbose, but clearer:

tmr = Qt::Timer.new
tmr.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)
樱桃奶球 2024-08-22 02:37:50

一行中塞满太多逻辑语句的代码很难阅读,因为读者本质上必须在大脑中将其展开为类似于 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:

  • It makes the line length flow past the border of most text editor windows.
  • It obscures individual steps you might need to see while debugging.
荭秂 2024-08-22 02:37:50

我想你的意思是父母?

Brace: {

Paren: (

如果您使用大括号而不是 do/end,您的第一个示例会变得更具可读性——按照惯例,如果块位于单行上,则使用大括号,而 do/end 用于多行块。


@pushButton.connect(SIGNAL :clicked) { (@select_file ||= Qt::FileDialog.new).show }

第二个我在 java/c 中见过很多例子,但实际上我在 ruby​​ 中看到了它的更多功能:

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.start(1000)

但是在启动它之后你就​​没有对该对象的引用。

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.


@pushButton.connect(SIGNAL :clicked) { (@select_file ||= Qt::FileDialog.new).show }

The second example I've seen a lot of in java/c, but I've actually seen it done more functionally in ruby:

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.start(1000)

But you then don't have a reference to the object after starting it.

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