Skip to content

Commit 5111c2a

Browse files
committed
Principle: Write only one algorithm to accomplish a task.
This explains why and when "polyglot" formats are a bad idea.
1 parent 62fc770 commit 5111c2a

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

index.bs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@ Required IDs: using-http
2424
2525
Link Defaults: html (dfn) queue a task/in parallel/reflect
2626
</pre>
27+
<pre class="biblio">
28+
{
29+
"draft-ietf-oauth-sd-jwt-vc": {
30+
"href": "https://datatracker.ietf.org/doc/html/draft-ietf-oauth-sd-jwt-vc",
31+
"title": "SD-JWT-based Verifiable Credentials (SD-JWT VC)",
32+
"status": "Internet-Draft",
33+
"publisher": "IETF"
34+
},
35+
"vc-data-model-1.1": {
36+
"href": "https://www.w3.org/TR/vc-data-model-1.1/",
37+
"title": "Verifiable Credentials Data Model v1.1",
38+
"status": "REC",
39+
"publisher": "W3C",
40+
"deliveredBy": ["https://www.w3.org/groups/wg/vc"]
41+
}
42+
}
43+
</pre>
2744
<pre class="link-defaults">
2845
spec:css-cascade-5; type:dfn; text:inherit
2946
spec:html; type:event; text:resize
@@ -3839,6 +3856,52 @@ While the best path forward may be to choose not to specify the feature,
38393856
there is the risk that some implementations
38403857
may ship the feature as a nonstandard API.
38413858

3859+
<h3 id="multiple-algorithms">Write only one algorithm to accomplish a task</h3>
3860+
3861+
When specifying how to accomplish a task, write a single algorithm to do it,
3862+
instead of letting implementers pick between multiple algorithms.
3863+
It is very difficult to ensure that
3864+
two different algorithms produce the same results in all cases,
3865+
and doing so is rarely worth the cost.
3866+
3867+
Multiple algorithms seem particularly tempting when defining
3868+
how to parse file formats,
3869+
but they are an equally bad idea in that context.
3870+
3871+
<div class="example" id="html-polyglot">
3872+
3873+
[[html-polyglot inline]] was an attempt to define a common subset of both XHTML and HTML
3874+
that could be parsed into equivalent DOM trees
3875+
using either the [[HTML#the-xhtml-syntax|XHTML parsing]]
3876+
or [[HTML#syntax|HTML parsing]] algorithm.
3877+
Authors who tried to use this syntax tended to produce documents
3878+
that actually only worked with one of the two parsers.
3879+
3880+
</div>
3881+
3882+
<div class="example" id="json-ld-polyglot">
3883+
3884+
The [[vc-data-model-1.1 inline obsolete]] and [[cid inline]] both endorse implementations
3885+
that use either JSON or JSON-LD to parse bytes into their data models.
3886+
Because JSON-LD provides many more ways
3887+
to assign properties to particular objects than JSON does,
3888+
these specifications had to add extra rules to both kinds of parsers
3889+
in order to ensure that each input document had exactly one possible interpretation.
3890+
[[vc-data-model-2.0 inline]] and [[draft-ietf-oauth-sd-jwt-vc inline]] fixed the problem
3891+
by defining different media types for JSON-LD vs JSON,
3892+
which means implementations are instructed to use exactly one of the algorithms.
3893+
The authors of [[cid inline]] have decided the extra specification work and testing
3894+
are acceptable prices to be able to allow both implementation strategies,
3895+
and they have partially mitigated the risks by explicitly stating that
3896+
if a single document can conformantly have multiple interpretations,
3897+
it's a specification bug.
3898+
3899+
</div>
3900+
3901+
Note: While [[rfc6838#section-6|structured suffixes]] define that
3902+
a document can be parsed in two different ways,
3903+
they do not violate this rule because the results have different data models.
3904+
38423905
<h3 id="monkey-patching">Avoid monkey patching</h3>
38433906
A <dfn export>monkey patch</dfn> layers new functionality on top of an existing specification in a way that extends, overrides, or otherwise modifies the existing specification's behavior.
38443907
<a href="https://en.wikipedia.org/wiki/Monkey_patch">Monkey patching</a> <strong>is generally considered bad practice and should be avoided</strong> for the reasons listed below; but is sometimes unavoidable

0 commit comments

Comments
 (0)