# What has been the worst Julia production code you have written?

**URL:** https://discourse.julialang.org/t/what-has-been-the-worst-julia-production-code-you-have-written/60524
**Category:** Offtopic
**Created:** [May 4, 2021, 3:13pm UTC](https://discourse.julialang.org/t/what-has-been-the-worst-julia-production-code-you-have-written/60524 "2021-05-04T15:13:35Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![rikh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rikh/32/204104_2.png) [@rikh](https://discourse.julialang.org/u/rikh)
#### Post date: [May 4, 2021, 3:13pm UTC](https://discourse.julialang.org/t/what-has-been-the-worst-julia-production-code-you-have-written/60524/1 "2021-05-04T15:13:35Z")

</div>

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:
> 
> ```julia
> 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
> 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
> 
> ```julia
> 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](https://docs.julialang.org/en/v1/stdlib/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](https://julialang.org/blog/2020/08/invalidations/). 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
> 
> ```julia
> 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.

---

<div class="post-metadata">

### Author: ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)
#### Post date: [May 4, 2021, 3:55pm UTC](https://discourse.julialang.org/t/what-has-been-the-worst-julia-production-code-you-have-written/60524/2 "2021-05-04T15:55:40Z")

</div>

This:

```julia
data = vcat(map((a, b, c, d, e, f)->
                hcat(a, b, c', d', e', rad2deg(f[1]), rad2deg(f[2]), f[3]/1000),
                gps_time, utc_time, r_TOD, r_PEF_ace, r_ECEF, wgs84)...)

```

😃

---

<div class="post-metadata">

### Author: ![rikh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rikh/32/204104_2.png) [@rikh](https://discourse.julialang.org/u/rikh)
#### Post date: [May 4, 2021, 4:10pm UTC](https://discourse.julialang.org/t/what-has-been-the-worst-julia-production-code-you-have-written/60524/3 "2021-05-04T16:10:54Z")

</div>

I‘m affraid that I can’t follow. What does your code do exactly and why is it a bad idea? @Ronis_BR

---

<div class="post-metadata">

### Author: ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)
#### Post date: [May 4, 2021, 4:27pm UTC](https://discourse.julialang.org/t/what-has-been-the-worst-julia-production-code-you-have-written/60524/4 "2021-05-04T16:27:41Z")

</div>

It takes a lot of vectors of vectors and tuples and create a matrix to write to a file using `writedlm`.

It is a bad idea because it is ugly and nobody can understand 🙂

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [May 4, 2021, 4:31pm UTC](https://discourse.julialang.org/t/what-has-been-the-worst-julia-production-code-you-have-written/60524/5 "2021-05-04T16:31:04Z")

</div>

> [@rikh](#):
>
> I can’t follow. …why is it a bad idea?

It seems you answered your own question. 😃

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [May 4, 2021, 4:34pm UTC](https://discourse.julialang.org/t/what-has-been-the-worst-julia-production-code-you-have-written/60524/6 "2021-05-04T16:34:31Z")

</div>

I wrote a piece of code that used `collect`:

> [@How to use string substitution in variable names](https://discourse.julialang.org/t/how-to-use-string-substitution-in-variable-names/55192/14):
>
> Faster an shorter:
> 
> ```julia
> collect(eachcol(diagm(ones(7))))
> 
> ```

😔

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [May 5, 2021, 7:10am UTC](https://discourse.julialang.org/t/what-has-been-the-worst-julia-production-code-you-have-written/60524/7 "2021-05-05T07:10:59Z")

</div>

I think I could find about 3-5 examples from the code I write every week.

I find it _liberating_ to write bad code on a first iteration and not care. Best is the enemy of good, thinking about the perfect way to code something breaks the flow. I usually try to improve stuff before I merge a PR.

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [May 5, 2021, 7:21am UTC](https://discourse.julialang.org/t/what-has-been-the-worst-julia-production-code-you-have-written/60524/8 "2021-05-05T07:21:20Z")

</div>

Probably this version of Ranges.jl: [GitHub - mkitti/Ranges.jl: Additional range syntax for Julia](https://github.com/mkitti/Ranges.jl)

It breaks all the type piracy rules, but it was fun to see what was possible.

---

<div class="post-metadata">

### Author: ![rikh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rikh/32/204104_2.png) [@rikh](https://discourse.julialang.org/u/rikh)
#### Post date: [May 5, 2021, 8:22am UTC](https://discourse.julialang.org/t/what-has-been-the-worst-julia-production-code-you-have-written/60524/9 "2021-05-05T08:22:48Z")

</div>

Yes, I agree.

I’ve updated the question since I was curious about production mistakes. So, where some piece of code seemed like a good idea but ended up breaking things.

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [May 14, 2021, 2:21am UTC](https://discourse.julialang.org/t/what-has-been-the-worst-julia-production-code-you-have-written/60524/10 "2021-05-14T02:21:02Z")

</div>

Found some code I wrote 4-5 years ago. Globals. Zillions of temporary arrays. Abstractly typed structs. It was otherworldly…

---

<div class="post-metadata">

### Author: ![FPGro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fpgro/32/20822_2.png) [@FPGro](https://discourse.julialang.org/u/FPGro)
#### Post date: [May 14, 2021, 9:16am UTC](https://discourse.julialang.org/t/what-has-been-the-worst-julia-production-code-you-have-written/60524/11 "2021-05-14T09:16:56Z")

</div>

Not exactly _production_ code because I have nothing that would qualify for this, but in the time between starting to learn Julia and actually understanding what I do (most of the time), I somehow came up with this beauty:

```julia
ntuple(t -> begin i,j = divrem(t+1,2); vals[i][j+1] end, 2*length(vals))

```

Which is a completely unintelligible and buggy way of expressing

```julia
Tuple(Iterators.flatten(vals))

```

(for a vector of 2-Tuples, that’s what vals is supposed to be)  
But I forgot what it was supposed to do and was afraid to change it for a long time because it looked so brittle, lol.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [May 14, 2021, 10:00am UTC](https://discourse.julialang.org/t/what-has-been-the-worst-julia-production-code-you-have-written/60524/12 "2021-05-14T10:00:33Z")

</div>

If Division II league posts are allowed here, the naive solution to [this integer decomposition problem](https://discourse.julialang.org/t/function-for-finding-additiion-decompositions-of-an-integer/60295/3), while simple and working fine for small N, its “algorithm” is so bad that the execution time increases exponentially with N (_compare it to Gustaphe’s turbo solution_).

A rough estimate is that the naive algorithm would take about 1 Million years to run for case N=100 in a normal laptop (_if it wouldn’t run out of memory long before that_).

_ **NB:** _  
_It would be nice to have a package that estimates the run time and disk space required for such massive problems, where complexity increases dramatically with some parameter N._

---

<div class="post-metadata">

### Author: ![anon60034542](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@anon60034542](https://discourse.julialang.org/u/anon60034542)
#### Post date: [July 22, 2021, 1:17pm UTC](https://discourse.julialang.org/t/what-has-been-the-worst-julia-production-code-you-have-written/60524/13 "2021-07-22T13:17:29Z")

</div>

What’s wrong with `collect()` in this case?

I’m still learning Julia, so I don’t quite see the problem.

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [July 22, 2021, 2:18pm UTC](https://discourse.julialang.org/t/what-has-been-the-worst-julia-production-code-you-have-written/60524/14 "2021-07-22T14:18:32Z")

</div>

`collect` materializes the results of an iterator. Using the iterator directly is usually sufficient and avoids the memory allocation.
