Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

ALWAYS Avoid Global Variables?

I'll assume this is a question. 

A good rule when you encounter these "universal rules" in programming, I think, is this:

  • do your absolute best to follow them
    • unless you understand why people say that, and why they could be wrong

People often say "don't use switch statements" or (especially this one) "don't use goto". However, both have their legitimate uses, and are the best solution in some cases. If you can say why that is the case, then feel free to use them. If you can't, then probably don't. 

 

 

There are definitely times when writing js code that I have used globals, and definitely times I technically could have, but avoided doing so in favour of a better solution. But, I'm also a CS graduate, and have been working professionally in software development for some time, so I sincerely hope I'd be able to make those determinations 😄

I will also say this: those rules mostly apply to writing "real" code, when making applications you plan to actually run and do something productive. If you're just playing around trying to learn, don't stress about it. It's much better to keep progressing in your learning experience and commit a couple faux-pas than to run into one, get stumped, and quit.

Main Rig: R9 5950X @ PBO, RTX 3090, 64 GB DDR4 3666, InWin 101, Full Hardline Watercooling

Server: R7 1700X @ 4.0 GHz, GTX 1080 Ti, 32GB DDR4 3000, Cooler Master NR200P, Full Soft Watercooling

LAN Rig: R5 3600X @ PBO, RTX 2070, 32 GB DDR4 3200, Dan Case A4-SFV V4, 120mm AIO for the CPU

HTPC: i7-7700K @ 4.6 GHz, GTX 1050 Ti, 16 GB DDR4 3200, AliExpress K39, IS-47K Cooler

Router: R3 2200G @ stock, 4GB DDR4 2400, what are cases, stock cooler
 

I don't have a problem...

Link to post
Share on other sites

I agree with @tarfeef101   This is a good case of "Learn the rules before you break them"

 

Speaking from experience though I will say this is a very, very good rule to follow.  Because global variables can be modified by any other code, this can lead to some extremely infuriating intermittent errors that can be a nightmare to solve.  So it is best to just go ahead and get in the habit of not using them, until you come across a situation where you need them.

Link to post
Share on other sites

I don't know why you are on w3schools if you are not going to read it.

Quote

 

Avoid Global Variables

Minimize the use of global variables.

 

Dude, the answer is literally below the title...

Link to post
Share on other sites

The reason for this: we reuse names quite frequently.

 

You will definitely get into a name collision, either writing something on top of another or reading something unexpected (which is the greatest source of bugs in programs as absolutely nobody check all inputs all the time).

 

You can pray for the fuzzying gods to find the bug you made, but prevention is far more effective.

Link to post
Share on other sites

W3schools is pretty bad and out of date but regardless the point is rather valid as others have demonstrated. I will say I'm working a Go package that has a few global variables but they're only used within the package and can't be accessed from outside of it if you were to import the package.

[Out-of-date] Want to learn how to make your own custom Windows 10 image?

 

Desktop: AMD R9 3900X | ASUS ROG Strix X570-F | Radeon RX 5700 XT | EVGA GTX 1080 SC | 32GB Trident Z Neo 3600MHz | 1TB 970 EVO | 256GB 840 EVO | 960GB Corsair Force LE | EVGA G2 850W | Phanteks P400S

Laptop: Intel M-5Y10c | Intel HD Graphics | 8GB RAM | 250GB Micron SSD | Asus UX305FA

Server 01: Intel Xeon D 1541 | ASRock Rack D1541D4I-2L2T | 32GB Hynix ECC DDR4 | 4x8TB Western Digital HDDs | 32TB Raw 16TB Usable

Server 02: Intel i7 7700K | Gigabye Z170N Gaming5 | 16GB Trident Z 3200MHz

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×