函数和声明局部变量

发布于 2024-09-08 13:18:44 字数 594 浏览 5 评论 0原文

刚刚在工作中与同事讨论如何声明变量。 对我来说,我已经决定了我喜欢哪种风格,但也许我错了。

“C”风格 - 所有变量都位于函数开头。 如果你想知道变量的数据类型,只需查看函数的开头即可。

bool Foo()
{
    PARAM* pParam = NULL;
    bool rc;

    while (true)
    {
       rc = GetParam(pParam);
       ... do something with pParam
    }
}

“C++”风格 - 尽可能将变量声明为局部变量。

bool Foo()
{       
    while (true)
    {
        PARAM* pParam = NULL;

        bool rc = GetParam(pParam);
       ... do something with pParam
    }
}

你更喜欢什么?

更新 问题是关于 POD 变量。

Just having an conversation with collegue at work how to declare a variables.
For me I already decided which style I prefer, but maybe I wrong.

"C" style - all variable at the begining of function.
If you want to know data type of variable, just look at the begining of function.

bool Foo()
{
    PARAM* pParam = NULL;
    bool rc;

    while (true)
    {
       rc = GetParam(pParam);
       ... do something with pParam
    }
}

"C++" style - declare variables as local as possible.

bool Foo()
{       
    while (true)
    {
        PARAM* pParam = NULL;

        bool rc = GetParam(pParam);
       ... do something with pParam
    }
}

What do you prefer?

Update
The question is regarding POD variables.

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

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

发布评论

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

评论(6

北恋 2024-09-15 13:18:44

第二个。 (C++风格)
这样做至少有两个充分的理由:

  1. 允许您在代码中应用 YAGNI 原则,因为您只在需要变量时声明变量,尽可能接近它们的使用。这使得代码更容易快速理解,因为您不必在函数中来回切换即可理解所有内容。每个变量的类型是有关该变量的主要信息,并且在变量名称中并不总是显而易见的。简而言之:代码更容易阅读
  2. 允许更好的编译器优化(如果可能)。阅读:http://www.tantalon.com/pete/cppopt/asyougo.htm #PostponeVariableDeclaration

The second one. (C++ style)
There are at least two good reasons for this:

  1. This allow you to apply the YAGNI principle in the code, as you only declare variable when you need them, as close as possible to their use. That make the code easier to understand quickly as you don't have to get back and forth in the function to understand it all. The type of each variable is the main information about the variable and is not always obvious in the varaible name. In short : the code is easier to read.
  2. This allow better compiler optimizations (when possible). Read : http://www.tantalon.com/pete/cppopt/asyougo.htm#PostponeVariableDeclaration
香草可樂 2024-09-15 13:18:44

如果由于您使用的语言的原因,您需要在函数顶部声明变量,那么显然您必须这样做。

如果您可以选择,那么在使用变量的地方声明变量会更有意义。我使用的经验法则是:声明具有所需的最小范围的变量。

减小变量的范围可以防止某些类型错误,例如,您意外地在循环外部使用了仅应在循环内部使用的变量。减小变量的范围将使编译器能够发现错误,而不是让代码在运行时编译但失败。

If due to the language you are using you are required to declare variables at the top of the function then clearly you must do this.

If you have a choice then it makes more sense to declare variables where they are used. The rule of thumb I use is: Declare variables with the smallest scope that is required.

Reducing the scope of a variable prevents some types errors, for example where you accidentally use a variable outside of a loop that was intended only to be used inside the loop. Reducing the scope of the variable will allow the compiler to spot the error instead of having code that compiles but fails at runtime.

腹黑女流氓 2024-09-15 13:18:44

我更喜欢“C++风格”。主要是因为它允许 RAII,您在 bool 变量的两个示例中都这样做。

此外,变量的严格范围为编译提供了更好的优化机会。

I prefer the "C++ style". Mainly because it allows RAII, which you do in both your examples for the bool variable.

Furthermore, having a tight scope for the variable provides the compile better oppertunities for optimizations.

行雁书 2024-09-15 13:18:44

这可能有点主观。

我更喜欢尽可能在本地,因为它可以完全清楚变量的预期范围,并且如果您在预期的有用范围之外访问它,编译器会生成错误。

This is probably a bit subjective.

I prefer as locally as possible because it makes it completely clear what scope is intended for the variable, and the compiler generates an error if you access it outside the intended useful scope.

霓裳挽歌倾城醉 2024-09-15 13:18:44

这不是风格问题。在 C++ 中,非 POD 类型将在声明时调用其构造函数,并在作用域末尾调用其析构函数。您必须明智地选择声明变量的位置,否则会导致不必要的性能问题。例如,在循环内声明类变量可能不是最明智的想法,因为构造函数/析构函数将在循环的每次迭代中被调用。但有时,如果变量有可能根本不被使用(就像变量仅在某些“if”语句中使用),那么在函数顶部声明类变量可能不是最好的。

This isn't a style issue. In C++, non-POD types will have their constructors called at the point of declaration and destructors called at the end of the scope. You have to be wise about selecting where to declare variables or you will cause unnecessary performance issues. For example, declaring a class variable inside a loop may not be the wisest idea since constructor/destructor will be called every iteration of the loop. But sometimes, declaring class variables at the top of the function may not be the best if there is a chance that variable doesn't get used at all (like a variable is only used inside some 'if' statement).

淡写薰衣草的香 2024-09-15 13:18:44

我更喜欢 C 风格,因为 C++ 风格对我来说有一个主要缺陷:在密集函数中,眼睛很难找到变量的声明/初始化。 (还没有任何语法突出显示能够可靠且可预测地应对我的 C++ 编码 hazards 习惯。)

尽管我确实不严格遵守任何样式:只有关键变量放在那里,并且大多数较小的次要变量都位于在需要的地方进行阻止(例如示例中的 bool rc )。

但我的代码中所有重要的关键变量最终都不可避免地在顶部声明。如果在嵌套块中我有太多局部变量,则表明我必须开始考虑将代码拆分为更小的函数。

I prefer C style because the C++ style has one major flaw to me: in a dense function it is very hard on eyes to find the declaration/initialization of the variable. (No syntax highlighting was able yet to cope reliably and predictably with my C++ coding hazards habits.)

Though I do adhere to no style strictly: only key variables are put there and most smallish minor variables live within the block where they are needed (like bool rc in your example).

But all important key variables in my code inevitably end up being declared on the top. And if in a nested block I have too much local variables, that is the sign that I have to start thinking about splitting the code into smaller functions.

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