23

If I had a problem, and fixed it, but do not know WHY it was fixed, would that question be acceptable to ask?

10
  • 5
    I think it is totally acceptable, cause we want to learn and actually know the answer and not magically sort things out. its like debug with print you can do the job but not fully understand the solution
    – Asaf Itach
    Commented Mar 18, 2021 at 13:46
  • 7
    Generally, yes, but have you researched first? Commented Mar 18, 2021 at 13:46
  • @SebastianSimon Yes I have, and found nothing useful.
    – Maimas2
    Commented Mar 18, 2021 at 13:54
  • 26
    We do allow questions for explaining code. But please do make sure to explain what you understand about the code and what is unclear. Questions like "explain this <code dump>" are not easy to answer, as it's not clear what to explain - variable assignments? What a loop is? Something else entirely?
    – VLAZ
    Commented Mar 18, 2021 at 13:58
  • Not exactly the same, but relevant: meta.stackoverflow.com/a/253788/843953
    – pho
    Commented Mar 18, 2021 at 14:04
  • I think these kinds of why questions can be very valuable for developing a deeper understanding, especially if you didn't actually come up with the solution yourself. I ran into this myself, and tried to frame the question as "Is there a better way to do this, also why does this work?" In retrospect it would have been better to ask "Why does this work" as it's more focused and that's what I mainly wanted to know. For the curious: joomla.stackexchange.com/q/28085/8455 (not on stack overflow but hopefully it illustrates how these questions can be valuable)
    – Obscerno
    Commented Mar 18, 2021 at 16:57
  • 1
    @Obscerno sure but it helps if there is a very specific topic involved, like performance. If you have two methods and method A happens to work a lot faster than method B and you can't explain why... that would be very valuable to see explained. But just having a random code dump and asking why it works... a debugger also works to explain that to yourself.
    – Gimby
    Commented Mar 18, 2021 at 17:08
  • 1
    If you don't know why it was fixed, go back piece by piece until it is broken again. The last step on this way tells you why. (Opposite from where people usually come when doing a minimal reproducible example, but works like a charm as long as you keep a history to go back to.) Commented Mar 18, 2021 at 22:01
  • 6
  • @Gavin S. Yancey: I knew it wasn't just déjà vu. (For those who don't get it, I answered that, five and a half years ago.)
    – BoltClock
    Commented Mar 19, 2021 at 16:25

2 Answers 2

34

In theory, yes

There are no rules prohibiting this, and asking how code works, or why a specific approach works is on-topic here. There are some well received questions asking why a specific approach works.

In practice, it's not likely to be well-received

You apparently have a problem you solved yourself without knowing how you solved it.

That reeks of overcomplicated code, shotgun debugging, and no minimal, reproducible example. It's odd to be able to write working code without understanding how this code works.

If you truly have reduced the code to a fully reproducible example while using the minimal amount of code to reproduce it, and both the error and the solution are reproducible, but you still don't understand the problem, well, then something odd is going on, and it may be a great question. However, such questions are incredibly rare.

Do make sure you show your research, e.g. linking to the docs of the relevant function, explaining why you think it shouldn't work while it does work, etc. If you ask us to explain how something works, starting with what you understand about how it works allows us to fill the gaps, instead of just repeating the docs that explain the subject.

8
  • 9
    Shotgun debugging, ooh I like that. I was calling it "Throw random things at the wall and see what sticks", quite wordy.
    – Gimby
    Commented Mar 18, 2021 at 14:31
  • 17
    A few hours of shotgun debugging will always save a few minutes in the documentation!
    – zcoop98
    Commented Mar 18, 2021 at 15:28
  • 5
    @zcoop98 - Or worse, not save them. Commented Mar 18, 2021 at 16:20
  • 6
    I didn't know this term existed en.wikipedia.org/wiki/Shotgun_debugging Commented Mar 18, 2021 at 16:54
  • I know they stated they wrote it. The more common case I see is someone else's code/answer that OP wants explaining.
    – QHarr
    Commented Mar 18, 2021 at 16:55
  • @Gimby Another meaning to spaghetti code? It's done when it sticks.
    – QHarr
    Commented Mar 18, 2021 at 16:56
  • 1
    I imagine more the following: somebody asks for X, one answer says "try Y" and it's not obvious why Y solves X. Often enough there are code-only answers on SO that do not sufficiently explain a solution. Asking why Y solves X might be a natural follow-up question but one that basically would be better answered by extending the answer to the original question. Commented Mar 18, 2021 at 22:07
  • 5
    @Gimby I've always preferred the phrase "a stochastic approach to programming" - using buzzwords makes everything sound fancier. Commented Mar 18, 2021 at 23:11
1

I think that it is. I will put one of my questions as an example.

As long as you have a minimal example, and you show some effort from your part, i think to ask them is fine.

And they can be helpfull too, in my case, I got to the answer based on the comments, and learned something new, that wasnt really obvious for me.

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