Wednesday, June 13, 2018

Piecing it together without tests...

I wrote a thing yesterday…. it was in python, and I was using PyCharm (which is very nice, and a departure from using vim + sniffer).

It was something I didn’t know how to do initially, and couldn’t really figure out how to test drive.  I did it without pair- or mob-programming.

It was essentially a screen-scraping tool to dl the latest version of a zip file... that's not really interesting.

I had to learn or relearn some libraries I’d forgotten about, using docs and StackOverflow, and the “inspect element” feature of chrome quite heavily.  I whinged about the lack of ids and names on elements in web pages on Twitter, but eventually got it worked out.

Because I wasn't sure how to make it work to begin with, I used the ipython repl (via the 'Python Terminal' window in PyCharm. I would try various ways of locating the anchor link I wanted until I found one that I thought was far less-fragile than the other ways (such as some rather dubious XPath). I basically pasted the lines that worked into the source file.

I ended up using requests, BeautifulSoup, and urlparse to do the whole job. It was really simple once I figured it out.

When I was done, I had something pretty ugly and linear.  I refactored it into composed functions and re-ran it to test it.

At that point I realized that I’d not really written any tests for it — I was building it in the repl mostly, and then pasting lines to the source file.  It dawned on me that a lot of code is written this way -- tested using the actual servers (not mocks or local data), pieced together from documents and code snippets and trail-n-error, tested only in the sense of being test-run by hand, and done entirely alone.

Since I didn't have the disciplinary reinforcement of a programming partner, it was easy to fall into making something that wasn't driven through automated tests, just through "try and see."

As my Industrial Logic coaching partner Mike Rieser would say, I was "programming for effect only."

I want to be fair about this being a little throwaway utility. It will be easy to discard and rewrite or to fix if the page changes in an unfriendly way. It has composed functions. It's tiny -- less than 50 lines including vertical whitespace. Easily 90% of the work was just learning how.

But I didn't test drive it. I didn't use partners. I could easily have done both.

I'm realizing that Lewin's equation was right. My behavior is a function of both my personality and my environment, and by taking myself out of my normal environment my behavior was not at the same professional level as usual.

Something to think about. 

No comments:

Post a Comment