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?
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:
I am working on flogging my widgets in
Z+-, and I have source code from a former developer which flogs the widgets this way:
Widget w = new Widget();
I always thought that widgets should be flogged this way, though:
Widget w = new Widget();
I can't see why it was done that way here. To my understanding, using static methods in
Z+-is a really bad thing in general because of what I read here.
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
i for counters/iterators/etc. While hardly satanic on it's own, it illustrates the point of someone writing code not using the most readable option (in this case a descriptive variable name) because to people aware of the convention that code is in fact perfectly readable as-is, and so it is relatively easy to give a clear answer.
Considering the meat of this question seems to revolve around best practices I'd also consider the beta site Code Review
Code Review Stack Exchange is for sharing code from projects you are working on for peer review. If you are looking for feedback on a specific working piece of code from your project in the following areas…
- Best practices and design pattern usage
- Security issues
- Correctness in unanticipated cases
I'm confused! What questions are on-topic for this site? Simply ask yourself the following questions. To be on-topic the answer must be yes to all questions:
Does my question contain code? (Please include the code in the question, not a link to it)
- Did I write that code?
- Is it actual code from a project rather than pseudo-code or example code?
- Do I want the code to be good code, (i.e. not code-golfing, obfuscation, or similar)
- To the best of my knowledge, does the code work?
- Do I want feedback about any or all facets of the code?