After publishing yesterdays epic post. I had time to think through some of my code in a bit more detail.
In particular the front matter and feed code generated when sending Webmentions.
I originally inserted some front matter at the top of my page like this:
This was overly complicated and verbose.
If I am sending a Webmention I only care about 2 things.
The target URL
The Webmention type (for formatting only)
Lumping the target and type together above was my first mistake.
My second was adding a name that wasn’t needed.
It lead to code like this for my feed “queue”
The code had to look for all the types of Webmention just to check if there was anything to send. It meant it would lead to verbose template logic. It’s a bad code smell. At the time I knew it wasn’t the best, getting something running is half the problem, we can refine later. So let’s refine it.
Refactoring the front matter from before, as below, is immediately better.
This is easy to read and clearly states what it is doing.
We only care if a target exists to generate the feed item. What type of Webmention we are sending will be determined by the receiver when they validate the page and look at the Microformats embedded in the page.
It means we can refactor the previous feed code to this:
Declaring the Webmention type like this webmention: "reply" also results in much cleaner code.
Previously in the header of each article and note page I had an include file called “author” that looked like this:
With the refactored code we can do something a bit more elegant like this:
The include code ends up being longer. But it is far more readable and quicker to execute (about 0.2 seconds faster).
By reducing the if statements looking for multiple variables and simplifying down to the case statement it should be easier to maintain and extend later. Separating the logic for the link and Microformat code inserted away from the HTML code block, keeps things clear and unambiguous.