也许是单身人士,makin g它可以通过属性 或方法获得。与此相关的错误相对更容易找到并修复,因为搜索的面积较小。 - Grace +和平, Peter N Roth 工程对象国际 http://engineeringobjects.com Matrix.NET的主页 " LP" < [email protected]>在消息中写道 新闻:%2 **************** @ TK2MSFTNGP14.phx.gbl ... 之后代码审查一位同事坚持认为全球性非常危险。除了表现,难以维持和危险之外,他并没有给出任何可靠的理由。 。我认为我正在使用它们在课堂上适当的。一个典型的例子:这个类启动TCP会话,不断发送命令到服务器应用程序并获取消息和代码,消息和代码存储在全局变量中,以便在方法之间保存它们电话。 他坚持认为我不应该有任何全局变量,而是将它们从一种方法传递给下一种方法。我觉得这很愚蠢。全局变量有什么坏处? 谢谢 目前还不清楚你的意思是什么全局变量。如果你的意思是你在你描述的情况下创建一个类级别的静态变量,那么 肯定是错误的。如果你的意思是你将类级别变量设为公共到其他课程的,那你也错了。整个软件行业已经多年来表示这已经好了多年。如果你想知道原因,那就再问一遍,我会给你一些理由给你带来。 让我们假设你的意思是什么其他。从上下文来看,它看起来像是你的全局变量。对于一个班级来说,这是一个非静态的领域。 我倾向于同意你的同事,但我不是100%教条。在 一般情况下,我只创建一个类字段。如果需要在对象实例的不同公共方法调用之间持续存在值。例如。我们的 类存储一个整数,我们有公共方法来读写 整数,所以我们创建一个全局类。领域。所以,一般来说,我没有创建一个字段 来保存一个值,该值可以作为方法之间的参数传递,但是不需要存储 。但是,我没有对此做出绝对的规则。 有时候,如果他们在方法的实现中是一个很深的层次,那么和一个值是在整个过程中使用,然后我会把它变成一个字段,但我把 a明确评论该字段说明为什么它是一个字段,并且它是'b $ b限制。 " LP"写道: 在代码审查后,一位同事坚持认为全球性非常危险。除了绩效惩罚,难以维持和危险之外,他并没有真正给出任何可靠的理由。我认为我正在使用它们适合课堂。一个典型的例子:这个类启动TCP会话,不断向服务器app发送命令并获取消息和代码,消息和代码存储在全局变量中以保存它们之间的方法电话。 他坚持认为我不应该有任何全局变量,而是将它们从一种方法传递给下一种方法。我觉得这很愚蠢。全局变量有什么坏处? 谢谢 After a code review one coworker insisted that global are very dangerous. Hedidn''t really give any solid reasons other than, "performance penalties","hard to maintain", and "dangerous". I think that I am using themappropriate in class in question. One typical example: This class initiatesTCP session, keeps sending commands to the server app and gets messages andcodes back, message and code are stored in global variables to be preservedthem between method calls.He insists I should not have any global variables, but instead passes themfrom one method to the next. I think it''s dumb. What''s so bad about globalvariables?Thanks 解决方案 The basic rule is "encapsulate", or said "isolate", that is isolate thevariable or code at where it should be seen, and don''t let others seeit 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 itcan be modified by other coworkers, it''s dangerous, he may modified bymis-operation, and change your variable to a status which is nonsenseor invalid.But, there should be a ballance. For an example, if it''s aGlobalLogger, pass it as a param to every method is stupid.As to your problem, if it''s only used by yourself you shouldencapsulate it, if not, you should think the whole process over todecide to expose it as global or encapsulate with public GetXX andpublic/private SetXX. The problem with a global variable is that its value canbe changed by _any_ code, in _any_ way, _anywhere_in a program.Therefore, when a bug arises that is suspected to berelated to that variable, the area of code to be searchedis: the entire program.In addition, each piece of code that changes the global variablemust 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 propertiesor methods. Bugs related thereto are ''relatively'' easierto find and fix because the acreage to search is smaller.--Grace + Peace,Peter N RothEngineering Objects International http://engineeringobjects.comHome of Matrix.NET"LP" <[email protected]> wrote in messagenews:%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? ThanksIt is not clear what you mean by a "global variable". If you mean that youcreate a class level static variable in the situation you describe, then aredefinitely wrong. If you mean that you make class level variables "public" toother classes, then you are also wrong. The whole software industry has saidthis for years now. If you want to know why, then ask again, and I''ll giveyou some reasons.Let''s assume that you mean something else. From the context, it looks likeyour "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. Ingeneral, I only create a class "field" if there is a need for the value topersist between different public method calls to an object instance. eg. Ourclass stores an integer, and we have public methods to read and write thatinteger, so we make a "global" field. So, in general, I don''t create a fieldto save a value which can be passed as a parameter between methods, but isnot 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 puta clear comment on the field stating why it is a field, and it''slimitiations."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 这篇关于全球变量是邪恶的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-21 20:33