by gavinpc · 12y ago · 0 comments
I made a bug once, and I need to tell you about it. So, in 2001, I wrote a
reference library for JSON, in Java, and in it, I had this line
private int index
that created a variable called "index" which counted the number of characters in
the JSON text that we were parsing, and it was used to produce an error message.
Last year, I got a bug report from somebody. It turns out that they had a JSON
text which was several gigabytes in size, and they had a syntax error past two
gigabytes, and my JSON library did not properly report where the error was — it
was off by two gigabytes, which, that's kind of a big error, isn't it? And the
reason was, I used an int.
Now, I can justify my choice in doing that. At the time that I did it, two
gigabytes was a really big disk drive, and my use of JSON still is very small
messages. My JSON messages are rarely bigger than a couple of K. And — a
couple gigs, yeah that's about a thousand times bigger than I need, I should be
all right. No, turns out it wasn't enough.
You might think well, one bug in 12 years you're doing pretty good. And I'm
saying no, that's not good enough. I want my programs to be perfect. I don't
want anything to go wrong. And in this case it went wrong simply because *Java
gave me a choice that I didn't need, and I made the wrong choice*.
[0] https://www.youtube.com/watch?v=bo36MrBfTk4&t=38mEDIT: is there a reference for formatting comments? I've never been able to find one.
Most programs written in the real world (enterprise-y Java apps) do not need strong control on GC, choice of integer types, or many other things offered to them. Reducing choice will increase code/tool quality.
I think that we should make the uncommon choice reallllly hard to put into place. Make it a pain to configure the GC, give specific integer types really long names. Just stop people from premature optimization and leave these tools to people who know what they're doing.
Doesn't sound very humble, this "HO".
Do you know how many programmers that run circles around you (and me) have done mistakes similar to what Doug describes?
Data manipulation beyond "pull out of database" or "submit user input to database" is a lot rarer in enterprise software like this than in scientific computing. I'm not saying it's bad to be aware of it, but software is more than numbers.
As long as you are concerned with adding a bit of eye candy and interactivity to a web page this may be true enough to get away with the JavaScript way of making every number a double precision floating point number but there a other domains where this will not fly. And even in the world of JavaScript asm.js is trying hard to overcome this limitation.
Sure, and I believe everybody, including Doug, knows this not so subtle distinction. He wasn't talking about programming HPC for NASA.
One the one hand "implicit int" are being phased out in favour of explicit int size. Your variables cannot be `int` anymore, you have to sit down, think and choose: u8? u16? u32? i32? u64? i64? This avoids all the pains of programs behaving differently or crashing when compiled on different architectures.
On the other hand, a new "native integer for sizes that do not matter, minimum 32 bits" is being brewed, for example for pointer offsets or collection sizes. The idea is that you will not be able to have a collection with more of 2^32 elements in a 32-bit architecture nor more than 2^64 in a 64-bit architecture.
After this discussion, my hope is to see the introduction of a fast-ish dynamic bigint (that starts native and grow up to 256 or 512 bits) that can be used in all the cases where you do not care about the exact size type, yet you want to be future-proof (this `private int index` fits this case, IMO).
[1] http://discuss.rust-lang.org/t/if-int-has-the-wrong-size/454... [2] https://github.com/rust-lang/rfcs/pull/464
It's only important for things like pointers that they match the size of the addressing space, and not even that is a very hard constraint, just a very convenient one.
However, using a 32 bit number to store values that will never be larger than 16 bits isn't that bad, it's just very slow.
However, an overflow to floating point isn't necessarily an improvement because, while a float will hold bigger numbers, it does so with limited precision and sometimes that lack of precision will cause bugs too. Probably more often, in fact.
In the example given it wouldn't be so bad, but you'd only get an approximate indication of where the error occurred rather than a specific line/character. So of course, whatever is reporting the error would now need to understand and handle the much more complex scenario of "fuzzy" location information instead of a simple unique index to a specific character. Depending on what it then needs to do with that information, the complexity could spiral from there.
If you want to just have things work no matter what, you have no choice but to use bignums. I was wondering about this recently, so did some benchmarks in Clojure. The performance was horrible, so frankly this is still not a viable alternative. Maybe in 10 years time, if every CPU has a bignum coprocessor by then.
Also, there are times, particularly in low-level graphics programming or cryptography, where you actually want integer modulo arithmetic, or to be able to do bitwise booleans predictably. In those cases, JavaScript-style loose typing can be a huge pain.
BTW, I've been a big advocate of JavaScript for about as long as Doug Crockford, so my point isn't that JavaScript-style type handling is bad: just that it's very far from a silver bullet.
What if Java gave an arguably more useful choice--whether to use a signed or unsigned integer?
Why is it even a question? Imagine a JSON of all the people on Earth.
And I certainly don't believe there are programmers making only 1 mistake for 12 years. I believe he's just making a joke, or using the example as a means to an end.
*Java gave me a choice that I didn't need, and I made the wrong choice*If you're doing compression you should use some sort of raw bytes type.
At least I think that's the platonic ideal.
IMHO Java makes some choices about safety. If you don't agree with those choices, use a different tool. It doesn't make Java wrong for having a different opinion. Likewise I wouldn't berate C for being too low level or Ruby for favouring readability over performance.
========
What kind of formatting can you use in comments?
http://news.ycombinator.com/formatdoc
========
Was that what you were looking for?