About using global vars

This is a discussion on About using global vars within the C Programming forums, part of the General Programming Boards category; Originally Posted by esbo You can't prove global variables are bad. If you can do it, do it, untill then ...

  1. #46
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,176
    Quote Originally Posted by esbo
    You can't prove global variables are bad.
    If you can do it, do it, untill then stop making claims you cannot substanciate.
    The claims have been substantiated. You merely refuse to face the truth. "All you can give a lot of waffle basically."

    In a way, you have a strawman argument, since it is indeed

    Quote Originally Posted by esbo
    I have a program with over a 100 globals and about 20 locals, it works fine.
    Is it maintainable? Do you have a test suite? Are its components reusable with (almost) no modification?

    Quote Originally Posted by esbo
    It would have been much harder to write using al local variabes, although possible, why should I waste time doing that?
    Clearly, this means that you lack the skill to participate in the development of programs of significant size.

    Quote Originally Posted by esbo
    Or maybe say it is only 1200 lines long and that I would have probllems if it was a million lines long?
    The reason it is not a milion line long is because it is well written
    No, the reason it is not a million lines long is because it is trivial.
    Last edited by laserlight; 12-16-2008 at 12:58 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #47
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    In smaller projects, you shouldn't pass all-local variables. But in greater ones, you'll really mess things up. Look at namespaces. They're a great idea, but not used in C. Try organizing global structs.

  3. #48
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I think we need examples. Post some common examples like "here a lot use a global, but it could be avoided". Some users, I suspect many, don't have the experience in working for a big C project so all of this seems kind of undefined. I have only made 2 medium-small C projects (3-4K lines) and didn't find the need to use globals at all. I want to do something, I create a function that takes input and gives output. The data I pass all in a struct which I update and flags are again all in one struct that I pass (or could just have had one global variable, no big difference).

  4. #49
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by C_ntua View Post
    I think we need examples. Post some common examples like "here a lot use a global, but it could be avoided". Some users, I suspect many, don't have the experience in working for a big C project so all of this seems kind of undefined. I have only made 2 medium-small C projects (3-4K lines) and didn't find the need to use globals at all. I want to do something, I create a function that takes input and gives output. The data I pass all in a struct which I update and flags are again all in one struct that I pass (or could just have had one global variable, no big difference).
    All good points.

    I would also like to point out that if a function takes more than three or four parameters and you think "Oh, well, if I make X a global, I don't have to pass it around so much" then it's most likely a scenario of "your funciton is trying to do too much". Of course, a parameter to a function CAN be a struct (or pointer to struct), which allows a grouping of several fields together into one variable. If you find that a lot of functions take the same set of variables, then passing them as a struct is one possibility.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #50
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Quote Originally Posted by esbo View Post
    Because it is.
    You can't prove global variables are bad.
    If you can do it, do it, untill then stop making claims you cannot substanciate.
    All you can give a lot of waffle basically.
    If you think that the use of globals is too case specific to talk about it, you should say that right of the bat.

    I and most people here disagree, and think that there are meaningful statements that can be said about when you should use globals. Specifically, you should avoid using a global variables when the you can instead you a local variable passed as a parameter. Now this is a generalization, and there are perhalps borderline cases, where globals are preferred even though using a parameter is possible. Therefor an exhaustive proof is not called for, and the provided list of disadvantages (mostly from the other thread) is sufficent.

    I have a program with over a 100 globals and about 20 locals, it works fine.
    It would have been much harder to write using al local variabes, although possible, why should I waste time doing that?
    For reasons of modularity, expandability, and thread safety.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #51
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by laserlight View Post
    The claims have been substantiated. You merely refuse to face the truth. "All you can give a lot of waffle basically."

    In a way, you have a strawman argument, since it is indeed


    Is it maintainable? Do you have a test suite? Are its components reusable with (almost) no modification?
    It has been maintained, it started off basic and more and more functionality has been
    added to it. It has also been 'ported' to work on 3 other poker sites, which store the
    data in different ways, some put one history in a file, some several, some spread it over serveral folders, some use a mixture of the two, all use a different format for the history
    with some similarities. It would prove too complex to have one multipurpose program
    especially as I will add other sites of unknown formats.

    Obviously I could have done some thing better with hindsight but not that much,
    obviously you have to balance off forward planning with having a working version.

    Each new feaure is tested.
    It has been ran over a million poker hand histories, that is over a million files
    in some cases. (obviously that take a while!!). It incorporates a database of a summary
    of the previous results and loads it in real time and adds the next history to it.
    You can get a commercial version called Pokertracker($98), I bet my version is quicker than that!! Obviously mine is not all singing all dancing but then it did not envolve
    a million man hours!!


    Quote Originally Posted by laserlight View Post
    Clearly, this means that you lack the skill to participate in the development of programs of significant size.
    You have provided no jusification for that statement.
    If you mean projects which end up having to be scrapped then probably yes.
    I have worked on a project with a heavy empathis on variable scope which had to be
    rewritted from scratch for other reasons. So that was largely a waste of time.

    Quote Originally Posted by laserlight View Post
    No, the reason it is not a million lines long is because it is trivial.
    It's not trivial just written by someone who is an efficient worker and knows how to
    design and write a program which is actually useful.

  7. #52
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by C_ntua View Post
    I think we need examples. Post some common examples like "here a lot use a global, but it could be avoided". Some users, I suspect many, don't have the experience in working for a big C project so all of this seems kind of undefined. I have only made 2 medium-small C projects (3-4K lines) and didn't find the need to use globals at all. I want to do something, I create a function that takes input and gives output. The data I pass all in a struct which I update and flags are again all in one struct that I pass (or could just have had one global variable, no big difference).
    I don't think you pass a structure though do you?
    You pass a pointer to a structure, so the function can modify the data.

    Most of the data my program uses might at some time need to be accessed
    by almost any function so it seems sensible to make most stuff global except for
    some local variable in functions, loop counters etc...
    I don't know what functionality I might want to add to it, so I don't want to have to
    constantly have to change the data I have to pass to the functions, it would just
    create an oppertunnity to introduce bugs every time I modified it.

    Again it is hard to generalise about programs without a specific example.

    Programs which are over structured tend to take on a rigidity which makes
    them hard to modify quickly, for example whilst I am playing poker!!
    Yes I sometimes write code as I play, sometimes when I am multi-tabling
    which can be 'interesting' having to switch from program to poker quickly!!
    If I then make a mistake I am then without the asistance of my program
    and have to play without knowledge of the playing style of the players
    aat the table. However often such 'knowledge' can be overvalued and dangerous
    if the data indicates the player is a ''bluffer' but it turns out he has great hand!!

    Also most of the time you never see the players again so it is largly a waste of
    time gathering data but it does help sometimes when the the player pushes
    all his chips in and the data indicates he is as tight as a vice. You can guarantee
    he will usually hold AA KK or QQ, probably best to fold anyway
    Last edited by esbo; 12-16-2008 at 07:13 PM.

  8. #53
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    Quote Originally Posted by esbo View Post
    If you mean projects which end up having to be scrapped then probably yes.
    I have worked on a project with a heavy empathis on variable scope which had to be
    rewritted from scratch for other reasons. So that was largely a waste of time.

    (snip)

    It's not trivial just written by someone who is an efficient worker and knows how to
    design and write a program which is actually useful.
    Well don't talk as though local variables are the reason the projects that you work on tend to get scrapped. It is far more likely that you FUBAR'd them, if you had a significant role in the development of said software.

    Also, Texas Holdem can be implemented trivially: you can write most card games for schoolwork if the I/O is lame enough, and you use rand as the PRNG. That's not a very convincing example on its own. Unless you want us to just give you the benefit of the doubt of course.

    Perhaps that's for the best. You often worry about us stealing the code, maybe we'll steal the specs too!

    Quote Originally Posted by esbo View Post
    I don't think you pass a structure though do you?
    You pass a pointer to a structure, so the function can modify the data.
    The situation you described does not make the pointer any less local to a function.

    Again it is hard to generalise about programs without a specific example.
    Well apparently you fail to realize that if the shoe (the card decks in the game) and the shuffling algorithm are separate, self-contained entities it is trivial to integrate that code into another card game project, say, FreeCell. By spewing globals all over the place, you learn to rely on those and end up introducing interesting side effects to a program. That doesn't fascilitate code reuse. It isn't as easy as it could be to compile an already available shuffle, so you have to resolve the same, boring part of the project over and over again.

    There are uses for global scoped stuff. But they are normally environment variables and not program data. Your inability to realise this is fascinating.

    But then, you claimed to have maintained things before so who knows what sort of kluges you have up your sleeve to avoid whittling away at tedium.

    You can guarantee he will usually hold AA KK or QQ, probably best to fold anyway
    Every hand's a winner, every hand's a loser. You're playing with predictable people.

  9. #54
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by whiteflags View Post
    Well don't talk as though local variables are the reason the projects that you work on tend to get scrapped. It is far more likely that you FUBAR'd them, if you had a significant role in the development of said software.
    Nowt to do with me
    Well maybe I spotted a big flaw in the design
    I joined it midway through. (then everything went pear shaped - lol)

    Quote Originally Posted by whiteflags View Post
    Also, Texas Holdem can be implemented trivially: you can write most card games for schoolwork if the I/O is lame enough, and you use rand as the PRNG. That's not a very convincing example on its own. Unless you want us to just give you the benefit of the doubt of course.
    What the program does is take a file such as below

    Code:
    Game #6682418603: Festive Freeroll (ID8701531) 0+0 - Hold'em NL (75/150) - 2008/12/16 - 18:46:47 (UK)
    Table "8701531 - 120" Seat 1 is the button. 
    Seat 1: Jacq5050 sits out
    Seat 2: Helokitty (2220 in chips)
    Seat 3: esbo(1635 in chips)
    Seat 4: _angela79 (4255 in chips)
    Seat 5: Sveto2609 sits out
    Seat 6: John135 (2630 in chips)
    Seat 7: Arsim0205 sits out
    Seat 8: Cojoc3101 sits out
    Seat 9: Donn20044 sits out
    Seat 10: __j_t__ (3050 in chips)
    Helokitty: posts small blind 75
    Flastard: posts big blind 150
    ----- HOLE CARDS -----
    dealt to esbo[7c Ts]
    _angela79: calls 150
    Sveto2609: folds
    John135: calls 150
    Arsim0205: folds
    Cojoc3101: folds
    Donn20044: folds
    __j_t__: folds
    Jacq5050: folds
    Helokitty: calls 75
    Flastard: checks
    ----- FLOP ----- [2d 2s 6c]
    Helokitty: checks
    Flastard: checks
    _angela79: checks
    John135: bets 300
    Helokitty: folds
    Flastard: folds
    _angela79: folds
    Returned uncalled bets 300 to John135
    John135: doesn't show hand
    John135 collected 600 from Main pot
    ----- SUMMARY -----
    Total pot 600 Main pot 600 Rake 0
    Board [2d 2s 6c]
    Seat 1: Jacq5050 (button) folded before Flop (didn't bet)
    Seat 2: Helokitty (small blind) folded on the Flop
    Seat 3: esbo(big blind) folded on the Flop
    Seat 4: _angela79 folded on the Flop
    Seat 5: Sveto2609 folded before Flop (didn't bet)
    Seat 6: John135 collected 600
    Seat 7: Arsim0205 folded before Flop (didn't bet)
    Seat 8: Cojoc3101 folded before Flop (didn't bet)
    Seat 9: Donn20044 folded before Flop (didn't bet)
    Seat 10: __j_t__ folded before Flop (didn't bet)

    And produce statistics on betting pattens etc (except properly aligned)

    Code:
                                      8764794 - 18.txt
    Marty02       52.0X        -9810P      24G     13C      25G     13C     5.0W  60.0X8  15.5CX3    21%WON     1F   4%FW    17%P  17%F   0%T   4%R 033%SD Marty02 
    __simpy__     18.2X         7510P      28G      8C      11G      2C     5.0W  33.3X8  11.1CX3    18%WON     3F  11%FW     7%P  18%F  11%T  11%R 050%SD __simpy__ 
    Juan1908      33.3X         2071P       9G      7C      21G      7C     5.3W  50.0X8  11.9CX3    59%WON     4F  44%FW    44%P  33%F  22%T  11%R 044%SD Juan1908 
    Polipid1       0.0X        -1720P      15G      2C      13G      0C     1.0W   0.0X8   5.9CX3     7%WON     0F   0%FW     7%P   0%F   0%T   0%R 050%SD Polipid1 
    esbo_____     16.1X        40265P   84161G  13960C   78607G  12685C  7947.2W  15.6X8  16.4CX3     9%WON  4251F   5%FW     4%P   6%F   2%T   2%R 052%SD esbo_____
    Rammy         50.0X         9818P      77G     54C      18G      9C    32.0W  38.5X8  66.7CX3    42%WON    22F  29%FW    21%P  19%F  13%T   8%R 045%SD Rammy 
    Tyrant002     50.0X          -15P       2G      1C       2G      1C     2.0W   NaNX8   4.5CX3   100%WON     1F  50%FW    50%P   0%F   0%T   0%R 100%SD Tyrant002 
    Pme007         0.0X         -680P       1G      0C      14G      0C     0.0W   0.0X8   0.0CX3     0%WON     0F   0%FW     0%P   0%F   0%T   0%R NaN%SD Pme007 
    Gav280216      0.0X         -870P       1G      0C      10G      0C     0.0W   NaNX8   0.0CX3     0%WON     0F   0%FW     0%P   0%F   0%T   0%R NaN%SD Gav280216 
    Poshtosh      46.2X        20230P      16G      7C      13G      6C     4.0W  46.2X8  21.9CX3    25%WON     3F  19%FW     6%P  12%F  19%T  19%R 050%SD Poshtosh

    So " 5%FW 4%P 6%F 2%T 2%R 052%SD esbo_____" tells me I 'force' 5% of my wins, raise preflop 4% of the time, raise 6% of the time post flop, raise 2% of the time after the 'turn' and raise 2% of the time after the 'river' (last card), and win 52% of my 'showdowns'.

    Other sites use different formats but some stuff is broadly the same.
    That was a fairly simple game as not much bettiing too place.
    Some sites miss information out so you have to work it out from the info given.

    Also there are different types games, cash and tournements etc..



    Quote Originally Posted by whiteflags View Post


    Perhaps that's for the best. You often worry about us stealing the code, maybe we'll steal the specs too!


    The situation you described does not make the pointer any less local to a function.


    Well apparently you fail to realize that if the shoe (the card decks in the game) and the shuffling algorithm are separate, self-contained entities it is trivial to integrate that code into another card game project, say, FreeCell. By spewing globals all over the place, you learn to rely on those and end up introducing interesting side effects to a program. That doesn't fascilitate code reuse. It isn't as easy as it could be to compile an already available shuffle, so you have to resolve the same, boring part of the project over and over again.

    There are uses for global scoped stuff. But they are normally environment variables and not program data. Your inability to realise this is fascinating.

    But then, you claimed to have maintained things before so who knows what sort of kluges you have up your sleeve to avoid whittling away at tedium.


    Every hand's a winner, every hand's a loser. You're playing with predictable people.
    Wish I was I like predictable people!!
    People get more predictable as the stakes get higher, apart from the 'nutters'

  10. #55
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,176
    Quote Originally Posted by esbo
    It would prove too complex to have one multipurpose program
    especially as I will add other sites of unknown formats.
    Note that I did not ask whether the program was reusable. I asked whether the components are reusable. Trying to write a program to solve every possible future requirement is indeed a fruitless task. Trying to write a program that can be extended with minimal changing of the current source code, and whose components can be reused in a new program with similiar yet different requirements is both feasible and desirable.

    Quote Originally Posted by esbo
    It's not trivial just written by someone who is an efficient worker and knows how to
    design and write a program which is actually useful.
    You have provided no jusification for that statement. A 1200 line C++ program is typically trivial, at least when compared to say, the complexity and resulting number of lines of code required by a modern operating system. Of course, even a program with less than 100 lines of simple code can be useful, so by saying that your program was trivial I did not imply that it was not useful.

    Quote Originally Posted by esbo
    Most of the data my program uses might at some time need to be accessed
    by almost any function so it seems sensible to make most stuff global except for
    some local variable in functions, loop counters etc...
    I don't know what functionality I might want to add to it, so I don't want to have to
    constantly have to change the data I have to pass to the functions, it would just
    create an oppertunnity to introduce bugs every time I modified it.
    That implies that your functions are not designed to be cohesive, loosely coupled units. They do not "do one thing and do it well". This in turn makes them monolithic components that are hard to reuse.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #56
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by laserlight View Post
    Note that I did not ask whether the program was reusable. I asked whether the components are reusable. Trying to write a program to solve every possible future requirement is indeed a fruitless task. Trying to write a program that can be extended with minimal changing of the current source code, and whose components can be reused in a new program with similiar yet different requirements is both feasible and desirable.
    The 'components' as such are to specific to the program, that the chance needing to
    reuse then is so small that making them reusable is a waste of time.
    I sometines copy the odd bit out, for example opening a few files, not worth doing much for than that.



    Quote Originally Posted by laserlight View Post
    You have provided no jusification for that statement. A 1200 line C++ program is typically trivial, at least when compared to say, the complexity and resulting number of lines of code required by a modern operating system. Of course, even a program with less than 100 lines of simple code can be useful, so by saying that your program was trivial I did not imply that it was not useful.
    Those operating systems are so big because they are badly designed.
    It is because they fail to identify common components and constantly rewrite them,
    they fail the test of reusablity. My programs are small because I don't keep re-inventing
    the wheel.
    The size of some programs these days is staggering beyond belief, what do they do?
    Write every loop out!!?

    I encounter some apallingly bad practices using programs, take this edit box for example.
    It is a pathetic design.
    For example, if I move the cursor past the end of this line and click, it goes to the end of the like - it's complete garbage. If I press down arrow it moves to the start of the next line, not just down - absolute rubbish.

    Quote Originally Posted by laserlight View Post
    That implies that your functions are not designed to be cohesive, loosely coupled units. They do not "do one thing and do it well". This in turn makes them monolithic components that are hard to reuse.
    No it does not. Most of the functions are so specific that they could never concieveable
    be reused in another program. They are basically just bits of code which the program and that program alone uses several times. Thats why it is not a billion lines long!!
    Big programs tend to be bad programs.

  12. #57
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,176
    Quote Originally Posted by esbo
    The 'components' as such are to specific to the program, that the chance needing to
    reuse then is so small that making them reusable is a waste of time.
    This contradicts your own statement that "It has also been 'ported' to work on 3 other poker sites, which store the data in different ways, some put one history in a file, some several, some spread it over serveral folders, some use a mixture of the two, all use a different format for the history with some similarities."

    Quote Originally Posted by esbo
    Those operating systems are so big because they are badly designed.
    It is because they fail to identify common components and constantly rewrite them,
    they fail the test of reusablity.
    That at least some common components are repeated is probably true, but the underlying reason is the inherent complexity in the many problems that those programs solve.

    Quote Originally Posted by esbo
    My programs are small because I don't keep re-inventing
    the wheel.
    Since you are unable to reuse your components, it is obvious that you are merely bluffing.

    Quote Originally Posted by esbo
    The size of some programs these days is staggering beyond belief, what do they do?
    Write every loop out!!?
    As I noted, loop unrolling is not the cause for the size of large programs. It is also a gross exaggeration to suggest that professional developers manually unroll every loop.

    Quote Originally Posted by esbo
    I encounter some apallingly bad practices using programs, take this edit box for example.
    It is a pathetic design.
    For example, if I move the cursor past the end of this line and click, it goes to the end of the like - it's complete garbage. If I press down arrow it moves to the start of the next line, not just down - absolute rubbish.
    This is a user interface issue and thus is separate from what we are discussing. (Besides, I do not agree with your conclusions, but then user interfaces are often a matter of taste.)

    Quote Originally Posted by esbo
    Big programs tend to be bad programs.
    Unfortunately, that is true because they also almost certainly contain more bugs than small programs, assuming that programmers of similiar skill are involved in writing both the big and small programs.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #58
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by laserlight View Post
    This contradicts your own statement that "It has also been 'ported' to work on 3 other poker sites, which store the data in different ways, some put one history in a file, some several, some spread it over serveral folders, some use a mixture of the two, all use a different format for the history with some similarities."
    There was one routine which I wish I had written a little better, it would have made it
    easier to port and has been one of the things I have been meaning to do for a while
    but as it does make much difference anyway I have not bothered yet.
    Just a simple thing really, I should have 'tied' the length of a string to the striing.
    eg strptr=inputstring, strnglen=strlen(inputstring);
    It's not much work to modify though, and it would not always 'work' anyway.
    The stuff you mention above would have been too difficult to implement really
    especialy as I don't know all the formats used, and I really needed a working version
    for my main site, it would literally have cost me money to try and implement that.
    It's easy to say in an ideal world where you have no time or cost constraints.

    One puts it histories in reverse time order ie newest first rather than last.

  14. #59
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by laserlight View Post

    Since you are unable to reuse your components, it is obvious that you are merely bluffing.
    There are just not really any cmponents which can be reused.
    C already has a vast library or reusable components.
    Most of the time it is qucker to write your own routine thann search the librarys!!

    If you think you can extract all the data in less lines you are welcome to try!!

  15. #60
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Most of the time it is qucker to write your own routine thann search the librarys!!
    Your jokes do not make me even smile... Stop advertising your nonsense...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Page 4 of 5 FirstFirst 12345 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 01:54 AM
  2. Best way to avoid using global variables
    By Canadian0469 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2008, 12:02 PM
  3. Maintaining Global Vars Between Libraries
    By Canadian0469 in forum C Programming
    Replies: 9
    Last Post: 11-27-2007, 12:29 PM
  4. Global objects and exceptions
    By drrngrvy in forum C++ Programming
    Replies: 1
    Last Post: 09-29-2006, 08:37 AM
  5. Global Vars
    By ihsir in forum C++ Programming
    Replies: 4
    Last Post: 04-18-2002, 11:40 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21