Wednesday, August 24, 2011

Cool idiom someone showed me today

Say you have something like this in your code:

if parrot == "dead":
    parrot.return_to_store()

Depending on the type of string you're testing, often you can do this:
if "dead" in parrot:
    parrot.return_to_store()
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)





Monday, August 22, 2011

nice interface change

Blogger now remembers what mode you were in last when posting. This is helpful, as I never have to see their Compose mode. Whee, I posted here.