Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Does not Developers get confused if there are too many global variables?

Does not Developers get confused if there are too many global variables?

Scheduled Pinned Locked Moved The Lounge
architecturequestion
67 Posts 29 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    This may be a stupid question, but that is the way I learn: Is there an "official" guide to the proper use of global variables, like a Microsoft guidelines document or something?

    Cornelius Henning ---------------------------------------------- "There is no reason for any individual to have a computer in their home." -- Ken Olson, President of DEC, World Future Society Convention, 1977

    P Offline
    P Offline
    Pablo Aliskevicius
    wrote on last edited by
    #34

    Here it is:

    1. Don't use global variables.
    2. If you thing you have found a reason to use a global variable, you're wrong. Just don't.

    Of course, synchronized singletons and read-only monostates are not 'real' global variables. Also, if you're writing C and you have a variable that allows access to a hardware device, it may be forgiven. JM2B,

    Pablo. "Accident: An inevitable occurrence due to the action of immutable natural laws." (Ambrose Bierce, circa 1899).

    1 Reply Last reply
    0
    • B BobJanova

      Anyone who declares anything dynamic without a damn good reason should get a slap round the head. It's loose, lazy, unclear and slow. If you know the name of methods that you want to call on an object (which you need to to make dynamic more than just object), you know enough to have it implement an interface and dispatch calls through that.

      M Offline
      M Offline
      Mohammed Hameed
      wrote on last edited by
      #35

      Superb...

      Understand SOLID! Believe SOLID! Try SOLID; your Code becomes Rock SOLID!!!

      1 Reply Last reply
      0
      • M Mohammed Hameed

        Life of Brian.

        Understand SOLID! Believe SOLID! Try SOLID; your Code becomes Rock SOLID!!!

        F Offline
        F Offline
        Forogar
        wrote on last edited by
        #36

        Ding! 10 points to Mr. Hameed!

        - I would love to change the world, but they won’t give me the source code.

        M 1 Reply Last reply
        0
        • V Valery Possoz

          I once worked on a project with over 3000 global variables. The "team leader" did not even know what half of them were suppose to be for... He just use to add new ones!! I let you guess the quality of the project! So yes they get confused... :(

          M Offline
          M Offline
          Mohammed Hameed
          wrote on last edited by
          #37

          Thanks for the real time scenario.

          Understand SOLID! Believe SOLID! Try SOLID; your Code becomes Rock SOLID!!!

          1 Reply Last reply
          0
          • F Forogar

            Ding! 10 points to Mr. Hameed!

            - I would love to change the world, but they won’t give me the source code.

            M Offline
            M Offline
            Mohammed Hameed
            wrote on last edited by
            #38

            Thanks :)

            Understand SOLID! Believe SOLID! Try SOLID; your Code becomes Rock SOLID!!!

            1 Reply Last reply
            0
            • J Joe Woodbury

              It's less confusing that where the developer uses classes at an absurdly granular level and/or buys into the "a method should do only one thing" nonsense. Then there's the one person who uses "var" for every definition in their C# code. The solution is to write aesthetically pleasing code will with meaningful names. In the C++ world, using static properly is a big help. (One thing that cracks me up are devs who criticize global variables and then create a singleton class full of variables. Or, worse, dozens of singleton classes chock full of variables. Speaking of singletons, I recently worked on some code where the original developer had create a factory class that produced exactly one and only one instance. Yes, it was "object" oriented, but the code would have been cleaner and made a whole lot more sense had they just written it in C.)

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #39

              Not that I am particularly defending the use of var, but I quite like it in the case of

              var foo = new SomeReallyQuiteSensibleButLongName();

              especially if there are a few of them (cuz they line up and look nice) You can see what the type is easily, so no harm done. When it is

              var foo = TheResultOfSomeFunctionWhoseNAmeDoesntReflectItsReturnType();

              it annoys me as I have to use intellisense to ind out what type it is. Especially annoying with something like

              var selectedCustomer = GetCustomerFromList();

              which, at first glance, I would guess at selectedCustomer being some kind of Customer object. Imagine my surprise when I find out it is a boolean! Yep - the function returns a boolean that determines whether the customer was selected from the list. Now this comes down to good naming - and I have yet to meet a developer that names consistently well (myself included)

              MVVM # - I did it My Way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

              M 1 Reply Last reply
              0
              • C Chris Maunder

                I will put my hand up and say loudly and with some shame: Our Javascript sucks

                cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

                R Offline
                R Offline
                Roger Wright
                wrote on last edited by
                #40

                It's a lot nicer than the old VBS, though. :-D

                Will Rogers never met me.

                1 Reply Last reply
                0
                • M Mohammed Hameed

                  It has been noticed that some developers heavily use Global variables, may be without any discomfort. Is it like they get accustomed to it and hence use it? Dont they get confused by over usage?

                  Understand SOLID! Believe SOLID! Try SOLID; your Code becomes Rock SOLID!!!

                  R Offline
                  R Offline
                  Roger Wright
                  wrote on last edited by
                  #41

                  Back in the day, global variables were the only kind we had, and sometimes we had to re-use them. ;P Some of us are still coding... :-O

                  Will Rogers never met me.

                  1 Reply Last reply
                  0
                  • M Mohammed Hameed

                    It has been noticed that some developers heavily use Global variables, may be without any discomfort. Is it like they get accustomed to it and hence use it? Dont they get confused by over usage?

                    Understand SOLID! Believe SOLID! Try SOLID; your Code becomes Rock SOLID!!!

                    realJSOPR Online
                    realJSOPR Online
                    realJSOP
                    wrote on last edited by
                    #42

                    Define "too many". I use whatever gets the job done. And what does "global" mean anymore? In .Net, everything's in a class. I often employ a static class that I call "Globals". Most of the contents are methods, but a lot of times, there's a collection or two in it.

                    ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                    -----
                    You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                    -----
                    "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

                    N 1 Reply Last reply
                    0
                    • L Lost User

                      This may be a stupid question, but that is the way I learn: Is there an "official" guide to the proper use of global variables, like a Microsoft guidelines document or something?

                      Cornelius Henning ---------------------------------------------- "There is no reason for any individual to have a computer in their home." -- Ken Olson, President of DEC, World Future Society Convention, 1977

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #43

                      OK, I was probably confusing "Global" with "static public". I just assumed they were synonyms. It's been some years since using anything but C#. I do from time to time use static public when a variable needs to be accessed from multiple classes. I assume the jury is less likely to convict me for this crime? :confused: I am a self-taught programmer working on my own - no one else to ask. This is why this forum is of crucial importance to me.

                      M R 2 Replies Last reply
                      0
                      • realJSOPR realJSOP

                        Define "too many". I use whatever gets the job done. And what does "global" mean anymore? In .Net, everything's in a class. I often employ a static class that I call "Globals". Most of the contents are methods, but a lot of times, there's a collection or two in it.

                        ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                        -----
                        You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                        -----
                        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

                        N Offline
                        N Offline
                        Nish Nishant
                        wrote on last edited by
                        #44

                        John Simmons / outlaw programmer wrote:

                        And what does "global" mean anymore? In .Net, everything's in a class.

                        Some people still work on C/C++ :-)

                        Regards, Nish


                        Blog: voidnish.wordpress.com The life of a Malayalee American - by Nish

                        An article I recently wrote for an event souvenir

                        M 1 Reply Last reply
                        0
                        • N Nish Nishant

                          John Simmons / outlaw programmer wrote:

                          And what does "global" mean anymore? In .Net, everything's in a class.

                          Some people still work on C/C++ :-)

                          Regards, Nish


                          Blog: voidnish.wordpress.com The life of a Malayalee American - by Nish

                          An article I recently wrote for an event souvenir

                          M Offline
                          M Offline
                          Mohammed Hameed
                          wrote on last edited by
                          #45

                          Thanks Nish for mentioning about C/C++ also.

                          Understand SOLID! Believe SOLID! Try SOLID; your Code becomes Rock SOLID!!!

                          1 Reply Last reply
                          0
                          • L Lost User

                            OK, I was probably confusing "Global" with "static public". I just assumed they were synonyms. It's been some years since using anything but C#. I do from time to time use static public when a variable needs to be accessed from multiple classes. I assume the jury is less likely to convict me for this crime? :confused: I am a self-taught programmer working on my own - no one else to ask. This is why this forum is of crucial importance to me.

                            M Offline
                            M Offline
                            Mycroft Holmes
                            wrote on last edited by
                            #46

                            We also use a minimum of public statics but for fucks sake stick them all in the one place. Public statics scattered throughout the app is a sure sign on incompetence.

                            Never underestimate the power of human stupidity RAH

                            L 2 Replies Last reply
                            0
                            • M Mycroft Holmes

                              We also use a minimum of public statics but for fucks sake stick them all in the one place. Public statics scattered throughout the app is a sure sign on incompetence.

                              Never underestimate the power of human stupidity RAH

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #47

                              Yeah, I usually create a small static class to hold all the static vars.

                              1 Reply Last reply
                              0
                              • M Mycroft Holmes

                                We also use a minimum of public statics but for fucks sake stick them all in the one place. Public statics scattered throughout the app is a sure sign on incompetence.

                                Never underestimate the power of human stupidity RAH

                                L Offline
                                L Offline
                                Lost User
                                wrote on last edited by
                                #48

                                Yeah, I usually put them all in a small, dedicated static class. Seems to work well for me.

                                1 Reply Last reply
                                0
                                • M Mohammed Hameed

                                  It has been noticed that some developers heavily use Global variables, may be without any discomfort. Is it like they get accustomed to it and hence use it? Dont they get confused by over usage?

                                  Understand SOLID! Believe SOLID! Try SOLID; your Code becomes Rock SOLID!!!

                                  R Offline
                                  R Offline
                                  R Erasmus
                                  wrote on last edited by
                                  #49

                                  Agree, they should read up on data coupling a bit.

                                  "Program testing can be used to show the presence of bugs, but never to show their absence." << please vote!! >>

                                  1 Reply Last reply
                                  0
                                  • OriginalGriffO OriginalGriff

                                    Joe Woodbury wrote:

                                    Then there's the one person who uses "var" for every definition in their C# code.

                                    I'm just glad var is shorter and easier to type than dynamic.... :sigh: I'm betting that is going to bite us in a couple of years time.

                                    The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

                                    S Offline
                                    S Offline
                                    Stefan_Lang
                                    wrote on last edited by
                                    #50

                                    OriginalGriff wrote:

                                    I'm just glad var is shorter and easier to type than dynamic

                                    If differences in typing speed is your most pressing problem, get an editor where it isn't (hint: language sensitive autocompletion helps).

                                    1 Reply Last reply
                                    0
                                    • M Mohammed Hameed

                                      It has been noticed that some developers heavily use Global variables, may be without any discomfort. Is it like they get accustomed to it and hence use it? Dont they get confused by over usage?

                                      Understand SOLID! Believe SOLID! Try SOLID; your Code becomes Rock SOLID!!!

                                      S Offline
                                      S Offline
                                      Stefan_Lang
                                      wrote on last edited by
                                      #51

                                      I've found that some people prefer defining globals, or defining variables within too wide a scope just for the sake of not having to pass them as a parameter to a function, because that would require typing additional parameters. Sadly, even language-sensitive editors cannot help a lot with this. Still, additional typing should never affect your coding design decisions! For example I've seen classes with lots of private member variables that are really only required to hold temporary flags and states of operations, rather than indicate the actual state of the object. I consider that almost as bad as using globals. I know I like declaring globals or class members instead of local vars when prototyping new code when I'm not yet sure about the final design. But, even if I often don't clean up my code to the degree I should, at the very least I'll move my variables to the proper scope. It's the minimum I can do to keep my interfaces clean.

                                      1 Reply Last reply
                                      0
                                      • L Lost User

                                        OK, I was probably confusing "Global" with "static public". I just assumed they were synonyms. It's been some years since using anything but C#. I do from time to time use static public when a variable needs to be accessed from multiple classes. I assume the jury is less likely to convict me for this crime? :confused: I am a self-taught programmer working on my own - no one else to ask. This is why this forum is of crucial importance to me.

                                        R Offline
                                        R Offline
                                        Rob Grainger
                                        wrote on last edited by
                                        #52

                                        Actually, public statics are nearly as bad. The crucial factor here is that they can introduce problems in the presence of concurrency - if two tasks/threads/... attempt to update the same global/static, the state of the system becomes indeterminate. Singletons are frequently referred to as an anti-pattern for exactly this reason. All such rules are made to be broken, however, and I sometimes have to use public statics - to avoid passing some variable references absolutely everywhere (because most languages don't have good support for lexical scoping). But I do think about each and every such useful very carefully and prefer to find alternatives.

                                        "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

                                        G 1 Reply Last reply
                                        0
                                        • V Valery Possoz

                                          I once worked on a project with over 3000 global variables. The "team leader" did not even know what half of them were suppose to be for... He just use to add new ones!! I let you guess the quality of the project! So yes they get confused... :(

                                          R Offline
                                          R Offline
                                          Rob Grainger
                                          wrote on last edited by
                                          #53

                                          That's one big problem, I've seen many projects like this (especially in VB6). The list of globals has a tendency to grow, never to shrink. Cleaning (or even understanding) such programs is an exercise in frustration.

                                          "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups