在外部初始化全局变量是不是很糟糕? [英] Is it that bad to initialize global variables at outside?

查看:63
本文介绍了在外部初始化全局变量是不是很糟糕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

偶然的机会,我开始明白,在程序之外初始化全局变量是不好的编程习惯。这样糟糕吗?在

为了满足这个要求,我不得不用const声明它们,但此后

它们不再是变量。只是看不出这个

练习的好处。欢迎提出任何意见或建议。


谢谢

vib

Hi there,

By chance, I came to learn that it is bad programming practice to
initialize global variables at outside of programs. Is it that bad? In
order to fullfil this, I had to declare them with const, but thereafter
they are no longer variables. Just couldn''t see the benefits of this
practice. Any comments or advices are welcome.

Thanks
vib

推荐答案

2005年6月6日06:14:04 -0700,vib < 6 ***** @ gmail.com>写道:
On 6 Jun 2005 06:14:04 -0700, "vib" <vi*****@gmail.com> wrote:
你好,

偶然的机会,我开始了解到在程序外部初始化全局变量是不好的编程习惯。这样糟糕吗?为了完成这个,我不得不用const声明它们,但此后它们不再是变量。只是看不出这种实践的好处。欢迎提出任何意见或建议。
Hi there,

By chance, I came to learn that it is bad programming practice to
initialize global variables at outside of programs. Is it that bad? In
order to fullfil this, I had to declare them with const, but thereafter
they are no longer variables. Just couldn''t see the benefits of this
practice. Any comments or advices are welcome.




全局常量变量确实很少使用。如果你需要

可以在你的程序中使用一些常数值,那么你应该使用#define或enum来获得



代码完整一书(McConnel,现已进入第2版)列出

避免全局变量的原因以及他们可以使用b $ b b的一些原因。一般来说,如果有一个

替代品,应该避免使用它们。



Global const variables are, indeed, of very little use. If you need
some constant value that can be used in your program then you should
use a #define or enum.

The book "Code Complete" (McConnel, now in its 2nd edition) lists
reasons to avoid global variables and some reasons in which they can
be handy. In general, they should be avoided if there is an
alternative.


" vib" < 6 ***** @ gmail.com>写道:
"vib" <vi*****@gmail.com> writes:
偶然的机会,我开始了解到在程序外部初始化全局变量是不好的编程习惯。这样糟糕吗?为了完成这个,我不得不用const声明它们,但此后它们不再是变量。只是看不出这种实践的好处。欢迎提出任何意见或建议。
By chance, I came to learn that it is bad programming practice to
initialize global variables at outside of programs. Is it that bad? In
order to fullfil this, I had to declare them with const, but thereafter
they are no longer variables. Just couldn''t see the benefits of this
practice. Any comments or advices are welcome.




无论是否初始化它们都不是问题。全局变量

本身可能会导致问题。例如,如果函数

仅与其调用者通信,并且仅通过其参数和

返回值,则分析它的作用相对容易。如果它
也使用全局变量,它可以影响并受其他任何使用相同全局变量的函数的影响,即使在
$中也是如此。 b $ b没有明确的电话。

程序中各种函数之间的依赖关系图,原本就是一个简单的树形结构,变成了一碗意大利面。


如果你需要使用全局变量,请继续使用它们(只需要小心)。如果你要声明一个全局变量,那么初始化它通常是有意义的,就像任何其他变量一样。


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti .net / ~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



Initializing them or not isn''t really the issue. Global variables
themselves can cause problems. For example, if a function
communicates only with its caller, and only via its parameters and
return value, it''s relatively easy to analyze what it does. If it
also uses global variables, it can affect, and be affected by, any
other functions that use the same global variables, even in the
absence of an explicit call. The graph of dependencies among the
various functions in your program, which would otherwise have been a
simple tree structure, becomes a bowl of spaghetti.

If you need to use global variables, go ahead and use them (just be
careful). And if you''re going to declare a global variable, it
usually makes sense to initialize it, just like any other variable.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


2005年6月6日星期一15:36:05 +0200,Paul Mesken写道:
On Mon, 06 Jun 2005 15:36:05 +0200, Paul Mesken wrote:
2005年6月6日06:14:04 -0700,振动 < 6 ***** @ gmail.com>写道:
On 6 Jun 2005 06:14:04 -0700, "vib" <vi*****@gmail.com> wrote:
你好,

偶然的机会,我开始了解到在程序外部初始化全局变量是不好的编程习惯。这样糟糕吗?为了完成这个,我不得不用const声明它们,但此后它们不再是变量。只是看不出这种实践的好处。欢迎提出任何意见或建议。
全局常量变量确实很少使用。
Hi there,

By chance, I came to learn that it is bad programming practice to
initialize global variables at outside of programs. Is it that bad? In
order to fullfil this, I had to declare them with const, but thereafter
they are no longer variables. Just couldn''t see the benefits of this
practice. Any comments or advices are welcome.
Global const variables are, indeed, of very little use.




他们可以为程序的其余部分提供信息。

如果你需要一些可以在程序中使用的常量值,那么你应该使用#define或enum。



They can provide information for the rest of the program.
If you need
some constant value that can be used in your program then you should
use a #define or enum.



这些在很多情况下都比较简单,但除此之外我没有看到优先使用它们的强烈理由。



These are simpler in a lot of cases, but other than that I don''t see a
strong reason to use them in preference.

书籍代码完整 (McConnel,现在已经是第2版)列出了
The book "Code Complete" (McConnel, now in its 2nd edition) lists


避免全局变量的原因以及它们可以使用的一些原因。一般情况下,如果有替代方案,应该避免使用它们。


reasons to avoid global variables and some reasons in which they can
be handy. In general, they should be avoided if there is an
alternative.




如果全局变量是常量,则全局变量的问题会大大减少。

很多人会说宏是非常讨厌的,一个非常生硬的工具

席卷范围,名称空间等问题。仅仅因为可能有一个

替代品全局变量并不意味着它是一个更好的变量。


合理使用全局变量可以写一次或在一个地方,

随处阅读,例如用于配置数据。遗憾的是,C没有提供一个简单的方法来实施这个。


劳伦斯



The problems with global variables are much reduced if they are const.
many people would say that macros are pretty nasty, a very blunt tool
sweeping over issues of scope, namespace etc. Just because there may be an
alternative to global variables doesn''t mean it is a better one.

A reasonable use of global variables can be a write once or in one place,
read anywhere approach e.g. for config data. It is a pity that C doesn''t
provide an easy way to enforce this.

Lawrence

这篇关于在外部初始化全局变量是不是很糟糕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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