I find that one of the best ways to demonstrate code examples, especially when picking apart something complicated or detailed, is to use diff hunks to illustrate differences.
This is something that still isn't done very often but I definitely see it more in blogs and articles more than in years past.
The reason that this works so well is that especially if the change being shown is a small part of a line, and even if the hunks are up to 6 lines or so in total, the color and the alignment draws the reader into the exact spots where code has changed. This is much better than pasting two larger chunks of code and putting the burden on the reader to pick apart the change.
The issue I am alluding to is that when I put in the <!-- language: lang-diff -->
comment above a code block in SO, this is not recognized. I'm just looking for a very light implementation of the diff language, which is probably the world's simplest syntax highlighting routine ever. If a string starts with +
, make the text of the rest of the line green, and if a line starts with -
, make it red. Otherwise, do not color the line.
In the meantime I just use <!-- language: lang-none -->
instead, the familiar diff format still helps because of the alignment and the repetition and the +
s and -
s. Coloring the lines would be really nice though.
As for those of you out there who never use diffs and can't stand them, well, this question doesn't really apply, and it won't take away any of the existing highlighting functionality that you know and love. In particular I am talking about adding a subtle color highlighting when diff output is used as a code block in a question or an answer. Not anything more custom or complicated than this.
Personally I find the usefulness of diffs to be self-evident. They draw the reader in to the exact thing that changed, it makes it so much easier to visualize the action that a piece of code has gone through. Yeah, if one has photographic memory, perhaps it's just moot... I have the opposite tendency, large walls of text put me to sleep, so anything that helps to pinpoint important information helps.
diff
after the three backticks that begin a code block, and it does it nicely.