C ++中不必要的花括号? [英] Unnecessary curly braces in C++?

查看:153
本文介绍了C ++中不必要的花括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天为同事做代码审查时,我看到了一个奇怪的事情。他用这样的大括号包围了他的新代码:

  Constructor :: Constructor()
{
现有代码

{
新代码:在此处做一些新奇的东西
}

现有代码
}

结果是什么?这可能是什么原因?这个习惯来自哪里?



编辑



和下面的一些问题我觉得我必须添加一些问题,即使我已经标记了一个答案。



环境是嵌入式设备。有很多遗留C代码包装在C ++服装中。有很多C转化为C ++开发人员。



在这部分代码中没有关键部分。我只看到它在这部分的代码。



大括号括起来的代码如下:

  {
bool isInit;
(void)isStillInInitMode(& isInit);
if(isInit){
return isInit;
}
}

(不介意代码,大括号...;))
大括号后面有一些更多的位,状态检查和基本信令。



我跟这个人说,他的动机是限制变量的范围,命名冲突,以及其他一些我无法真正掌握的。



从我的POV这似乎很奇怪,我不认为花括号应该在我们的代码。我在所有的答案中看到一些好的例子,为什么可以用大括号括起来的代码,但不应该将代码分离为方法?

解决方案

它有时很好,因为它给你一个新的范围,你可以更干净声明新的(自动)变量。



C ++ 这可能不是那么重要,因为你可以在任何地方引入新变量,但也许习惯是从 C ,你不能这样做直到C99。 :)

由于 C ++ 有析构函数,它也可以方便地拥有资源(文件,互斥体,自动释放作为范围退出,这可以使事情更清洁。这可能意味着你可以持续一些共享资源的持续时间比你在方法开始时抓住它的时间更短。


When doing a code review for a colleague today I saw a peculiar thing. He had surrounded his new code with curly braces like this:

Constructor::Constructor()
{
   existing code

   {
      New code: do some new fancy stuff here
   }

   existing code
}

What is the outcome, if any, from this? What could be the reason for doing this? Where does this habit come from?

Edit:

Based on the input and some questions below I feel that I have to add some to the question, even though that I already marked an answer.

The environment is embedded devices. There is a lot of legacy C code wrapped in C++ clothing. There are a lot of C turned C++ developers.

There are no critical sections in this part of the code. I have only seen it in this part of the code. There are no major memory allocations done, just some flags that are set, and some bit twiddling.

The code that is surrounded by curly braces is something like:

{
   bool isInit;
   (void)isStillInInitMode(&isInit);
   if (isInit) {
     return isInit;
   }
}

(Don't mind the code, just stick to the curly braces... ;) ) After the curly braces there are some more bit twiddling, state checking, and basic signaling.

I talked to the guy and his motivation was to limit the scope of variables, naming clashes, and some other that I couldn't really pick up.

From my POV this seems rather strange and I don't think that the curly braces should be in our code. I saw some good examples in all the answers on why one could surround code with curly braces, but shouldn't you separate the code into methods instead?

解决方案

It's sometimes nice since it gives you a new scope, where you can more "cleanly" declare new (automatic) variables.

In C++ this is maybe not so important since you can introduce new variables anywhere, but perhaps the habit is from C, where you could not do this until C99. :)

Since C++ has destructors, it can also be handy to have resources (files, mutexes, whatever) automatically released as the scope exits, which can makes thing cleaner. This can mean that you can hold on to some shared resource for a shorter duration than you would do if you grabbed it at the start of the method.

这篇关于C ++中不必要的花括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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