What is the XY problem?
When asking questions, how do I recognize when I'm falling into it? How do I avoid it?
What is it?
That is, you are trying to solve problem
This can lead to frustration by people who are trying to help you solve the problem because by the time you ask about it, the solution that you need help with might not have any obvious connections to the problem that you are trying to solve.
How to Avoid
To avoid falling into this trap, always include information about a broader picture along with any attempted solution. If someone asks for more information, or especially a more specific question, do provide details. If there are other solutions which you believe will be suggested and which you've already ruled out, then don't try to avoid going over them again – instead state why you've ruled them out, as this gives more information about your requirements and helps others provide better answers.
A recent IRC conversation for illustration:
This is easier to do in a fully interactive chat (regardless of what mode), but on a SE site, where you polish a post a bit, post it, and then have 5-30 mins, or longer, before feedback, it really helps to head in the right direction from the start.
The X-Y Problem, as it is sometimes called, is a mental block which leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help. It often goes something like this
The problem occurs when people get their train of thought stuck on one approach and become unable to take a step back. Remaining open to having a new look at the bigger picture, these people might find their way back to X and continue searching for alternative solutions.
I'm adding an extra answer as a complement to the excellent answers already present.
An XY problem seems to be a subset of the Einstellung effect, where a problem-solver gets stuck on a particular solution and is unable to backtrack mentally to see potentially superior solutions. This psychological phenomenon affects everyone, novices and experts alike.
An example from Chess would be a person winning with a 5-move smothered mate in one game. The next game, the same player fails to see a faster 3-move mate because they are stuck on the idea of a 5-move mate.
Specific to Q&A, the perniciousness of an XY problem comes from the fact that it is frustrating for everyone involved:
The Missing square puzzle problem is a good illustration. It illustrates a simple and universal problem/question/solution process, where "some illusion" causes complications.
There are a self-evident problem — the 1×1 hole in that arises on the second figure —, but the solution is evident only after we know (like an Egg of Columbus)... All people, experts and non-experts, agree that there is a problem.
The clever geometry-expert thinks in terms of "similar geometries that aren't perfect equivalents".
The normal user thinks, mistakenly, in terms of exact congruence. So, the use of wrong hypothesis, produce ill
The "XY problem" as a specialization of the "wrong hypothesis behaviour"
You want to solve the real question-
So, "XY Problem" is only another (more specialized) term to say "Use of wrong working hypothesis".
Avoiding the XY problem
I argue that you can't avoid it. Not without simply throwing your program requirements at SO and asking them to do your design for you (not recommended).
I argue this because the design process for all software is based on a starting set of requirements "A". From there you say "I can achieve A if I do B and C". From there you say "I can achieve B if I do D and E and I can achieve C if I do F and G". And that continues to the point we say that "I can achieve X if I do Y". We usually do this so fast we don't even think about the process.
So the major problem with the XY problem is that Y is not possible, but you don't know how much of your design to unwind to get back to the X which is possible. You usually don't even know that Y is impossible without actually asking. You don't know what you don't know. Therefore it is unavoidable
Asking Questions where you risk falling into XY
The best thing you can do about the XY problem is to guard against it when asking questions. Still ask the same question but give as much pertinent information as possible:
This will help people identify that its XY and help you much more quickly.
IMPORTANT: Giving Answers to XY problems
In my opinion the biggest problem with XY questions is the (frequently) unhelpful answers they provoke. We will never stop people asking these questions so the best thing is to understand how we can answer them quickly and effectively.
Ironically a lot of these bad answers and responses are given by those wanting to be the most helpful and can be given by some of the most reputable people on the forum / SO.
I've discovered a method of answering these questions which appears to help get round the psychology associated with XY problems and lead the OP of a question to a working solution. The method takes a little longer to answer in the first instance but closes the Q/A loop much more quickly.
I suggest that you answer the question in three parts and give them in the following order.
Most people are here to learn so parts 1 and 2 of this answer are as important as part 3. But too often part 3 is given on it's own and it is extremely frustrating and patronising to the OP not to mention a lot of OPs will not accept the answer.
Giving this answer also avoids embarrassment when you think the OP has an XY problem when in fact they don't. All you've done is give a little extra information. Simply giving part 3 risks appearing to have not read the question.
Incidentally. Read the question again and read this answer... notice the three parts?
(separated from my other answer because this one has more opinion than explanation)
If you agree that the "XY Problem" is only another (more specialized) term for "Use of wrong working hypothesis", as illustrated and explained here, and similar to the "Einstellung effect" explained here by @Jonathan Benn…
We can think in terms of some main situations:
So let's put focus on the normal situation.
The "XY problem" is a valid problem!
The Stack Exchange question is to show a problem. If my question helps to show that the real problem is my wrong hypothesis, it is OK! It is a first step, and perhaps I will not need any other questions after obtaining the correct hypothesis (and trying to solve by myself).
Example. See my rushed question about "self axis that fails"; the real problem is that
Negotiating a union contract...
The XY problem is relevant to the difference between "interest" based bargaining (X) and "position" based bargaining (Y).
X = the employee's overall take-home pay has been stagnant for the last several years, while their cost of living continually increases; the employee needs more take-home pay.
Y = picking "you will not raise parking fees" as a hill to die on / the only "solution" to X that the employee is willing to accept.
Aiming for Y (taking a position) is aiming for a very specific and limited solution to the problem (X). It cuts the employee off from from the universe of solutions to the problem (X) by insisting that it only be solved in one very specific and limited way. If that way (Y) is, for whatever reason, objectionable to the employer, then there will be a stalemate -- a period of time where there is no solution to X, where a solution has to be acceptable to both sides.
If the employee can aim for X instead of Y, then the universe of solutions remains open / unrestricted, and the employer can be recruited to help find possible solutions to X. (Maybe it will turn out to be OK with the employee to raise parking rates, in exchange for a cost of living raise...)
This interest/position paradigm seems relevant to asking questions on SO, when OP asks for help making their position work. Sometimes you can cut to X by simply asking: "Why are you trying to do this?"; But I wonder if sometimes, aiming for Y isn't just as valuable. How many of us have ever tried to do something, just to see if we could? Anyone here running their own DNS server with some specific type of software for that very reason? :-) Might not be your best solution to X, but it's still interesting...