Bad array indexing is considered deadly

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Wed May 31 19:50:41 PDT 2017


On Wednesday, May 31, 2017 22:33:43 Nick Sabalausky  via Digitalmars-d 
wrote:
> On 05/31/2017 05:00 PM, Steven Schveighoffer wrote:
> > On 5/31/17 3:17 PM, Moritz Maxeiner wrote:
> >> So in your specific use case I would say use a wrapper. This is one of
> >> the reasons why I am working on my own library for data structures
> >> (libds).
> >
> > That is my conclusion too.
>
> Honestly, I really think that if there is need to wrap something as
> basic as "all arrays in a codebase" then it's clear something in the
> langauge had gone horribly wrong.
>
> But short of actually *fixing* D's broken concept of Error, I don't see
> a better solution either.

Using an Exception to signal a programming bug and then potentially trying
to recover from it is like trying to recover from a segfault. It really
doesn't make sense.

Yes, it's annoying when you have a bug that kills your program, and even
when you do solid testing, you're unlikely to have found everything, but the
solution to a bug is to fix the bug, not try and have your program limp
along in an unknown state.

Yes, there may be cases where array indices are effectively coming from user
input, and you're going to have to check them all rather than the code
having been written in a way that guarantees that the indices are valid, and
in those cases, wrapping an array to do the checks may make sense, but in
the vast majority of programs, invalid indices should simply never happen -
just like dereferencing a null pointer should simply never happen - and if
it does happen, it's a bug. So, treating it like bad user input as the
default really doesn't make sense. Just fix the bug and move on, and over
time, such problems will go away, because you'll have found the bugs and
fixed them. And if you're consistently not finding them while testing, then
maybe you need to do more and/or better testing.

I can totally understand how it can be frustrating when a bug results in
your program being killed, but it's far better for it to be in your face so
that you find it and fix it rather than letting your program limp along and
potentially have problems later down the line that are disconnected from the
original bug and thus far harder to track down.

- Jonathan M Davis



More information about the Digitalmars-d mailing list