User Controls

Is it bad practice to initialize a null variable?

  1. #1
    Sophie Pedophile Tech Support
    I suppose it might be redundant. You could do...


    import random
    import string

    def digit_generator(size=5, chars=string.digits):
    return ' '.join(random.choice(chars) for _ in range(size))


    Instead of...

    import random
    import string

    digits = ''

    def digit_generator(size=5, chars=string.digits):
    global digits
    digits = ''.join(random.choice(chars) for _ in range(size))



    [FONT=arial]Thoughts[/FONT]?
  2. #2
    SBTlauien African Astronaut
    Where in your first code example, do you initialize a null variable? I usually assign all of my variables a value when I declare them.
  3. #3
    Lanny Bird of Courage
    Use of global in python is a code smell, so avoid that. The latter piece of code would be considered very bad style. Generally speaking it is preferable to return a value without mutating something out in space, how important you think that is depends on ideological factors but even hardcore C people will admit if you can fit your value in a word it's better to return it as a value than mutate it as a refrence. That's kind of jargony, maybe not relevant here.

    The issues here are twofold, firstly it's unintuitive, if a function generates something it should return it. Secondly there's potential for concurrency woes, if you call this function you then need to read the value back from the global `digits` before using it. Consider if you call this function, use it once, but then the scheduler starts execution on some other thread that calls this function again, then resumes execution on the first. `digits` in the first thread will evaluate to something different than it did the line before, it can change under your feet over the course of executing a function. There are some caveats in terms of what "scheduler" means in the context of python but gevent is a thing (and a very good thing at that!) and this is a real situation you need to consider.
  4. #4
    Sophie Pedophile Tech Support
    Use of global in python is a code smell, so avoid that. The latter piece of code would be considered very bad style. Generally speaking it is preferable to return a value without mutating something out in space, how important you think that is depends on ideological factors but even hardcore C people will admit if you can fit your value in a word it's better to return it as a value than mutate it as a refrence. That's kind of jargony, maybe not relevant here.

    The issues here are twofold, firstly it's unintuitive, if a function generates something it should return it. Secondly there's potential for concurrency woes, if you call this function you then need to read the value back from the global `digits` before using it. Consider if you call this function, use it once, but then the scheduler starts execution on some other thread that calls this function again, then resumes execution on the first. `digits` in the first thread will evaluate to something different than it did the line before, it can change under your feet over the course of executing a function. There are some caveats in terms of what "scheduler" means in the context of python but gevent is a thing (and a very good thing at that!) and this is a real situation you need to consider.

    Yee, i don't think i quite thought it through. Thanks.
  5. #5
    aldra JIDF Controlled Opposition
    depends on the language, sometimes you need to initialise it before you use to make sure it's cleared what was previously in that memory space I prefer to do it in less explicit languages like php as well though, find it easier to keep track of them if they're all declared in the same place
  6. #6
    oatking Yung Blood
    As Lanny said, it's better to stay away from writeable globals. It always comes back sooner or later to bite you in your ass. The first function looks nice and clean, and it's easy to read because it's self-contained.
  7. #7
    Sophie Pedophile Tech Support
    As Lanny said, it's better to stay away from writeable globals. It always comes back sooner or later to bite you in your ass. The first function looks nice and clean, and it's easy to read because it's self-contained.

    Yeah, agreed. Thanks everybody.
Jump to Top