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'. 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!).
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:
...
Just some healthy advice from my 'Avoid a full day of debugging' bucket :D
--
Chris Evich, RHCA, RHCE, RHCDS, RHCSS
Quality Assurance Engineer
e-mail: cevich + `@' + redhat.com o: 1-888-RED-HAT1 x44214
_______________________________________________
Virt-test-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/virt-test-devel