什么是使用大括号的目的(即{})为单行if或循环? [英] What's the purpose of using braces (i.e. {}) for a single-line if or loop?

查看:222
本文介绍了什么是使用大括号的目的(即{})为单行if或循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在读我的C ++讲师的一些讲义和他写道:


  

      
  1. 使用缩进//确定

  2.   
  3. 从不依靠运营商precedence - 始终用括号//确定

  4.   
  5. 始终使用{}块 - 即使是单行// 不确定,为什么???

  6.   
  7. 在比较左侧const对象//确定

  8.   
  9. 使用符号对于那些> = 0 //好的技巧变量

  10.   
  11. 设置指针为NULL删除后 - 双人删除保护//不坏

  12.   

第3技术目前尚不清楚对我说:我会获得通过放置一条线在
一个 {...}

例如,采取这种怪异的code:

  INT J = 0;
的for(int i = 0; I< 100; ++ I)
{
    如果(我%2 == 0)
    {
        J ++;
    }
}

和其替换为:

  INT J = 0;
的for(int i = 0; I< 100; ++ I)
    如果(我%2 == 0)
        J ++;

什么是使用第1版的好处?


解决方案

让我们尝试同时修改 I 当我们增加Ĵ

  INT J = 0;
的for(int i = 0; I< 100; ++ I)
    如果(我%2 == 0)
        J ++;
        我++;

哦,不!从Python的到来,这看起来不错,但实际上它不是,因为它等同于:

  INT J = 0;
的for(int i = 0; I< 100; ++ I)
    如果(我%2 == 0)
        J ++;
我++;

当然,这是一个愚蠢的错误,而是一个有经验的程序员可以做。

的另一个很好的理由 ta.speot.is的回答指出。

第三一个我能想到的是嵌套如果的:

 如果(COND1)
   如果(COND2)
      做一点事();

现在,假设你现在想 doSomethingElse() COND1 不满足(新功能)。所以:

 如果(COND1)
   如果(COND2)
      做一点事();
其他
   doSomethingElse();

这显然是错误的,因为与其他联营内如果


编辑:由于这是得到一些关注,我要澄清我的观点。我当时回答的问题是:


  

什么是使用第1版的好处?


我所描述的。有一些好处。但是,海事组织,永远的规则并不总是适用。所以,我并不完全支持


  

始终使用{}块 - 即使是单行//也不行,为什么?


我不是说的总是的使用 {} 块。如果这是一个很简单的条件和放大器;的行为,不要。如果您怀疑后与功放有人会进来;改变你的code以添加功能,做的。

I'm reading some lecture notes of my C++ lecturer and he wrote the following:

  1. Use Indentation // OK
  2. Never rely on operator precedence - Always use parentheses // OK
  3. Always use a { } block - even for a single line // not OK, why ???
  4. Const object on left side of comparison // OK
  5. Use unsigned for variables that are >= 0 // nice trick
  6. Set Pointer to NULL after deletion - Double delete protection // not bad

The 3rd technique is not clear to me: what would I gain by placing one line in a { ... }?

For example, take this weird code:

int j = 0;
for (int i = 0 ; i < 100 ; ++i)
{
    if (i % 2 == 0)
    {
        j++;
    }
}

and replace it with:

int j = 0;
for (int i = 0 ; i < 100 ; ++i)
    if (i % 2 == 0)
        j++;

What's the benefit of using the 1st version?

解决方案

Let's attempt to also modify i when we increment j:

int j = 0;
for (int i = 0 ; i < 100 ; ++i)
    if (i % 2 == 0)
        j++;
        i++;

Oh no! Coming from Python, this looks ok, but in fact it isn't, as it's equivalent to:

int j = 0;
for (int i = 0 ; i < 100 ; ++i)
    if (i % 2 == 0)
        j++;
i++;

Of course, this is a silly mistake, but one that even an experienced programmer could make.

Another very good reason is pointed out in ta.speot.is's answer.

A third one I can think of is nested if's:

if (cond1)
   if (cond2) 
      doSomething();

Now, assume you now want to doSomethingElse() when cond1 is not met (new feature). So:

if (cond1)
   if (cond2) 
      doSomething();
else
   doSomethingElse();

which is obviously wrong, since the else associates with the inner if.


Edit: Since this is getting some attention, I'll clarify my view. The question I was answering is:

What's the benefit of using the 1st version?

Which I have described. There are some benefits. But, IMO, "always" rules don't always apply. So I don't wholly support

Always use a { } block - even for a single line // not OK, why ???

I'm not saying always use a {} block. If it's a simple enough condition & behavior, don't. If you suspect someone might come in later & change your code to add functionality, do.

这篇关于什么是使用大括号的目的(即{})为单行if或循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆