3 ms·
It is very easy to inadvertently add dependencies on key order. For example, if you are generating HTML, you might have something like: print("<a " + " ".j
by bjourne 10y ago
It is very easy to inadvertently add dependencies on key order. For example, if you are generating HTML, you might have something like:
print("<a " + " ".join(k+"=\""+v+"\"" for (k,v) in d.items()) + "/>")
And d would be a dict of attributes for the anchor element. The code will produce different output depending on computer which is not what you want because it can cause subtle problems for other systems expecting a certain output.
Several compiler bugs have been of this kind so it is not only newbie programmers that make these mistakes.
- eyelidlessness 10y agoHow does HTML attribute order change output?
- bjourne 10y agoThe code will produce a string which will be different depending on unpredictable factors. Such as the memory address of the 'd' object.
- eyelidlessness 10y agoBut how will that affect output? I'm not aware of any scenario where HTML attribute order matters.
- bjourne 10y agoIt shouldn't affect the way a browser renders the HTML. But there are many possible scenarios in which the order is relevant for some other reason. Suppose you have another system monitoring the web page generated by the above code. Like a cdn or something. That system would think the web page is constantly updated as the code of the HTML page would be different each time it accessed it, leading to lots of redundant work if it has to update caches, check outbound links and so on.
- eyelidlessness 10y agoIf a caching thing doesn't understand that order of HTML attributes doesn't matter, I don't want to be using that thing.