Skip to content

Commit 11d92df

Browse files
committed
Add a new article about naming convention.
This article explain what was discussed in the RFC : hoaproject/Central#54
1 parent 316b828 commit 11d92df

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<?xyl-overlay href="hoa://Application/Overlays/Article.xyl"?>
3+
<?xyl-meta name="title" value="Introduce data conversion naming"?>
4+
<?xyl-meta name="date" value="2017-07-01T07:54:50+02:00"?>
5+
6+
<overlay xmlns="http://hoa-project.net/xyl/xylophone">
7+
<article id="main">
8+
<p>
9+
For 2017, we <a href="http://discourse.hoa-project.net/t/roadmap-to-2017-a-draft/235">defined
10+
a roadmap</a> composed of Request For Comments (RFC) that are discussed
11+
and implemented. One if these RFC is about
12+
<a href="https://github.com/hoaproject/Central/issues/54">naming convention
13+
and simplification</a>.
14+
</p>
15+
<p>
16+
So far, we use this formalism: <code>getFoo()</code> to name a method
17+
that returns the value <code>foo</code>. This can be a direct attribute, or a
18+
computation. To get this data within another form, i.e. to convert this
19+
data into another type, we don't have any formalism yet. For instance,
20+
if <code>foo</code> is an array, and we would like to get it as a string,
21+
we will probably name a method like <code>getFooAsString()</code> but this
22+
is not deterministic.
23+
</p>
24+
<p>
25+
Conversions can be expensive so the method prefix must be clear enough to
26+
know operation cost directly inside the code.
27+
</p>
28+
<p>
29+
We decided to introduce 3 method prefixes:
30+
</p>
31+
<table>
32+
<thead>
33+
<tr>
34+
<th>Prefix</th>
35+
<th>Cost</th>
36+
<th>Consumes convertee</th>
37+
</tr>
38+
</thead>
39+
<tbody>
40+
<tr>
41+
<td><code>as</code></td>
42+
<td>Free</td>
43+
<td>No</td>
44+
</tr>
45+
<tr>
46+
<td><code>to</code></td>
47+
<td>Expensive</td>
48+
<td>No</td>
49+
</tr>
50+
<tr>
51+
<td><code>into</code></td>
52+
<td>Variable</td>
53+
<td>Probably</td>
54+
</tr>
55+
</tbody>
56+
</table>
57+
<p>
58+
Example:
59+
</p>
60+
<ul>
61+
<li>
62+
Let's <code>$x</code> be a float. <code>asInteger()</code> will be
63+
(almost) free.
64+
</li>
65+
<li>
66+
Let's <code>$x</code> be an array. <code>toString()</code> will be
67+
expensive because we have to iterate over the array, to allocate a
68+
string, and to convert every pairs in the array as a string (like a
69+
serializer).
70+
</li>
71+
<li>
72+
Let's <code>$x</code> be an object. <code>intoArray()</code> will not be
73+
that expensive, it might reference all attributes into an array, so
74+
that's just one allocation.
75+
</li>
76+
</ul>
77+
<p>
78+
Conversions prefixed <code>as</code> and <code>into</code> typically
79+
decrease abstraction, either exposing a view into the underlying
80+
representation (<code>as</code>) or deconstructing data into its
81+
underlying representation (<code>into</code>). Conversions prefixed
82+
<code>to</code>, on the other hand, typically stay at the same level of
83+
abstraction but do some work to change one representation into another.
84+
</p>
85+
<p>
86+
This is not something we will use often, but it is important to have a
87+
strict naming here. Based on this naming, the user will be able to choose
88+
if the resulting data must be cached or not (for instance, all <code>to</code>
89+
conversions are likely to be cached because they might be expensive).
90+
</p>
91+
</article>
92+
</overlay>

0 commit comments

Comments
 (0)