Snippet Praxis
Copypasting from stuff is A Good Idea
Hi everyone, I'm back from Strangeloop! As always it was a fantastic conference. I've already done a 2000-word writeup on it but I'm waiting for all the videos to be uploaded first.
(As part of SL I taught 7-hour, 30 person TLA+ workshop. Normally my workshops are 3 days and 4 people, so this was a big experiment for me, and it worked pretty well! Students said they really liked it; I'm aiming to run a web version of it in early December. Stay tuned!)
On the train ride back, I wrote a couple of tools I've wanted a while. The first is a markdown link adder. I often need to put links in my documents and manually going over to every [empty link]()
is really annoying. So this opens a GUI with all the links in place, and then I can add them all in once place. Then it puts all the links in the appropriate places.
The second is a small browser extension. I often forget which page I opened a tab from, so the extension tracks which url lead to each tab and lets me query the provenance.
On the train ride back from Strange Loop, learned just enough browser extension APIs to make a research utility I've wanted for years: track which url I opened a new tab from. pic.twitter.com/4cuDoZ4Ih1
— Inactive; Bluesky is @hillelwayne(dot)com (@hillelogram) September 26, 2022
Now both of these will be really helpful to me, and I have a lot of thoughts on "toolmaking" in computing, but today I want to focus on the development process. The last time I made a Tkinter GUI was 2 years ago and I have no experience with javascript, much less webextensions. So the development process looked like this:
- Find a working copy of appropriate code (my old Tkinter experiment, the MDN docs)
- Tweak things until they kinda do what I want
- Tweak tweak tweak tweak tweak
Oh no, it's the dreaded Copy-and-paste programming! I regularly see old-timers complain about how modern programming isn't about "understanding" things anymore, people just copy code from Stackoverflow. This is seen as a bad thing.
My take is more complex: copy-paste can be a very good technique! The advantage of "take-and-modify" is that you're starting from a complete program with an embodied mental model, as opposed to building a model from scratch. That makes it (potentially) easier to understand, easier to learn, and easier to change into your desired program.
Here's a quick example. Let's say you want to use Hypothesis to generate inputs for some function. You also need some fairly wonky property, like I dunno the difference between the smallest and largest numbers in the list is less than the length of the list. If you're starting from scratch, you could look at the documentation and eventually figure out how to do what you want, but it'd take a while. But let's say I also give you this fragment of a hypothesis generator to start:
from hypothesis import given
import hypothesis.strategies as s
@s.composite
def smallest_is_first(draw):
i = draw(s.integers())
l = draw(s.lists(s.integers(min_value=i), min_size=1))
return [i] + l
@given(smallest_is_first())
def test_example_of_pbt(l):
assert min(l) == l[0]
You now have a complete example of a working hypothesis test. You can make tweaks to it and see how it works, which will help you improve your mental model. But also, you can make changes that move you through variations of working code, based on the information you extracted from the snippet. F.ex the snippet shows you how to use random values to generate other random values, so you can infer how to use random values to fix the list's size.
In my head I call this mindset snippet praxis.1 Copy-paste is a powerful technique for writing code in an unfamiliar domain.
Supporting Snippet Praxis
That we joke programming is "stack overflowing" speaks to how common this all is. But while it's well used, it's not well supported. All IDEs have a "snippet manager", but they're for dropping in templates and boilerplate, not complete blocks of code that we then modify. There's also not easy ways to search for snippets, besides skimming SO and people's blogs. Github has an enormous collection of snippets but it's not curated. That said, Microsoft's new IntelliCode API Usage Examples is a great step in the right direction. I used it to fix up some stuff on the linkfiller script and it helped a bunch.
(I switch between VSCode and Neovim regularly, they're both great editors, though I prefer to do things in Neovim when possible.)
A tool I'd like to see is a snippet rolodex. I want to take my Tkinter programs, split them into orthogonal snippets, and shove them in a database.
TipJar
I added a new section to my website, the tipjar, where I upload snippets I find useful. I'm gonna try to update it weeklyish, if I have good stuff to share. (I also need to make a better categorization system so you can filter for just the languages you want)
-
Praxis is a great word and we should use it more ↩
If you're reading this on the web, you can subscribe here. Updates are once a week. My main website is here.
My new book, Logic for Programmers, is now in early access! Get it here.