Debian Codegraph @

doc/articles/comparisons.html

raw
1 ---
2 layout: default
3 title: Comparisons
4 ---
5
6 <div class="hgroup">
7 <div class="hgroup-inline">
8 <div class="panel">
9 <h1 id="comparisons">Comparisons</h1>
10 </div>
11 <div style="clear: both"></div>
12 </div>
13 </div>
14
15 <div class="hgroup">
16 <div class="hgroup-inline">
17 <div class="panel">
18 <p>
19 This article attempts to describe the whole open source configuration space, and Jsonnet's
20 place within it. Needless to say this is challenging and this will be a living document.
21 </p>
22 </div>
23 <div style="clear: both"></div>
24 </div>
25 </div>
26
27 <div class="hgroup">
28 <div class="hgroup-inline">
29 <div class="panel">
30 <h2 id="json">Comparison With JSON</h2>
31 </div>
32 <div style="clear: both"></div>
33 </div>
34 </div>
35
36 <div class="hgroup">
37 <div class="hgroup-inline">
38 <div class="panel">
39 <p>
40 JSON is used to describe data literally. Jsonnet extends JSON. Jsonnet has relaxed syntax
41 and comments to make it easier for humans to write. Jsonnet also adds constructs for
42 generating, translating and refining data. Because Jsonnet is an extension of JSON, valid
43 JSON is also valid Jsonnet that just emits that JSON unchanged.
44 </p>
45 </div>
46 <div style="clear: both"></div>
47 </div>
48 </div>
49
50
51 <div class="hgroup">
52 <div class="hgroup-inline">
53 <div class="panel">
54 <h2 id="yaml">Comparison With YAML</h2>
55 </div>
56 <div style="clear: both"></div>
57 </div>
58 </div>
59
60 <div class="hgroup">
61 <div class="hgroup-inline">
62 <div class="panel">
63 <p>
64 YAML is also an extension of JSON that generates JSON. Like Jsonnet, YAML is intended to
65 improve the user experience when data is transferred over the human / machine boundary. Its
66 syntax is very tight; YAML files are much shorter than their JSON equivalents. However its
67 constructs for generating, translating, and refining data are very weak in comparison to
68 Jsonnet. Moreover, the tightness of its syntax makes it hard to extend YAML to add these
69 features. In particular, YAML and Jsonnet disagree about what 1+1 should mean. YAML
70 already defines it to mean the string "1+1", whereas in Jsonnet we want it to be 2. Jsonnet
71 therefore builds from JSON and not YAML.
72 </p>
73 </div>
74 <div style="clear: both"></div>
75 </div>
76 </div>
77
78
79 <div class="hgroup">
80 <div class="hgroup-inline">
81 <div class="panel">
82 <h2 id="templates">Comparison With Other Templating Languages</h2>
83 </div>
84 <div style="clear: both"></div>
85 </div>
86 </div>
87
88 <div class="hgroup">
89 <div class="hgroup-inline">
90 <div class="panel">
91 <p>
92 Jsonnet is a form of templating language as it generates data from programs that interleave
93 verbatim data with computation constructs. However other templating languages operate on
94 plain text. That is to say that the data amongst which the computational constructs are
95 intermingled are simply plain text, and the output of the evaluation is also plain text.
96 This means such systems can be used to produce files with arbitrary syntax, e.g. custom
97 configuration file syntax, HTML, or even JSON.
98 </p>
99
100 <p>
101 However, since these templating languages are unaware of the syntax of the file they're
102 generating (they think it is simply plain text), they cannot help the programmer avoid
103 creating errors in the underlying representation.
104 </p>
105
106 <p>
107 By analogy, the C preprocessor has no understanding of C syntax. C preprocessor macros can
108 break that syntax if they are used wrongly. The net result of this is that problems with
109 the use of these macros must be debugged by examining the output of the evaluation (i.e. the
110 generated C code). Errors cannot be presented at the level at which the programmer is
111 actually working. For this reason, the vast majority of modern languages do not feature a
112 pre-processor.
113 </p>
114
115 <p>
116 The situation is the same when templating languages are used to generate HTML, often the
117 developer will check the generated HTML because successful evaluation of the template does
118 not imply the HTML is correct. The same is true when the target language is JSON. Jsonnet
119 avoids these problems, as although it is limited to JSON output, it is guaranteed that the
120 output will be valid JSON.
121 </p>
122 </div>
123 <div style="clear: both"></div>
124 </div>
125 </div>
126
127
128 <div class="hgroup">
129 <div class="hgroup-inline">
130 <div class="panel">
131 <h2 id="functional">Comparison With Other Functional Languages</h2>
132 </div>
133 <div style="clear: both"></div>
134 </div>
135 </div>
136
137 <div class="hgroup">
138 <div class="hgroup-inline">
139 <div class="panel">
140 <p>
141 ML is a functional language like Jsonnet. However ML is not pure, and also has strict
142 evaluation. Haskell is a pure lazy functional language, but unlike Jsonnet it has static
143 type safety. Providing static type safety means programmers have to understand type
144 inference, unification errors, and also provide the occasional type annotation. Even then,
145 some correct programs are rejected.
146 </p>
147 <p>
148 The Nix expression language, which is a pure lazy functional language with dynamic typing,
149 is therefore closer to Jsonnet. Jsonnet differs from the Nix expression language mainly in
150 that it also has object-oriented semantics in the form of mixins. Also, Jsonnet is an
151 extension of JSON, whereas the Nix expression language is not (in particular it lacks
152 floating point numbers). Finally, the Nix expression language is an embedded part of the
153 Nix package manager (and other tools), whereas Jsonnet is a standalone evaluator that can
154 sit in front of anything that accepts JSON.
155 </p>
156 </div>
157 <div style="clear: both"></div>
158 </div>
159 </div>
160
161
162 <div class="hgroup">
163 <div class="hgroup-inline">
164 <div class="panel">
165 <h2 id="scripting">Comparison With Other Scripting Languages</h2>
166 </div>
167 <div style="clear: both"></div>
168 </div>
169 </div>
170
171 <div class="hgroup">
172 <div class="hgroup-inline">
173 <div class="panel">
174 <p>
175 Most scripting languages are dynamically typed languages, often with object-oriented
176 features.
177 </p>
178
179 <p>
180 However, Jsonnet is also a templating language, which makes it very good for generating data
181 that is a mixture of verbatim and computed elements. Jsonnet programs can be easily
182 understood and modified by someone who only knows JSON. Any parts of the program which are
183 just JSON (i.e. do not make use of the extended Jsonnet constructs) can be modified with
184 predictable results. Jsonnet's pure functional semantics also make modifying the program
185 more predictable.
186 </p>
187
188 <p>
189 More importantly, Jsonnet has the property of hermeticity. This means that regardless of
190 when or where a Jsonnet program is evaluated, it will always generate the same JSON. This
191 means the program can be freely converted to data, and thus it can be thought of as
192 equivalent to data. This is not the case with general purpose languages, as they are
193 allowed to read files, contact the network, look up the time, or any number of other sources
194 of non-deterministic behavior. While it is possible to sandbox some scripting languages in
195 order to patch all of these holes, the result of this is a new language that is incompatible
196 with the original language. This makes Jsonnet a lot more suitable for configuration than
197 general purpose scripting languages.
198 </p>
199
200 <p>
201 One advantage that is often put forward for using general purpose scripting languages as
202 configuration languages is that they are already well-understood by a large number of
203 people. However this is also true of Jsonnet, because of its backwards compatibility with
204 JSON, its use of mixins (a decades old construct that is well-understood) and the mimicking
205 of Python syntax in cases where that makes sense (e.g. string formatting and comprehension
206 constructs).
207 </p>
208
209 <p>
210 Finally, Jsonnet is a much smaller language than most general purpose scripting languages,
211 yet despite this it has considerable expressive power.
212 </p>
213 </div>
214 <div style="clear: both"></div>
215 </div>
216 </div>
217
218
219 <div class="hgroup">
220 <div class="hgroup-inline">
221 <div class="panel">
222 <h2 id="configuration">Comparison With Other Configuration Languages</h2>
223 </div>
224 <div style="clear: both"></div>
225 </div>
226 </div>
227
228 <div class="hgroup">
229 <div class="hgroup-inline">
230 <div class="panel">
231 <p>
232 <a href="https://code.google.com/p/coil/">Coil</a> is a configuration language that is
233 interpreted by a Python library. Like Jsonnet, it has a JSON-like data model and some
234 facilities for extending objects to avoid duplication, referring to other parts of the
235 structure, and importing from other files. However it is a long way from the expressiveness
236 of the object-oriented semantics of Jsonnet, and it also does not have the ability to do
237 arithmetic beyond substituting values into strings. Finally, coil binds variables according
238 to <a href="https://en.wikipedia.org/wiki/Scope_(computer_science)#Dynamic_scope">dynamic
239 scoping rules</a>, which have long been discredited because they quickly make code very
240 difficult to understand. Jsonnet uses static scoping, like almost all mainstream languages.
241 </p>
242 <p>
243 <a href="https://github.com/wickman/pystachio">Pystachio</a> is a Python framework that
244 makes it easier to use Python itself as a configuration language. It extends Python's
245 existing data literals (which are JSON-like) to enforce immutability and add some templating
246 functionality. However it lacks the full computational abilities of Jsonnet. Although it
247 does allow some computation within string values via template expansion, the only variables
248 you can access are stored in separate "environments", and there is no direct access to the
249 data being configured. Even if we assume that all data is stored in environments and object
250 fields merely forward to the environment, the language does not have any true
251 object-orientation features. While scopes are inherited from outer objects, this just an
252 implicit way of doing what you can do in Jsonnet with <code>$</code> or <code>local</code>.
253 In Pystachio there is no way to derive a struct from another struct with a few changes, let
254 alone use late binding or the expressive power of mixins.
255 </p>
256 <p>
257 Finally, there are a number of other configuration languages that have grown to be used by a
258 range of applications. For example <a href="https://en.wikipedia.org/wiki/INI_file">INI
259 files</a> or <a href="https://httpd.apache.org/docs/2.2/configuring.html">Apache config
260 files</a>. Typically these offer few features beyond simple JSON and provide very little
261 functionality when compared to Jsonnet.
262 </p>
263 </div>
264 <div style="clear: both"></div>
265 </div>
266 </div>