if 或 Else 的结果为正?

发布于 2024-12-04 17:59:25 字数 533 浏览 0 评论 0原文

这是基于编码风格的,想知道是否最好将 if/else 决策的积极结果放在 if 或 else 块中。例如:

    yes_no = object.get() #Response will be 'yes' or 'no'
    if yes_no == 'yes':
        yes response
    else:
        no response

优于:

    yes_no = object.get() #Response will be 'yes' or 'no'
    if yes_no != 'yes':
        no response
    else:
        yes response

据我所知,没有什么区别,因为它们都寻找“是”并输出“是”响应,但是是否有人们遵循或发现更容易阅读的任何样式?

我正在使用 python,但由于 if/else 无处不在,我想其他语言也相关。

编辑:替换通行证没有任何响应,因为没有必要。

This is coding style based, was wondering if it's better to put a positive outcome from an if/else decision in the if or else block. For example is:

    yes_no = object.get() #Response will be 'yes' or 'no'
    if yes_no == 'yes':
        yes response
    else:
        no response

better than:

    yes_no = object.get() #Response will be 'yes' or 'no'
    if yes_no != 'yes':
        no response
    else:
        yes response

As far as I can tell there's no difference, as they both look for a 'yes' and output a yes response, but is there any sort of styling that people follow or find easier to read?

I'm using python, but since if/else is everywhere I guess other languages are relevant also.

EDIT: Replaced passes with no response, as were unnecessary.

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

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

发布评论

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

评论(5

删除→记忆 2024-12-11 17:59:25

我认为这主要取决于程序员想要强调的内容。 IF 条件应该是:

  • 最可能的
  • 最简单的
  • 导致计算量更少的
  • 更具可读性
  • ...

就我个人而言,我总是尽量避免这种情况:

if condition:
    ... tons of code ...
else:
    ... one line of code ...

I think that most depends on what the programmer wants to highlight. The IF condition should be either:

  • the most probable
  • the simplest
  • the one which leads to less calculation
  • the more readable
  • ...

Personally I always try to avoid this:

if condition:
    ... tons of code ...
else:
    ... one line of code ...
坚持沉默 2024-12-11 17:59:25
  • 大多数时候,避免双重否定(例如 !is_no)会更干净
  • 无论条件是什么,都首先放置早期语句:

    if(!a): 
        返回
    别的:
        长的 
        顺序
        的
        声明
    
  • Most of the time it's cleaner to avoid double negative (like !is_no)
  • Put early out statements first, whatever the condition is:

    if(!a): 
        return
    else:
        long 
        sequence
        of
        statements
    
十年不长 2024-12-11 17:59:25

这严格取决于您的程序的逻辑。如果提出“积极”条件有意义,那么就将其设为积极条件。如果它是一个消极的“条件”,那就让它成为一个消极的条件。对于这两种类型的情况,都没有固定的风格规则,或者至少据我所知没有。

That strictly depends on your program's logic. If it makes sense to make a "positive" condition, then make it a positive condition. If it's a negative "condition", then make it a negative condition. There are no set rules of style when it comes to conditions of either type, or at least not that I am aware of.

甜心小果奶 2024-12-11 17:59:25

我能给你的最好建议是在代码中做最容易读的事情,不要为不添加任何内容的代码路径而烦恼。我会将您的代码修改为:

yes_no = object.get() #Response will be 'yes' or 'no'
    if yes_no == 'yes':
        yes response

如果没有针对 no 条件的操作,则没有理由检查它。我还会修改“get”,使其更明显地显示为什么它会返回“是”或“否”响应。实际上,object.get() 相当模糊。

Best advice I could give you is do what reads best in your code and don't bother with code paths that don't add anything. I would rework your code as:

yes_no = object.get() #Response will be 'yes' or 'no'
    if yes_no == 'yes':
        yes response

If there's no action for the no condition there's no reason to check for it. I'd also rework the "get" to make it more obvious why it is returning a yes or no response. As it is, object.get() is pretty vague.

一身骄傲 2024-12-11 17:59:25

如果您的方法仅返回两个值之一,请让它返回 True返回 False,而不是任意字符串。您可能应该重命名它,以便方法名称有意义。

那么您可以执行

if object.get():
    # do something
else:
    # do something else

或省略 else

if not object.get():
    # do something
else:
    # do something else

如果只有一种情况实际上会导致响应,

If your method only returns one of two values, have it return True or return False, not arbitrary strings. And you should probably rename it so the method name means something.

Then you can do

if object.get():
    # do something
else:
    # do something else

or

if not object.get():
    # do something
else:
    # do something else

omitting the else if only one case actually leads to a response.

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