Just a tongue in cheek topic. What has been the worst Julia production code you have written?
I kick it off with some of my recent “favourites”.
Converting a HTTP.Response to string
What is wrong with the following code? Looks good, I thought:
using HTTP
function obtain_unauthorized(url)
[...]
r = HTTP.get(url, headers)
string(r)
end
The problem is that the output of r
is truncated when a long response is converted to string:
julia> using HTTP
julia> r = HTTP.Response(200, string(zeros(1000)));
julia> s = string(r)
"HTTP.Messages.Response:\n\"\"\"\nHTTP/1.1 200 OK\r\n\r\n[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,\n⋮\n5000-byte body\n\"\"\""
Note the ⋮
. So, my code worked as long as the response from the server wasn’t too long. Obviously, servers can send very long responses and, eventually, it all broke down when trying to parse the “stored” user data as JSON and again when trying to store the authentication for users.
Serializing strings of objects to a file
So, suppose you have a nice vector of pairs where pairs look like
Pair{AbstractString, AbstractString}("a", "b")
and you need to write this to a file and read from it again. Of course, you can use Serialization but that isn’t guaranteed to be stable over different Julia versions. Therefore, you just write this to a file and use x = Parse.eval(line)
for each pair in the document. Super great idea; now Julia will give you super fresh methods, because a lot of methods are invalidated. Loading your package will take considerably longer due to this.
Writing to a database via a global variable
So, the database can handle concurrent writes, so as a developer you don’t need to think anymore, right? Well, if you conveniently add
const CONN = db_connect()
to your module, then it will work just fine during basic tests. Unfortunately, when real users start using your webserver at the same time, things will go haywire and you will, again, lose data.
I’m very curious what other great ideas other people have had, which turned out not to be so great
EDIT: Added “production” to the question.