User Controls
Is it bad practice to initialize a null variable?
-
2016-09-18 at 3:03 PM UTCI 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]? -
2016-09-20 at 7:09 AM UTCWhere in your first code example, do you initialize a null variable? I usually assign all of my variables a value when I declare them.
-
2016-09-20 at 7:23 AM UTCUse 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. -
2016-09-20 at 7:50 AM UTC
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. -
2016-09-20 at 8:30 AM UTCdepends 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
-
2016-09-20 at 12:20 PM UTCAs 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.
-
2016-09-20 at 1:35 PM UTC
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.