0

There are a number of simple errors that beginners frequently make in many different languages. Examples include:

  1. Using a loop to calculate a result, and putting the print statement to show the result inside the loop instead of after it's done.
  2. Initializing an accumulator variable in the loop instead of before it.
  3. When using a variable that should persist state between calls to a function, declaring it inside the function instead of globally.
  4. (variation on #3) Re-declaring a variable from an outer scope in an inner scope, rather than just assigning it.

Questions due to errors like this come up numerous times every day (how to print sum of numbers between any given numbers using loop python is a recent example of #1). But searching for previous questions to use as a duplicate is difficult, since there's no search criteria that's likely to find them.

I feel like we could have a language-agnostic, FAQ-style community question that demonstrates all these errors along with the (fairly simple) fixes. Then we could close most of these questions as duplicates of this question. The title could be something like "What are some common logic errors and how can they be fixed?"

Would this be reasonable to start?

32
  • There is already What is an off-by-one error and how do I fix it? which is language agnostic and deals with loops of the sort for (int i = 0; i <= arrayLength; i++) and thus go out of bounds of the array indexes. I feel it's in the same vein of what you propose, so we have a precedent.
    – VLAZ
    Commented Dec 9, 2021 at 19:50
  • 5
    Are you suggesting a single FAQ that covers many of these basic errors? I feel like that would be too broad, and would basically be an RTFM target. One canonical per question is much more reasonable, even if it's language-agnostic.
    – cigien
    Commented Dec 9, 2021 at 20:01
  • 1
    @VLAZ I almost always VTC those as typo. I know it's technically a logic error, but it's so trivial. But I'll save that dupe.
    – Barmar
    Commented Dec 9, 2021 at 20:19
  • @cigien Yes, I'm suggesting a single FAQ, along the lines of What does this regex mean and What does this symbol mean in PHP. Each individual error seems too trivial for its own question, so I thought a collection in one place would be easier.
    – Barmar
    Commented Dec 9, 2021 at 20:23
  • 3
    @cigien I'm fairly sure it was a suggestion for one-per-problem. Doesn't really make sense otherwise. EDIT: I guess I was wrong. Then I support the one-per-problem. Similar to the off-by-one Q.
    – VLAZ
    Commented Dec 9, 2021 at 20:23
  • Ah, in that case, I don't think it's a good idea. I don't know about the PHP question you linked, but the regex one is definitely an RTFM target, and actually using such a broad question like that in any tag other than regex would be frowned upon (why regex is allowed to be an exception, I don't know, but that's a separate issue). I don't really see the problem with a canonical for each specific problem. Even if they're trivial, that's fine; SO is full of highly specific, basic, questions, and I think that's fine.
    – cigien
    Commented Dec 9, 2021 at 20:26
  • 2
    Also Definitive List of Common Reasons for Segmentation Faults. There are quite a few "kitchen sink" questions like these.
    – Barmar
    Commented Dec 9, 2021 at 20:31
  • 1
    True, but they're not good targets. FWIW, at least one moderator has stated clearly that questions like these should not be used as targets, unless the original question is something like "what are some reasons for segmentation faults?". If a question shows some code that has a segfault, and asks how to fix it, closing it with that target would be considered an RTFM closure, and is frowned upon.
    – cigien
    Commented Dec 9, 2021 at 20:48
  • Yeah, now I know what you mean. I feel similarly when I see people use PHP parse/syntax errors and how to solve them or "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP as dup targets. The OP will hardly ever be able to figure out where their problem is in those collections.
    – Barmar
    Commented Dec 9, 2021 at 20:52
  • I deliberately tried to come up with a title that wouldn't try to categorize the poster (even though we know that this is all newbie stuff).
    – Barmar
    Commented Dec 9, 2021 at 21:11
  • 2
    I think if you saw somebody asking about these four things, you would probably vote to close it as "needs more focus", and it would be correct. I don't think we should leave close-worthy questions open just because the person who made them intended for it to be a canonical dupe-target. It could also give newcomers the wrong idea about what questions are appropriate here. One canonical question about each issue would be fine.
    – kaya3
    Commented Dec 9, 2021 at 23:23
  • @kaya3 These types of questions generally identify themselves as generic answers, not real questions. I wrote one a few years ago: stackoverflow.com/questions/42913798/…
    – Barmar
    Commented Dec 9, 2021 at 23:29
  • @Barmar That's a good Q&A to be a dupe target for mistakes of your type #1. I think it would be made significantly less useful as a dupe target if it also explained three other types of mistake and how to fix them, since most questions exhibiting this mistake will not exhibit the other three.
    – kaya3
    Commented Dec 10, 2021 at 1:37
  • @kaya3 It has nothing to do with type 1 errors. The only similarity is that they involve looping.
    – Barmar
    Commented Dec 10, 2021 at 1:39
  • 1
    While we’re at it: in JS there is What does this symbol mean in JavaScript?, and there’s also What is the difference between the = and == operators and what is ===? (Single, double, and triple equals) for the good old if(variable = value). Commented Dec 10, 2021 at 3:03

3 Answers 3

5

(Originally posted as comments, converting to an answer)

My intuition from teaching beginner programmers is that your four types of mistakes are caused by different (but perhaps related) misconceptions. Two are purely about control flow, while the other two indicate misconceptions about variable lifetime. Yes, the answer is mechanically the same (declare/initialise/print the variable in a different place) but I think a single canonical "what is the correct place to declare/initialise/print a variable?" question would be far too broad, let alone "what are some common beginner mistakes and how to fix them?".

The more different common mistakes you try to address in a single Q&A, the less useful that Q&A is as a dupe target for a question exhibiting just one of those mistakes, because the novice programmer whose question you're closing will not necessarily find it easy to figure out which mistake from the big list is the one they made. On the other hand, a single "big list of common mistakes" Q&A does make it easier for high-rep users, since they won't have to remember a "big list" of dupe targets; but I think that's optimising for the wrong thing.

A better solution in my opinion is to have separate Q&As for each common mistake, but we need a good way for high-rep users to be able to find the right dupe targets without having to memorise all of them. There are lists of the most common dupe targets in some tag wikis, e.g. and . I'm not aware of a similar list for language-agnostic Q&As about logical mistakes like those in your post; it doesn't seem that useful to list them on the tag wiki, so perhaps a Q&A like "What dupe targets should be used for common logical mistakes by beginners?" on meta would be the best option.

3
  • 1
    Note that my usual solution to the second paragraph is to add a comment with the trivial fix -- the dupe closure is mainly just to cut off people trying to post answers.
    – Barmar
    Commented Dec 10, 2021 at 3:10
  • But that may just be me -- I notice lots of other SMEs duping to RTFM questions without helping the OP narrow down to the specific problem (this happens especially with PHP syntax errors and undefined variable errors -- I find them overused).
    – Barmar
    Commented Dec 10, 2021 at 3:14
  • 3
    "There are lists of the most common dupe targets in" the frequent tab of every tag of the site, ex. python stackoverflow.com/questions/tagged/python?tab=Frequent
    – Braiam
    Commented Dec 10, 2021 at 17:40
4

So if the point of these questions would be some canonical we could use to then close the questions down...I'm going to volunteer that a dozen of those already exist.

The tricky thing is to get the SMEs to both find them and agree to close them as a duplicate. We've had a lot of friction accomplishing this before and I don't think that's likely going to change anytime soon.

Even if you give the SMEs exactly one question to use to close it down, there's a good chance that the question would have had three answers before you could blink.

4
  • 1
    The problem of "three answers before you could blink" happens all the time already, except for some of the most well know dupes that the SMEs usually catch early (e.g. "How to return a result from an async function")
    – Barmar
    Commented Dec 9, 2021 at 21:58
  • 3
    Closing a question doesn't even always prevent people subsequently posting answers.
    – khelwood
    Commented Dec 9, 2021 at 22:02
  • 1
    @khelwood And an answer being good doesn't protect it from downvotes for being unhelpfully posted on a blatant duplicate... Maybe if enough users downvoted the answers they'd eventually stop getting posted. Commented Dec 10, 2021 at 0:20
  • 2
    frankly speaking, these types of FAQs is where we could use the format of articles but brought to the whole public platform - they could contain pointers to one problem per question (just like the regex one) without the usual hassle of discussions of suitability of such a Q&A. Commented Dec 10, 2021 at 12:51
1

No, for this we have canonical questions, that are used to mark duplicates, which such beginners questions often are.

So it would be better to use a question as canonical, if such doesn't exist to a topic and add knowledge to it, which would be like a FAQ.

but in reality people often don't understand the basic concepts and so couldn't understand the canonical question or the FAQ

You must log in to answer this question.

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