Like Tree67Likes

Global Varibles

This is a discussion on Global Varibles within the C++ Programming forums, part of the General Programming Boards category; Sorry if I was a bit overly cocky - it does feel somewhat like a competition. But, I think using ...

  1. #76
    Registered User
    Join Date
    Feb 2010
    Posts
    93
    Sorry if I was a bit overly cocky - it does feel somewhat like a competition.
    But, I think using a namespace in my situation is a perfectly legitamate C++ usage.
    I actually love it.

    (I really do get all of your arguments too. They're fine in general, but just not for my situation.)

  2. #77
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by motocross1 View Post
    Sorry if I was a bit overly cocky - it does feel somewhat like a competition.
    But, I think using a namespace in my situation is a perfectly legitamate C++ usage.
    I actually love it.

    (I really do get all of your arguments too. They're fine in general, but just not for my situation.)
    Many newcomers on this board is in the same situation as you. They are writing code purely for themselves as a hobby-activity, and thus they feel many of the guidelines that are provided here are not applicable to them. And to a certain extent, they are right. If you are writing a tiny tiny program (less than 100 lines) to check whether or not a string is palindrome or not (very common problem for beginners), it is very tempting to throw a goto or 2 in there, because gotos are easier than loops and they work just as well in that particular case.
    This is all fine and dandy, but the thing about gotos is that they don't scale very well, when your programs reach 1000 lines of code, excessive use of gotos will make the code SIGNIFICANTLY harder to follow, and when the code is 100000 lines and there are 12 other programmers involved, using gotos where there should really be a loop, the code will be flat out impossible to troubleshoot or even read and understand.
    As such, all of the experienced programmers on this board always argue strongly against using gotos even in tiny programs, because it is important as a programmer to get into the right mindset from the beginning, and not resort to hacky half-arsed solutions. If you cultivate a strong coding discipline and shun some of these horrid coding loopholes, you will grow to be a better programmer in the long run.

    Now i know you're not a complete beginner per se, and you might argue that gotos are worse than global variables (rightly so i would say). But the point still stands, in regards to coding, steer clear of the easy solutions, do things properly from the start, and 5 years from now you would never even consider using globals or any other form of cheap loophole to get a piece of code to do what you want, you will just do it properly, on pure instinct and habit.

    I think this is worth mentioning aswell, stop thinking about how you can get this darned thing to just work and be done with it, and start thinking about how you as a coder can grow (even if just a tiny bit), by solving this properly.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  3. #78
    Registered User
    Join Date
    Feb 2010
    Posts
    93
    GOTO is a stupid example - it IS hard to follow.

    if( !User.isLoggedIn ) DoLogin();
    ...is the same as...
    if( !global_isLoggedIn ) DoLogin();
    Both are easy to track and understand.

    My code is very efficient, stable, readable, manageable, scalable, and "Professional".
    Just quit already.

  4. #79
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by motocross1 View Post
    My code is very efficient, stable, readable, manageable, scalable, and "Professional".
    Just quit already.
    So, why did you start this thread ?
    Quote Originally Posted by motocross1 View Post
    But, what is the "professional" way?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #80
    Registered User
    Join Date
    May 2009
    Posts
    2,680
    FYI:

    The one thing that is harder to do when global variables are used that surprised me was writing test code is harder to do cleanly.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  6. #81
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by motocross1 View Post
    GOTO is a stupid example - it IS hard to follow.

    if( !User.isLoggedIn ) DoLogin();
    ...is the same as...
    if( !global_isLoggedIn ) DoLogin();
    Both are easy to track and understand.

    My code is very efficient, stable, readable, manageable, scalable, and "Professional".
    Just quit already.
    So in 100.000 lines of code, where would you go to find out what is wrong when global_isLoggedIn suddenly changes for no apparent reason?

    In the case of User.isLoggedIn(), all you have to do is to check that specific class (User), since nobody else will have access to the bool that decides whether the user is logged in or not.

    Why would i change global_isLoggedIn you might ask? Well say you don't, maybe one of your coworkers changes it in some obscure function where it shouldn't be touched at all, maybe he did i by accident, or he did it on purpose because he felt confident no other classes or functions depend on it.

    The private keyword is there to tell other coders to leave your stuff alone.

    Good luck debugging that, ill just open User.cpp and find out which function is the culprit, i might have to go through 5-10 functions, you will have between 1 and a googol lines of code to wade through, since you didn't specify who can mess with your variables, and who can't.

    Stop kidding yourself, using global variables instead of proper encapsulation in software development is like using blue cheese instead of reinforced concrete in structural engineering. Sure, it works when the project is tiny and can fit on your desk, but when you scale it up to a usable size, it collapses on itself and suddenly there is cheese everywhere.
    rags_to_riches and Elkvis like this.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  7. #82
    Registered User
    Join Date
    Feb 2010
    Posts
    93
    "where would you go to find out what is wrong"
    1-billion lines of code, find this: global_isLoggedIn=(true|false);
    Very easy.
    Duh.

  8. #83
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by motocross1 View Post
    "where would you go to find out what is wrong"
    1-billion lines of code, find this: global_isLoggedIn=(true|false);
    Very easy.
    Duh.
    Assuming that the code is in one file (Which it won't be), also assuming that there won't be 1500 different places where your global is being changed, and only one of them is wrong.

    Encapsulation is a cornerstone of the OOP concept, which in turn is a cornerstone of alot of software being written today. OOP has been with us for a while now, it took many great minds and thousands of man-years to come up with and refine. Thousands of PHDs and Professors swear by it and teach it on a daily basis, millions of programmers know OOP in and out and work with it many hours a day, billions of dollars are being poured into big software projects that utilize OOP, millions of pages in thousands of books have been written on the subject.

    But hey, i guess they're all wrong, you must be right. Damn, why didn't you tell us this sooner?

    Code:
                              (
                           (   )  )
                            )  ( )
                            .....
                         .:::::::::.
                         ~\_______/~
    HUMBLE PIE FROM ME TO YOU! BE SURE TO EAT IT ALL!
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  9. #84
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    140
    Quote Originally Posted by motocross1 View Post
    1-billion lines of code, find this: global_isLoggedIn=(true|false);
    But it could look like global_isLoggedIn=<some expression> or *PtrIsLoggedIn=(true|false) [Where somewhere there is a int * PtrIsLoggedIn; somewhere and PtrIsLoggedIn = &global_isLoggedIn; somewhere else] or many other variations.

    I am assuming that your situation is different than many of us. I am assuming that you wrote all the code in your application (except for the libraries supplied with your compiler). And you always intend to be the person who maintains this code. This reduces the importance of the guidelines many of us are mentioning.

  10. #85
    Registered User
    Join Date
    Feb 2010
    Posts
    93
    I am assuming that your situation is different than many of us.
    YES, finally, someone noticed.
    I'm not a corporation.

    Even then, do a Google: "gobals are good"
    There are some great reads out there:
    I love globals, or Google Core Dump
    The more I read, the more I like globals.

    (BTW, that humble pie is pretty cool.)

  11. #86
    Registered User
    Join Date
    Oct 2006
    Posts
    2,501
    if you like globals, that's fine. that's your prerogative. stop trying to convince the rest of us that they are ok. we're not buying what you're selling. if you ever work for someone, where your job responsibilities include working with other developers on a larger project, you can be sure that they will disagree with you, and exclusion of globals will likely be a matter of policy.
    rags_to_riches likes this.

  12. #87
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I've changed my mind: Long live the "Global Varibles" thread!
    Quote Originally Posted by motocross1
    My code is very efficient, stable, readable, manageable, scalable, and "Professional".
    I believe the "Professional" part, quotes included.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  13. #88
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    There are some great reads out there:
    I love globals, or Google Core Dump
    The more I read, the more I like globals.
    So you found another troll on your side of the argument. Good for you.

  14. #89
    Registered User
    Join Date
    Feb 2010
    Posts
    93
    Note: Singletons are not thread safe...
    C++ and The Perils of Double-Checked Locking: Part I | Dr Dobb's

    And, you guys are right about my use of "Professional".
    Scratch that.
    "Professional" means to be a clone who cannot think for themselves.
    Yep, I'm happily disqualified from that club.

    How about someone recommending to end the thread again?

  15. #90
    Registered User
    Join Date
    Mar 2011
    Posts
    61
    Quote Originally Posted by motocross1 View Post
    And, there ARE times to get an abortion.
    Okay, I don't want to go completely off-topic here, but this statement really offended me. Comparing a programming strategy to a real-life moral issue is a terrible analogy. Not to mention that there are no times when it is condonable to get an abortion because abortion is wrong 100% of the time.

    Quote Originally Posted by MK27 View Post
    Okay, but usually not because you planned to get pregnant, figuring this is (another?) good time for an abortion. Practically no one would want to make it a regular practice. Like, "Abortion? All the time! It's not a bad habit, really."
    Oh, really? So it's okay to kill a baby when you didn't plan on getting pregnant, but not okay when you did? What kind of philosophy is that? You either believe abortion is right or wrong, not right occasionally but wrong in excess. By saying that it's wrong to do it frequently, you're saying the act is wrong. If the act is wrong, it must always be wrong, regardles of the circumstances.

Page 6 of 7 FirstFirst 1234567 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string varibles??
    By Labmouse in forum C++ Programming
    Replies: 2
    Last Post: 08-23-2007, 08:16 AM
  2. Declaring Varibles
    By lbrault in forum C++ Programming
    Replies: 6
    Last Post: 02-05-2003, 11:23 AM
  3. extern varibles
    By manwhoonlyeats in forum C Programming
    Replies: 5
    Last Post: 12-17-2002, 11:43 PM
  4. C Functions and varibles
    By mart_man00 in forum C Programming
    Replies: 13
    Last Post: 08-01-2002, 01:11 PM
  5. varibles
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 06-24-2002, 10:15 AM

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