What is meta? ×
Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 128 Stack Exchange communities.

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?

share|improve this question
7  
No, as long as it's reasonably short and clear. It wouldn't be the first question along that line. –  Henk Holterman Aug 7 '13 at 19:17
    
If anyone goes to look for said question, I realized that I had misunderstood part of the code. The error remains, but is not due to how that line of code was written. –  Btuman Aug 7 '13 at 20:00
    
Satanic= what exactly? Equivalent to job security code or somesuch? –  Joe Aug 7 '13 at 21:11
1  
@Joe, IMHO "satanic" can be an appropriate metaphor for code that cannot possibly work under the rules of our universe, yet still does. –  Frédéric Hamidi Aug 7 '13 at 21:15
1  
@Btuman: I've edited my answer somewhat significantly below due to a point Cody Gray made. –  Andrew's a Unitato Aug 8 '13 at 13:15
add comment

3 Answers 3

up vote 11 down vote accepted

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();
Widget.Flog(w);

I always thought that widgets should be flogged this way, though:

Widget w = new Widget();
w.Flog();

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.

etc...

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.

share|improve this answer
4  
And then Eric Lippert shows up and gives with a 2-page lecture on how they designed it and why the way it is... :) –  Mysticial Aug 7 '13 at 19:19
2  
Questions about satanic code can be useful. I learned a few things about SharePoint thanks to one. –  Frédéric Hamidi Aug 7 '13 at 19:19
    
@Mysticial hehe; Well, in that case, you do have the person who wrote it available. (or at least someone deeply involved!) ;) –  Andrew's a Unitato Aug 7 '13 at 19:19
2  
We call those "home field advantage" posts where the person involved answers. But there's a clear distinction between questions that benefit from a "home field advantage" answer vs. questions that require it. –  Mysticial Aug 7 '13 at 19:24
1  
I disagree with parts of this. There are lots of known idioms/hacks that look obfuscated or downright wrong to the uninitiated, but that experienced users are quite familiar with and can explain either why they are necessary or why they were necessary when the code was originally written. There is nothing wrong with asking what a particular line of code does, and why it is written the way it is. If there is no good reason for it, that is a valid answer. –  Cody Gray Aug 8 '13 at 8:14
    
@CodyGray Hmmm... that's not a bad point, actually. I think I might edit to nuance my answer a bit. –  Andrew's a Unitato Aug 8 '13 at 13:04
add comment

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.

share|improve this answer
    
This is a great point that Cody just noted, too. +1 for that! –  Andrew's a Unitato Aug 8 '13 at 13:29
add comment

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
  • Performance
  • 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?
share|improve this answer
    
But in this case it was code I was working with and did not write, and it was a question of "this code seems to not work" (It seemed to validated a string as an int). –  Btuman Aug 8 '13 at 14:36
1  
I think there's an importance difference between getting a code review and trying to adhere to best practices, versus "What does this code do, and why?" –  Cody Gray Aug 9 '13 at 1:02
add comment

You must log in to answer this question.

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