官术网_书友最值得收藏!

Pros and cons of global variables

To reiterate, a global variable is a variable that is declared outside of a function or class. Doing this makes our variable accessible in every function, hence us calling it global. When being taught programming in school, we were often told that global variables are a bad thing or at least, that modifying global variables in a function is considered to be poor programming practice.

There are numerous reasons why using global variables is a bad idea:

  • Source code is the easiest to understand when the scope of the elements used is limited. Adding in global variables that can be read or modified anywhere in the program makes it much harder to keep track of where things are being done, as well as making it harder to comprehend when bringing on new developers.
  • Since a global variable can be modified anywhere, we lose any control over being able to confirm that the data contained in the variable is valid. For instance, you may only want to support up to a certain number, but as a global variable this is impossible to stop. Generally, we advise using getter/setter functions instead for this reason.
  • Using global variables tightens how coupled our programs are, making it difficult to reuse aspects of our projects as we need to grab from a lot of different places to make things work. Grouping things that are connected to each other tends to improve projects.
  • When working with the linker, if your global variable names are common, you'll often have issues when compiling your project. Thankfully, you'll get an error and have to fix the issue in this case. Unfortunately, you may also have an issue where you are trying to use a locally scoped variable in a project but  end up selecting the global version due to mistyping the name or relying too heavily on intelligence and selecting the first thing you see, which I see students doing on multiple occasions.
  • As the size of projects grow, it becomes much harder to do maintenance and/or make changes to/on global variables, as you may need to modify many parts of your code to have it adjust correctly.

This isn't to say that global access is entirely bad. There are some reasons why one would consider using it in their projects:

  • Not knowing what a local variable is
  • Not understanding how to create classes
  • Wanting to save keystrokes
  • Not wanting to pass around variables all the time to functions
  • Not knowing where to declare a variable, so making it global means anyone can get it
  • To simplify our project for components that need to be accessible anywhere within the project

Aside from the last point, those issues are really bad reasons for wanting to use global variables, as they may save you some time up front, but as your projects get larger and larger it'll be a lot more difficult to read your code. In addition, once you make something global it's going to be a lot more difficult to convert it to not be global down the road. Think that, instead of using global variables, you could instead pass parameters to different functions as needed, making it easier to understand what each function does and what it needs to work with to facilitate its functionality.

That's not to say that there isn't any time when using a global variable is a reasonable or even a good idea. When global variables represent components that truly need to be available throughout your project, the use of global variables simplifies the code of your project, which is similar to what we are aiming to accomplish.

Norm Matloff also has an article explaining times that he feels like global variables are necessary when writing code. If you want to hear an alternative take, check out http://heather.cs.ucdavis.edu/~matloff/globals.html.

Basically, always limit your variables to the minimal scope needed for the project and not any more. This especially comes to mind when you only ever need one of something, but plan to use that one object with many different things. That's the general idea of the Singleton design pattern and is the reason why it's important that we understand the general usage before moving onwards.

主站蜘蛛池模板: 德兴市| 竹北市| 始兴县| 成安县| 襄城县| 修文县| 石城县| 红河县| 将乐县| 荥阳市| 留坝县| 青海省| 台南县| 舒兰市| 广汉市| 长沙市| 永春县| 顺平县| 滦南县| 梁河县| 徐汇区| 临安市| 新闻| 赞皇县| 静宁县| 平塘县| 汝阳县| 斗六市| 合肥市| 丰宁| 扶沟县| 安平县| 保靖县| 子洲县| 都安| 宁津县| 耿马| 鄂州市| 富川| 锦州市| 郴州市|