On 03/18/2013 10:45 PM, Chris Evich wrote:
On 02/25/2013 04:29 AM, Alex Jia wrote:
On 02/22/2013 02:51 PM, Yu Mingfei wrote:
+ # modify XML if called for
+ if net_name is not "":
A shorter code is 'if net_name'.
Alex is right, and "duck-typing" is generally a good idea. However,
in this specific situation I actually prefer the more explicit 'is not
""' or an equivalent. The simple reason is that when dealing with
user input, you can easily run into implied (i.e. hidden) values that
can be (from actual experience) VERY tricky to debug.
To understand, consider all of the things which evaluate to false:
'False', '[]', '{}', '""', '(,)', '0', 'None'.
Yes, we must be clear about which one we want with "if xxx".
It seems that I should check my codes again carefully.
I'm sure this have happened in some place. :D
The last one will ruin your day very easily, it's the default value
returned by functions, methods, and many accessors:
some_option = params.get("foobar")
if not some_option:
...
-or-
def get_option(option_one, option_two)
...
option_one + option_two
some_option = get_option("foo", "bar")
if not some_option:
...
These conditions will /PASS/ if the "foobar" param doesn't exist (i.e.
get() defaults to None implicitly!), and because someone forgot the
'return' statement in get_option() (i.e. an easy code bug!).
Good catch! I did not occur this yet, it is really amazing~
So, IMHO, it's better in this situation to be explicit, don't process
user-input with any implied or hidden defaults:
some_option = params.get("foobar", None)
if some_option is None:
...
elif not some_option:
...
...
or even better:
some_option = params.get("foobar", "@!@!SENTINEL!@!@")
if some_option is "@!@!SENTINEL!@!@":
...
elif not some_option:
...
Good idea, thanks~
Just some healthy advice from my 'Avoid a full day of debugging'
bucket :D
--
Best Regards
Yu Mingfei
_______________________________________________
Virt-test-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/virt-test-devel