Is there anything we can do about the oft-experienced off-by-one
error? I've noticed that a steady stream of questions from people who
don't realize what the error means, and people who've made a lousy
mistake.
Yes, anyone can make a mistake with array indexing, just like anyone can get a flat tyre. Your car starts to make rumbling noises, tip over in one corner, steers badly and veers all over the place, what do you do?
1) Call a garage and say 'my car has stopped working'.
2) Get out, walk round spot the flat tyre, swear profusely. Change the wheel, use the emergency inflater bottle or call a local tyre place to send a truck.
A huge majority of OP's only know option (1). Why - because they cannot troubleshoot problems at all or just think it's someone else's job to fix everything from some vague 'help!' plea. All developers make silly errors with array indexing etc. The reason why professional and enthusiast programmers don't post such issues is that they're usually easily debugged and so get fixed.
Is there a canonical we can point these users to, along with quick
comment, or should we close these questions with some other reason?
I usually go with 'Unclear' [why you didn't just fix this with a trivial debug or by simply printing out stuff like array indices]
I thought maybe closing as a typo would be good for those who
immediately realize their mistake in comments, but what about those
fundamentally don't understand why they are off-by-one, those newer to
programming and iteration?
They are not professional or enthusiast programmers. If a user cannot handle array indexing, string operations, conditionals, boolean algebra and all the the other 'Computer 101' issues, they cannot effectively write programs, never mind test/debug/fix.
I see many, many duplicate (and sometimes LQ) answers all about the
topic of using <= length instead < length when iterating through a for
loop in JavaScript (and a myriad of other languages), or even
encountering IndexOutOfBoundsExceptions in Java, but since these are
somewhat tied to each language (with error messages and whatnot),
should we create a language-agnostic canonical? What's the correct
course of action to reduce duplicate answers and give new users and
in-depth reason to why off by one errors occur?
OP's should be aware of Obi-Wan and fencepost issues in the language of their choice before they get to the 'post SO question' stage.
We all make silly mistakes but, usually, they are easy to track down with very basic skills like printing out variable values in loops or [gasp!] using an actual debugger.
Just close them as 'Unclear' or 'Too broad', whichever seems most appropriate: the OP's cannot program computers and should stop trying until they have demonstrably attained a minimal level of skill in troubleshooting and/or debugging.