Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: I know that global variables are evil but ...

  1. #1
    Join Date
    Jul 2007
    Location
    The Bavarian Alps
    Beans
    129
    Distro
    Kubuntu 7.10 Gutsy Gibbon

    I know that global variables are evil but ...

    As the title says "I know that global variables are evil and those who use them should be publicly humiliated but ...."

    I am writing a simple program with Python. It has a GUI made with Qt.

    The main window (class theApp(GUI) starts by reading the actual serial port from a configuration file.

    It also has a menu point that when selected opens a "preferences" box (class prefDlg(frmPref)
    This class begins by opening the configuration file and reading the current serial port. It adds this in to a lineEdit.

    When the user closes the dialog, that calls a function in the class prefDlg(frmPref): (def _acceptPreferences(self) that writes the actual port back to the configuration file.

    Up to now I don't have a huge problem but ..

    The next time the user calls a function that uses the serial port I should ideally be using the one he just selected in the preferences dialog.

    This means I now need to open the configuration file at the start of every function

    Or I could use a global value for the whole program containing the port.

    my problem is that both methods seem inelegant.
    A global variable will put me in league with the devil.
    Opening, reading and closing configuration files all over the place does not seem so good either.

    As a configuration file is a fairly standard thing I hope there is a fantastic solution out there.

    thanks for all any any help.
    Neill

  2. #2
    Join Date
    Oct 2006
    Location
    Austin, Texas
    Beans
    2,715

    Re: I know that global variables are evil but ...

    Can't you just wrap it in a class?

  3. #3
    Join Date
    Oct 2005
    Beans
    36

    Re: I know that global variables are evil but ...

    I'd use a Singleton to handle Configuration data that can be called from anywhere in the program.

    Code:
    //Java-ish code.
    public class Configuration
    {
         private static Configuration Config;
    
         private Configuration()
         {
    
         }
    
        public Configuration getInstance()
        {
            if(Config == null)
            {
               Config = new Configuration();
            }
         
            return Config;
        }
    }

  4. #4
    Join Date
    Oct 2007
    Beans
    115
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: I know that global variables are evil but ...

    In python there no exist the necesity to create a singleton, take a module (a .py) and write the code there, import it, thus "it will be like a singleton".
    Code:
    value = 5
     
    if __name__ == "__nothing__":
       print "nothing!!!!"
    else:
        print "or some... lol"
     
    def Add1():
        global value
        value = value +1
    def Dec1():
        global value
        value = value -1
    
    
    Add1()
    Add1()
    Add1()
    Dec1()
    print value
    import it 1 time (or more times, doesnt matter, the impact will be 1 and only 1 instance of the module in the program... a "singleton").
    Last edited by tyoc; November 30th, 2007 at 08:15 PM. Reason: ops
    me ||
    Las cosas que no se comparten se pierden. || Todo es lo que aparenta ser hasta que se descubre lo que en realidad es.
    How to become a hacker?

  5. #5
    Join Date
    Jun 2006
    Location
    CT, USA
    Beans
    5,267
    Distro
    Ubuntu 6.10 Edgy

    Re: I know that global variables are evil but ...

    for quick and dirty global object, make name of dictionary global. Read about keyword 'global'

  6. #6
    Join Date
    Mar 2007
    Location
    Turkey
    Beans
    1,574

    Re: I know that global variables are evil but ...

    Quote Originally Posted by NeillHog View Post
    As the title says "I know that global variables are evil and those who use them should be publicly humiliated but ...."
    Neill
    That's wrong. You can do anything you want in your program. The "never use goto" and similar is just some annoyance.

  7. #7
    Join Date
    Oct 2006
    Location
    Austin, Texas
    Beans
    2,715

    Re: I know that global variables are evil but ...

    Quote Originally Posted by Majorix View Post
    That's wrong. You can do anything you want in your program. The "never use goto" and similar is just some annoyance.
    Well, yeah, you can write your entire program in obfuscated Perl if you want. But when it comes to best practice, there are certain design strategies that are considered taboo. And they got that way for a reason. Typically because they're either easy to abuse, or because they make maintainability a hassle. Unnecessary globals and gotos are among those things. The biggest issue with globals is the entire reason Python emphasizes on namespaces so much... Because they make namespace pollution hard to avoid (thus unmaintainable code).

  8. #8
    Join Date
    Apr 2007
    Beans
    14,781

    Re: I know that global variables are evil but ...

    Quote Originally Posted by Majorix View Post
    That's wrong. You can do anything you want in your program. The "never use goto" and similar is just some annoyance.
    You can do anything you want, but should you, is the question.

    I have programs that I wrote which use extensive use of globals and goto's, but they are in Fortran 77, in more modern languages, the use of these concepts are discouraged not because of irrational hate, but because of the lessons learned with earlier, less structured languages.

  9. #9
    Join Date
    Jul 2007
    Location
    The Bavarian Alps
    Beans
    129
    Distro
    Kubuntu 7.10 Gutsy Gibbon

    Re: I know that global variables are evil but ...

    Thank you for the replies.
    I need to do a lot more reading about "Singletons".

    I understand from the above that reading a configuration file every time I need the serial port is not a problem but that the code I use to do it should be in one class which I call whenever I need a variable from the configuration file. As a suggestion:

    Port = getConfVariable("port", "hardware")

    I could use the getConfVariable whenever I needed anything from my configuration file.

    The question remains where to define the name of my configuration file? Here I can either hardcode it in to my getConfVariable class or I could use a global variable so that I can have it right at the top of my code where it is easy to find and easy to change if ever I want to change from ~/.gabow,conf

    I appreciate all your helps and comments
    Thanks!
    I am a newbie. I have been for many years now. Sometimes I feel that I understand things, turn the corner and find that there is even more to understand.

  10. #10
    Join Date
    Dec 2006
    Location
    Uk
    Beans
    109

    Re: I know that global variables are evil but ...

    Quote Originally Posted by NeillHog View Post
    I understand from the above that reading a configuration file every time I need the serial port is not a problem but that the code I use to do it should be in one class which I call whenever I need a variable from the configuration file.
    Why read the config file multiple times when you can just store the value in the config object?

    The question remains where to define the name of my configuration file? Here I can either hardcode it in to my getConfVariable class or I could use a global variable so that I can have it right at the top of my code where it is easy to find and easy to change if ever I want to change from ~/.gabow,conf
    Sure if you only have one item at the top of your code it is easy to find. What if you have 10 items? or 50 items?
    OpenStreetMap - Free editable map of the whole world

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •