| 1 |
--- |
| 2 |
title: What is wrong with node anchors and references? |
| 3 |
--- |
| 4 |
|
| 5 |
An example of a snippet of YAML that uses node anchors and references is described on the [YAML wikipedia page](https://en.wikipedia.org/wiki/YAML#Advanced_components): |
| 6 |
|
| 7 |
```yaml |
| 8 |
# sequencer protocols for Laser eye surgery |
| 9 |
--- |
| 10 |
- step: &id001 # defines anchor label &id001 |
| 11 |
instrument: Lasik 2000 |
| 12 |
pulseEnergy: 5.4 |
| 13 |
pulseDuration: 12 |
| 14 |
repetition: 1000 |
| 15 |
spotSize: 1mm |
| 16 |
|
| 17 |
- step: &id002 |
| 18 |
instrument: Lasik 2000 |
| 19 |
pulseEnergy: 5.0 |
| 20 |
pulseDuration: 10 |
| 21 |
repetition: 500 |
| 22 |
spotSize: 2mm |
| 23 |
- step: *id001 # refers to the first step (with anchor &id001) |
| 24 |
- step: *id002 # refers to the second step |
| 25 |
- step: |
| 26 |
<<: *id001 |
| 27 |
spotSize: 2mm # redefines just this key, refers rest from &id001 |
| 28 |
- step: *id002 |
| 29 |
``` |
| 30 |
|
| 31 |
While the intent of the feature is obvious (it lets you deduplicate code), the effect is to make the markup |
| 32 |
more or less unreadable to non-programmers. |
| 33 |
|
| 34 |
The example above could be refactored to be clearly as follows: |
| 35 |
|
| 36 |
```yaml |
| 37 |
# sequencer protocols for Laser eye surgery |
| 38 |
--- |
| 39 |
- step: |
| 40 |
instrument: Lasik 2000 |
| 41 |
pulseEnergy: 5.4 |
| 42 |
pulseDuration: 12 |
| 43 |
repetition: 1000 |
| 44 |
spotSize: 1mm |
| 45 |
- step: |
| 46 |
instrument: Lasik 2000 |
| 47 |
pulseEnergy: 5.0 |
| 48 |
pulseDuration: 10 |
| 49 |
repetition: 500 |
| 50 |
spotSize: 2mm |
| 51 |
- step: |
| 52 |
instrument: Lasik 2000 |
| 53 |
pulseEnergy: 5.4 |
| 54 |
pulseDuration: 12 |
| 55 |
repetition: 1000 |
| 56 |
spotSize: 1mm |
| 57 |
- step: |
| 58 |
instrument: Lasik 2000 |
| 59 |
pulseEnergy: 5.0 |
| 60 |
pulseDuration: 10 |
| 61 |
repetition: 500 |
| 62 |
spotSize: 2mm |
| 63 |
- step: |
| 64 |
instrument: Lasik 2000 |
| 65 |
pulseEnergy: 5.4 |
| 66 |
pulseDuration: 12 |
| 67 |
repetition: 1000 |
| 68 |
spotSize: 2mm |
| 69 |
- step: |
| 70 |
instrument: Lasik 2000 |
| 71 |
pulseEnergy: 5.0 |
| 72 |
pulseDuration: 10 |
| 73 |
repetition: 500 |
| 74 |
spotSize: 2mm |
| 75 |
``` |
| 76 |
|
| 77 |
The intent of this document is a lot clearer than the version above - *especially* for |
| 78 |
non-programmers. However, it comes at a cost of increased repetition. |
| 79 |
|
| 80 |
Between the node/anchor version and this I would prefer this. |
| 81 |
|
| 82 |
However, it is still repetitive and ideally it should be non-repetitive and still |
| 83 |
clear. This can be done by refactoring the *structure* of the document and changing |
| 84 |
the way the application interprets it. |
| 85 |
|
| 86 |
For example, instead of representing using the schema above, a schema that separates |
| 87 |
step definitions from actual steps could be used. For example: |
| 88 |
|
| 89 |
```yaml |
| 90 |
step definitions: |
| 91 |
large: |
| 92 |
instrument: Lasik 2000 |
| 93 |
pulseEnergy: 5.4 |
| 94 |
pulseDuration: 12 |
| 95 |
repetition: 1000 |
| 96 |
spotSize: 1mm |
| 97 |
medium: |
| 98 |
instrument: Lasik 2000 |
| 99 |
pulseEnergy: 5.0 |
| 100 |
pulseDuration: 10 |
| 101 |
repetition: 500 |
| 102 |
spotSize: 2mm |
| 103 |
steps: |
| 104 |
- step: large |
| 105 |
- step: medium |
| 106 |
- step: large |
| 107 |
- step: medium |
| 108 |
- step: |
| 109 |
from: large |
| 110 |
except: |
| 111 |
spotSize: 2mm |
| 112 |
- step: large |
| 113 |
``` |
| 114 |
|
| 115 |
The above document has an entirely different and slightly complex schema but it |
| 116 |
fundamentally represents the same data as the node/anchor version above, in a clearer |
| 117 |
manner, without duplication. |