26

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. 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 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? 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?

12
  • 15
    There are (at least) two questions in one here: "What to do about common error messages?" and "What to do about common programming mistakes?". Commented Aug 6, 2018 at 5:06
  • 11
    Seems like a typical "people don't search" problem, creating even more generic canonicals is not going to solve that problem. In my experience errors are prime suspects, as people fear/hate them rather then see them as their best friends who are in the game to tell you what's wrong and where to look. You can see this happening as people asking questions about them are generally in the market to make them go away rather than solving them. They bypass the necessary first job before you ask questions: figure out what causes the error. "This error happens, how to get rid of it?"
    – Gimby
    Commented Aug 6, 2018 at 9:35
  • Agree with @ivan_pozdeev here, this is two questions in one, and maybe needs splitting. Both questions need a decent answer to them, so best not to specialise on just "off-by-one" questions. Solution to the general questions may well be the best solution to the specific questions. On another note, shouldn't closing a question as a "typo" be limited to real typos? A regular programming mistake isn't just a typo, it's a common issue for a lot of programmers (novice or expert), and may need a more specific category. Commented Aug 6, 2018 at 9:36
  • If I can quickly find a good question in the same language, I'll VTC as a duplicate, otherwise I'll often just comment and move on. I've seen others VTC as a "can't be reproduced / simple typographical error", which I can see the reasoning for but doesn't sit quite right with me (it's generally not a typo, it's a misunderstanding.) Commented Aug 6, 2018 at 9:39
  • 1
    @Gimby "people don't search" can't be solved, but "we close the typo questions and reduce number of LQ answers" can.
    – user202729
    Commented Aug 6, 2018 at 9:40
  • @user202729 yes, but that doesn't solve any problem, which is the steady stream of low quality questions that are at the root of the low quality answers.
    – Gimby
    Commented Aug 6, 2018 at 11:02
  • @Gimby Avoiding LQ questions is another (waaaay harder) problem.
    – user202729
    Commented Aug 6, 2018 at 11:09
  • 1
    Unfortunately, if we cut out all the low-quality homework-dumps etc, the questions /views/adRevStream drops by ~90% and we lose SO. I have nuthin' :( Commented Aug 6, 2018 at 11:23
  • 3
    Obi-Wan questions are jost one aspect of the general lack of troubleshooting and/or debugging skills. If a car goes forwards in 1,2,4,5 gears, but not in 3rd, everyone knows that the gearbox is bust. If an array[5] indexing loop goes round 6 times, everyone should know that an OOB access will happen. So many don't, because they don't know how to drive their computer in any gear:( Commented Aug 6, 2018 at 11:31
  • 1
    @MartinJames - What's an "Obi-Wan question"? Is it just a slightly-amusing way of saying off-by-one? Commented Aug 6, 2018 at 15:15
  • @T.J.Crowder very likely just another problem of programmers.
    – Braiam
    Commented Aug 6, 2018 at 15:16
  • 7
    I'm not usually two to favour a strict approach but I say we close every single zero of them.
    – Pekka
    Commented Aug 6, 2018 at 15:34

3 Answers 3

15

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.

12
  • 16
    In an ideal world, yes, vote to close, downvote and move on. What happens, unavoidably, is 5 close votes can't be accumulated before a rep-farmer posts a one-line answer. I don't think "Unclear" is a good workaround as I can't see 5 people ever (timely) choosing this option for "failure to debug".
    – jpp
    Commented Aug 6, 2018 at 12:12
  • 1
    Agree with jpp here. And I don't think it's fair to say they aren't enthusiast programmers nor to assume they are supposed to know all the basics (how often does that even really happen?) since a majority of askers are low-rep newcomers. In an ideal world, I would expect each user to have basic knowledge about what they're doing, but SO is magnitudes less than ideal.
    – Andrew Li
    Commented Aug 6, 2018 at 12:28
  • 16
    @Li357 well, until SO takes 'Stack Overflow is for professional and enthusiast programmers, people who write code because they love it' off the Help/Ask page, then I will continue to assume that SO users are obliged to 'know all the basics' to the extent that they can easily look them up without involving an SO question. Commented Aug 6, 2018 at 12:40
  • 2
    Not sure about "unclear". We judge the question, not OP's intention. It's unclear why OP can't "just" debug it, but it has nothing to do with the question itself. I'd go with typo.
    – user202729
    Commented Aug 6, 2018 at 13:35
  • 5
    Hold on a second. Since when have off-by-one questions been explicitly off-topic? If the question is well-framed and otherwise on-topic, why should they be closed because they're an off-by-one question? Are you using a close vote as a super downvote?
    – Makoto
    Commented Aug 6, 2018 at 14:55
  • 1
    "swear profusely" I think this is a continuous action, instead of a verb. Is more accurate to say "while swearing profusely"
    – Braiam
    Commented Aug 6, 2018 at 15:18
  • 1
    Why not "a problem that can no longer be reproduced or a simple typographical error"? That's essentially the "OP made a trivial mistake and missed it" close reason.
    – jpmc26
    Commented Aug 6, 2018 at 17:17
  • 2
    @MartinJames You don't have to know all the basics to be an enthusiast re: programming by any stretch of the imagination. Not to mention that the more you know (like when someone has 20k+ reputation on SO from over 1300 answers), the bigger and more nuanced the bucket called "the basics" becomes. What's basic for you may not be learned by a beginner until they've been programming for a year, or more. Especially in the days of self-taught programmers.
    – TylerH
    Commented Aug 6, 2018 at 19:19
  • @jpmc26 Typo is a tough call for me. On the one hand, I don't agree that questions where OP wrote wrong code counts as a typo usually because that's not what 'typo' means (an error in transcription); they'd be just as likely to write the code that way if they tried to make a copy of their code. On the other hand, I think problems arising by simplistic things like not validating your code (in the case of HTML or CSS, for example) or saying until x > 6 when you code only runs 5 times should be close-worthy questions, even if they're not "typos".
    – TylerH
    Commented Aug 6, 2018 at 20:24
  • 4
    @TylerH These used to be closable as either "Lacks minimal understanding" (= OP has no idea what they're talking about) or "Too localized" (= has no lasting value because there's not really anything to be learned from it that you can then go and use in other situations). I'm more and more seeing why users were upset about the changes to close reasons, as it's now a lot harder to justify closing utter garbage.
    – jpmc26
    Commented Aug 6, 2018 at 20:33
  • @jpmc26 Yep, I wish we still had "Too localized". I don't miss "Lacks minimal understanding" or "Not a real question", though.
    – TylerH
    Commented Aug 6, 2018 at 20:51
  • @Braiam A verb can be a continuous action. A verb is simply a "doing word". "Swearing" is a verb used in present progressive tense. But I did understand what you meant and I'm nit-picking because I'm bored and sick of struggling through this PHP problem at work :P
    – Clonkex
    Commented Aug 7, 2018 at 0:47
3

This does sound like we'd benefit from a canonical, but the problem is that off-by-one errors aren't entirely generic.

There are several instances in which you can get an off-by-one (or off-by-several), none of which really relate to each other.

  1. Arithmetic computation or display of a value (displaying the numeral zero as the first value in a numbered list because they didn't realize their loop started at zero)
  2. Numerically indexing and/or accessing a data structure (most common when translating between certain languages; Lua is 1-based whereas most sane languages start at 0)
  3. Time-based computation or display of a value (which is a separate bag of worms to arithmetic, even if they relate to each other, due to the wonderful and sane world of timezones and the myriad ways that time is handled across libraries or services - for instance, the difference between SQLite date arithmetic and PostgreSQL date arithmetic)

So if we were able to overcome those problems and find a way to craft that into a single canonical, I think we'd be okay.

Except...I'm not sure we should for two reasons.

  • Generifying all of the different off-by-one scenarios is fundamentally impossible. You're going to want to create a canonical for each category of off-by-one scenario.
  • As you mentioned, there are differences across languages (and libraries and services) which make tying all of the loose ends up into one compact and tight package a much more daunting task than what it's worth.

So, what do we do?

  • Create a canonical for the language and scenario that you're experiencing this the most with, and start closing questions as a dupe of that.
  • If the question is truly off-topic (e.g. is too broad, or is a simple typo), then vote to close it. Do not close the question just because it's asking about an off-by-one error, since if the question is otherwise on-topic, then it should not be closed.
3
  • 1
    @pnuts: Documentation covered the easy stuff, so...no.
    – Makoto
    Commented Aug 6, 2018 at 16:53
  • @pnuts: Every time I've seen off-by-one manifest, there were different root causes. I've only highlighted the ones I could remember, but there are more subtle and devious ways that this can happen. The problem space is deceptive since everyone believes it to be easy. But that's the lie that we were all told.
    – Makoto
    Commented Aug 6, 2018 at 16:56
  • "Do not close the question just because it's asking about an off-by-one error". But if you have found or created a canonical answer that covers the relevant language and scenario, wouldn't you say that it should be closed (as a dup of that canonical question) without any further thought?
    – abarnert
    Commented Aug 6, 2018 at 22:50
-1

From what I understand of this issue, you are referring to people who ask questions that could've been found through a more thorough, and slightly more abstract, search.

However, as someone who is starting up with a new language at work and feeling totally lost, the hardest part of this is when I don't know what I don't know. So, when I ask questions and search I'll often make tons of connections and so that eliminates a lot of other questions, and increases my ability to ask the right questions. I would HOPE that Stack Overflow would want to nurture this curiosity in people. It is truly what makes good programmers, but it needs to be cultivated for sure.

2
  • 1
    Problems easily solved with a good book or schooling. Stack Overflow is absolutely not involved in people's educational status or how good of a programmer they are, it is only a repository for questions and answers; a very specific subset of questions and answers at that. That is why as a user base, we so dogmatically keep hammering on the content. Upvoting, downvoting, flagging, editing, closing, deleting, placing bounties, discussing required changes in comments, discussing on meta about how to better do all the things I just mentioned. Repository maintenance.
    – Gimby
    Commented Aug 7, 2018 at 14:38
  • Stack Overflow isn't an arbiter of actual experience, and of course content should be strictly curated and fussed over. however, to suggest that this should never happen is also a bit idealistic. People don't know what they don't know, and SO is the first stop for those who don't. Most will figure out quickly that doing lots of research and then posting will be most beneficial, but even now sometimes i'll post and then immediately think of some other way to research my issue.... it's all a process, and i'm grateful to SO for helping me along the way! keep fighting the good fight! =)
    – pedron
    Commented Aug 7, 2018 at 18:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .