How to Share Code With Users
Published on SepĀ 8, 2008 (updated AprĀ 16, 2024), filed under development, quality (feed). (Share this on Mastodon orĀ Bluesky?)
This and many other posts are also available as a pretty, well-behaved ebook: On Web Development.
If you share HTML/CSS code with users: Make sure that the code is valid and that ideally, it works with both HTML and XHTML.
Focusing on valid codeāa step towards quality codeāshould be obvious. āInvalidatingā other peopleās sites isnāt nice. Recommending code to be insertable without modification with both HTML and supposed XHTML is less evident: Itās often possible to offer the same code for both flavors, and hence such code can improve usability and, ultimately, the experience of the developers embedding your code.
Letās have a quick look at a made-up code example to demonstrate how all of this can work:
<p>My awesome <a href="https://example.com/?foo=bar&baz=scribble">gadget thingy</a>ā¦<br>
Nothingās better for your site.
Technically valid HTML, except for the classic ampersand issue:
<p>My awesome <a href="https://example.com/?foo=bar&baz=scribbleā>gadget thingy</a>ā¦<br>
Nothingās better for your site.
ā¦now fixed. [In ChapterĀ 2 of Upgrade Your HTML, Iāll argue not to escape ampersands unless necessary, but that happens many years later, in a different world of web development.]
Making this code snippet formally ready for XHTML is simple, too, however, the br
element deserves different treatment in HTML and XHTML. We want elegant code in both. The idea now is to do what web development often asks us to do, to look for alternatives. And, CSSāour partner for all styling questionsāto the rescue, we can replace the br
element by a p
element:
<p>My awesome <a href="https://example.com/?foo=bar&baz=scribble">gadget thingy</a>ā¦</p>
<p>Nothingās better for your site.</p>
That should have been easy to follow. Code shared with users comes with responsibility, and it requires focus on compatibility (different environments, here: document types) and maintainability (the code better be good to spare users from updates, and you from supporting badly aging code).
All of this should inspire larger companies, too. The āmake sure the HTML is at least validā part is most important. Donāt hand your users code thatās of poor quality. Especially if your users donāt have a good idea about code.
Update (September 24, 2020)
Thereās more! The 4 Pillars of Good Embed Code.
About Me
Iām Jens (long: Jens Oliver Meiert), and Iām a web developer, manager, and author. Iāve been working as a technical lead and engineering manager for companies youāve never heard of and companies you use every day, Iām an occasional contributor to web standards (like HTML, CSS, WCAG), and I write and review books for OāReilly and Frontend Dogma.
I love trying things, not only in web development and engineering management, but also in other areas like philosophy. Here on meiert.com I share some of my experiences and views. (I value you being critical, interpreting charitably, and giving feedback.)