全球变量是邪恶的? [英] Are global variable evil?

查看:60
本文介绍了全球变量是邪恶的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过代码审查后,一位同事坚持认为全球性非常危险。除了绩效惩罚,b $ b难以维持和危险之外,他还没有给出任何可靠的理由。我认为我正在使用它们

适合在课堂上讨论。一个典型的例子:这个类启动

TCP会话,不断向服务器app发送命令并获取消息和

代码返回,消息和代码存储在全局变量中保留

方法调用之间。


他坚持认为我不应该有任何全局变量,而是传递它们

来自一个方法到下一个。我觉得这很愚蠢。全局

变量有什么坏处?


谢谢

解决方案

< blockquote>基本规则是封装,或者说隔离,即将

变量或代码隔离在应该看到的位置,并且不要让其他人看到

考虑一件事:你知道如何使用它,其他人不要''要知道,如果它可以被其他同事修改,那么它很危险,他可能会被错误操作修改为b
,并将你的变量改为一个状态是无稽之谈

或无效。


但是,应该有平衡。举个例子,如果它是
GlobalLogger,将它作为参数传递给每个方法都是愚蠢的。


关于你的问题,如果它'只有你自己使用,你应该把它封装起来,如果没有,你应该认为整个过程已经决定将它暴露为全局或封装公共GetXX和

公共/私人SetXX。


全局变量的问题是它的值可以是

可以通过_any_代码,_any_ way,_anywhere_

在程序中更改。


因此,当出现疑似错误的错误时>
与该变量相关,要搜索的代码区域

是:整个程序。


另外,每一段代码改变全局变量

必须重新理解发生了什么。


因此,良好的做法是将变量存储在类中,
也许是单身人士,makin g它可以通过属性

或方法获得。与此相关的错误相对更容易找到并修复,因为搜索的面积较小。

-

Grace +和平,

Peter N Roth

工程对象国际
http://engineeringobjects.com

Matrix.NET的主页

" LP" < lp@nospam.com>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP14.phx.gbl ...

之后代码审查一位同事坚持认为全球性非常危险。
除了表现,难以维持和危险之外,他并没有给出任何可靠的理由。 。我认为我正在使用它们在课堂上适当的。一个典型的例子:这个类启动TCP会话,不断发送命令到服务器应用程序并获取消息和代码,消息和代码存储在全局变量中,以便在方法之间保存它们电话。

他坚持认为我不应该有任何全局变量,而是将它们从一种方法传递给下一种方法。我觉得这很愚蠢。全局
变量有什么坏处?

谢谢



目前还不清楚你的意思是什么全局变量。如果你的意思是你在你描述的情况下创建一个类级别的静态变量,那么

肯定是错误的。如果你的意思是你将类级别变量设为公共到其他课程的
,那你也错了。整个软件行业已经多年来表示这已经好了多年。如果你想知道原因,那就再问一遍,我会给你一些理由给你带来



让我们假设你的意思是什么其他。从上下文来看,它看起来像是你的全局变量。对于一个班级来说,这是一个非静态的领域。


我倾向于同意你的同事,但我不是100%教条。在

一般情况下,我只创建一个类字段。如果需要在对象实例的不同公共方法调用之间持续存在值。例如。我们的

类存储一个整数,我们有公共方法来读写

整数,所以我们创建一个全局类。领域。所以,一般来说,我没有创建一个字段

来保存一个值,该值可以作为方法之间的参数传递,但是不需要存储

。但是,我没有对此做出绝对的规则。

有时候,如果他们在方法的实现中是一个很深的层次,那么
和一个值是在整个过程中使用,然后我会把它变成一个字段,但我把

a明确评论该字段说明为什么它是一个字段,并且它是'b $ b限制。

" LP"写道:

在代码审查后,一位同事坚持认为全球性非常危险。除了绩效惩罚,难以维持和危险之外,他并没有真正给出任何可靠的理由。我认为我正在使用它们
适合课堂。一个典型的例子:这个类启动TCP会话,不断向服务器app发送命令并获取消息和
代码,消息和代码存储在全局变量中以保存它们之间的方法电话。

他坚持认为我不应该有任何全局变量,而是将它们从一种方法传递给下一种方法。我觉得这很愚蠢。全局
变量有什么坏处?

谢谢



After a code review one coworker insisted that global are very dangerous. He
didn''t really give any solid reasons other than, "performance penalties",
"hard to maintain", and "dangerous". I think that I am using them
appropriate in class in question. One typical example: This class initiates
TCP session, keeps sending commands to the server app and gets messages and
codes back, message and code are stored in global variables to be preserved
them between method calls.

He insists I should not have any global variables, but instead passes them
from one method to the next. I think it''s dumb. What''s so bad about global
variables?

Thanks

解决方案

The basic rule is "encapsulate", or said "isolate", that is isolate the
variable or code at where it should be seen, and don''t let others see
it or touch(set) it if it''s none of his business.

consider one thing: you know how to use it, others don''t know, if it
can be modified by other coworkers, it''s dangerous, he may modified by
mis-operation, and change your variable to a status which is nonsense
or invalid.

But, there should be a ballance. For an example, if it''s a
GlobalLogger, pass it as a param to every method is stupid.

As to your problem, if it''s only used by yourself you should
encapsulate it, if not, you should think the whole process over to
decide to expose it as global or encapsulate with public GetXX and
public/private SetXX.


The problem with a global variable is that its value can
be changed by _any_ code, in _any_ way, _anywhere_
in a program.

Therefore, when a bug arises that is suspected to be
related to that variable, the area of code to be searched
is: the entire program.

In addition, each piece of code that changes the global variable
must be restudied to understand what is happening.

Hence, good practice is to stash the variable in a class,
perhaps a singleton class, making it available via properties
or methods. Bugs related thereto are ''relatively'' easier
to find and fix because the acreage to search is smaller.
--
Grace + Peace,
Peter N Roth
Engineering Objects International
http://engineeringobjects.com
Home of Matrix.NET
"LP" <lp@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...

After a code review one coworker insisted that global are very dangerous.
He didn''t really give any solid reasons other than, "performance
penalties", "hard to maintain", and "dangerous". I think that I am using
them appropriate in class in question. One typical example: This class
initiates TCP session, keeps sending commands to the server app and gets
messages and codes back, message and code are stored in global variables
to be preserved them between method calls.

He insists I should not have any global variables, but instead passes them
from one method to the next. I think it''s dumb. What''s so bad about global
variables?

Thanks



It is not clear what you mean by a "global variable". If you mean that you
create a class level static variable in the situation you describe, then are
definitely wrong. If you mean that you make class level variables "public" to
other classes, then you are also wrong. The whole software industry has said
this for years now. If you want to know why, then ask again, and I''ll give
you some reasons.

Let''s assume that you mean something else. From the context, it looks like
your "global variable" is a non static field for a class.

I tend to agree with your co-worker, but I am not 100% dogmatic about it. In
general, I only create a class "field" if there is a need for the value to
persist between different public method calls to an object instance. eg. Our
class stores an integer, and we have public methods to read and write that
integer, so we make a "global" field. So, in general, I don''t create a field
to save a value which can be passed as a parameter between methods, but is
not needed to be stored. However, I don''t make an absolute rule about this.
Sometimes, if their is a deep heirarchy in the implementation of a method,
and a value which is used throughout, then I will make it a field, but I put
a clear comment on the field stating why it is a field, and it''s
limitiations.
"LP" wrote:

After a code review one coworker insisted that global are very dangerous. He
didn''t really give any solid reasons other than, "performance penalties",
"hard to maintain", and "dangerous". I think that I am using them
appropriate in class in question. One typical example: This class initiates
TCP session, keeps sending commands to the server app and gets messages and
codes back, message and code are stored in global variables to be preserved
them between method calls.

He insists I should not have any global variables, but instead passes them
from one method to the next. I think it''s dumb. What''s so bad about global
variables?

Thanks



这篇关于全球变量是邪恶的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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