I am working on a red-mine. I have needed to read through the code for part of the work. In doing so I have stumbled upon a line of code that seems wrong/illogical/satanic. Is there anything wrong with asking "Considering X, why is this code the way it is?" on SO?
What is meta?
- Any Stack Exchange user can ask a question
- The community provides support, votes on ideas, and reports bugs
- Your voice helps shape the way Stack Exchange operates
It's possible no one may be able to answer that question for you. We could possibly make guesses, but that might not be as useful as you might think. Especially if the problem was just bad code - and you know it's bad. Just fix it so you know it was right.
As Cody Gray noted in a comment here, though, I do see some potential value. The code may just look plain senseless to you, but perhaps there is a known (anti-)pattern that was being used, or there is just a common mistake being made which the person asking has not seen in someone else's code. If you don't understand why that code may have been written that way, I do think this could be useful, then.
I would probably present such a question in this general form:
By showing the way it is done in the existing code, and presenting how you thought it should be done, and why you think yours is better and the other is wrong - and asking a specific question concerning it, you'd be avoiding asking something where people would be guessing. As much, anyway...
Ultimately, the best way to find out why it was done that way may be to ask the person who wrote it. I know... good luck with that. And it may or may not be worth it to bother. I've seen some known-stupid code that I couldn't at all imagine what made someone think that was the proper way to do it... but!!
Also, I'd add that this type of question is most useful when you really think the 'bad' way of doing it is truly bad in some way, as opposed to just another alternate way to do it. The line between those can be quite dim, but you want to try to avoid, "I'm not sure which of these two seemingly-valid ways is correct", as you will tend to get opinions or, "it depends". But if you really think something is just flat-out bad, it could be worth asking. The question might still be "either one could work in certain circumstances", but at least you were not originally asking for opinions on two options.
I completely agree with Andrew's answer (essentially that a clear answer can rarely be given, but that doesn't mean it's necessarily a poor question), but one thing I'd like to point out is that often code can appear satanic if you're not used to the style and conventions of a particular language or even the accepted approaches/terms for a specific problem.
Because of this, a question asking if a snippet of code is unnecessarily obscure or simply adhering to a convention you aren't aware of (yet) is usually easy to give a reasonably solid answer to.
A simple example would be the use of
Considering the meat of this question seems to revolve around best practices I'd also consider the beta site Code Review