简体   繁体   English

使用全局变量或将方法参数传递给方法变量的最佳实践?

[英]Best Practices Using Global Variables or Passing Method Arguments to Method Variables?

In the past few months I have been focusing on cleaning up my code for readability purposes. 在过去的几个月中,我一直致力于清理代码以提高可读性。 As my app gets larger, it's critical that I have a handle on my variables, classes, methods, etc. 随着我的应用程序变大,对变量,类,方法等进行处理至关重要。

This being said, I often question my decision to create a global variable as opposed to pass a method argument and create a method variable. 话虽如此,我经常质疑我创建全局变量而不是传递方法参数和创建方法变量的决定。 I find creating local method variables cleans up the remainder of my code by "hiding the overhead", however, I find there is a trade off having to trace and understand the flow of passing method arguments. 我发现创建本地方法变量可以通过“隐藏开销”来清理代码的其余部分,但是,我发现必须跟踪和理解传递方法参数的流程,这是一个折衷方案。

Ultimately I believe the best practice comes down to creating minimal overhead by initializing variables at as low a level as possible, essentially: 最终,我相信最佳实践归结为通过以尽可能低的级别初始化变量来创建最小开销的方法,本质上是:

  • One should only use global variables where the variable is required in multiple methods. 一个人应该只使用全局变量,在多种方法中都需要该变量。
  • One should use a method argument and a method variable where the variable is only required in said method and hence is local to said method. 一个人应该使用一个方法参数和一个方法变量,其中该变量仅在所述方法中是必需的,因此对于所述方法是局部的。

Is that the simple logical way to approach global vs. method variables? 这是处理全局变量与方法变量的简单逻辑方法吗? Any advice on this line of thought would be much appreciated. 对此思路的任何建议将不胜感激。 As my code begins to grow into hundreds of methods I need to take more care to create my variables in the most logical way. 随着我的代码开始发展成数百种方法,我需要更加小心以最合乎逻辑的方式创建变量。

Thank you for your advice! 感谢您的意见!

Using global variables is considered bad programming practice in most programming languages that I know. 在我所知道的大多数编程语言中,使用全局变量被认为是不良的编程习惯。 You should use params whenever possible. 您应该尽可能使用params。 If at all you need something like the singleton pattern. 如果有的话,您需要类似singleton模式的东西。

If you want to store some global thing you should extend Application class, it some kind of session in Android. 如果要存储一些全局的东西,则应该扩展Application类,这是Android中的某种会话。 In this class you can store everything global. 在此类中,您可以将所有内容存储在全局范围内。

And don't use singleton, it's a bad practice in Android, use Application class. 而且不要使用单例,这在Android中是一种不好的做法,请使用Application类。

This is not an easy question to answer in a few sentences. 用几个句子回答这个问题并不容易。 Use of global variables is a bad practice. 使用全局变量是一种不好的做法。 It leads to tightly coupled code and makes it very difficult to modify or enhance. 它导致紧密耦合的代码,并且使其很难修改或增强。 The need for large numbers of global variables is an indicator that you need to focus on your design. 对大量全局变量的需求表明您需要专注于设计。

You mention your code having hundreds of methods. 您提到了具有数百种方法的代码。 How many classes do you have? 你有几节课? If you have hundreds of methods in a single class, that one class has way too many responsibilities. 如果单个类中有数百种方法,那么该类的职责太多。 Your design should focus on having classes with clear responsibilities. 您的设计应重点放在具有明确职责的类上。 Each class should have only the data that it needs for handling those responsibilities. 每个类别应仅具有处理这些职责所需的数据。 This in turn will lead to a reduction in the number of global variables. 反过来,这将导致减少全局变量的数量。 If the data is clearly the responsibility of one class then it shouldn't be global and accessible to other classes. 如果数据显然是一类的责任,那么它就不应是全局的,其他类也不能访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM