Wednesday, August 24, 2011

Cool idiom someone showed me today

Say you have something like this in your code:

if parrot == "dead":

Depending on the type of string you're testing, often you can do this:
if "dead" in parrot:
This is powerful because when you want to test this function, you can pass it "dead.unittest" and it will succeed, exercising the code.

Naturally, use with caution as if the code shouldn't execute on "undead" you can't use this sort of idiom.

The more I think about it, the more I think this could be considered dangerous; good for some internal uses (e.g. unit tests) but there are tradeoffs; probably doesn't belong in a public facing API.

But I'm posting it anyway because, hey, maybe someone has input. (lol)

No comments:

Post a Comment