A nice little refactor


I was in some code I haven’t visited in a while. And I came upon something I coded months ago.

It used a list comprehension to test every element of a list. If the result was empty, it signaled an error. Otherwise, it used result[0].

Gah! That’s so retarded! Was I asleep when I wrote that?

I changed it to a generator expression with a .next(), within a try/except on StopIteration.

One fewer line of code, and it’s faster.

I felt good.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.