9
\$\begingroup\$

I recently posted a question in which I purposely only included a specific code snippet: Purposely raising exceptions to use catch

In my original (unedited) question, it had a simple example of actual code that I'm using, with the method names and specific exceptions removed. The intent was to keep my question simple so my question is addressed appropriately). "Best practice" types of questions are not generally acceptable for Stack Overflow, hence why I posted it in Code Review.

Was I truly wrong to try to make my method names and exceptions generic? They had no value to my question and would only serve to confuse respondents. Through giving my method names dummy names for an example scenario -- irrelevant to my scenario -- the downvoter decided that my question was now appropriate, even though this edit added no value to my question.

I've read this site's rules and it does little to clarify the requirements around this type of scenario. If generic best practices don't belong in this site at all, why are specific best practices acceptable? That just allows duplicate questions to be asked with slightly different requirements.

\$\endgroup\$
  • 6
    \$\begingroup\$ +1 Thanks for bringing this to meta! \$\endgroup\$ – Mathieu Guindon Apr 22 '14 at 15:19
9
\$\begingroup\$

I decided to downvote your question instead of casting a close vote, because of the relatively new consensus about narrowed-down questions, to which I don't agree.

So I let your question go, with a downvote. After 2 days there still hadn't been a single close vote on it, so seeing that it had been edited, I retracted my downvote and decided to play the game and review your code - because that's what CR answers are supposed to be: peer reviews.

This is the accepted answer (7 upvotes, 0 down):

Using exception handling for flow control is discouraged by Microsoft.

And a round table on the topic is available.

That being said, C# supports doing so, and I suppose it depends on the condition encountered whether an exception is the most appropriate response.

Nowhere near a peer review. It does answer the OP's question though - about whether or not it's ok to throw an exception just to send the execution path into a catch block, i.e. to control execution flow.

The problem is that, in my opinion, this isn't what this site is about.

From our Help Center (emphasis mine):

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?

If the answer to any of these 6 points is no, then CR is not the right place to ask. In my opinion the narrowed-down nature of your question went against the last one; answerers simply cannot address any facet of your code, other than what you've narrowed it down to.

As a result, I deem your question to be more of a high-level design question that's best asked on Programmers.StackExchange, since all you're looking for is a "no, it's not best-practice because xyz" rather than an actual peer review, where answerers can help you come up with cleaner alternatives and point out other flaws in your code, flaws that can affect readability, maintainability, performance, security, etc.

Until a couple of weeks ago, I would have simply cast a close vote and pointed to Programmers.SE in a comment. In spite of recent meta-discussions and seeing that no one else was casting a close vote, I chose to downvote instead.

Your comment on my answer:

This answer demonstrates exactly why I tried to keep my code generic: I didn't want to list out every single exception that I actually make in my code; I didn't want to list the actual method names; I didn't want to list the method declaration; I didn't want syntax suggestions. I had a single question about whether throwing exceptions for flow control was okay, which was promptly answered by B2K. I would be happy to debate this on meta. – grovesNL 41 mins ago

To me, demonstrates exactly why I should have stuck to my original idea and cast a close vote instead. If you're looking for a "yes/no" answer, CR definitely isn't the right place to ask. Here's a Community Ad that was recently submitted, that does a very good job at explaining what we do:

enter image description here

\$\endgroup\$
  • \$\begingroup\$ From Introducing Programmers.StackExchange.com: "In a nutshell, Stack Overflow is for when you’re front of your compiler or editor working through code issues. Programmers is for when you’re in front of a whiteboard working through higher level conceptual programming issues." Based on this description, I don't think the question qualifies for Programmers.SE either as it's not really asking about exceptions conceptually. \$\endgroup\$ – grovesNL Apr 22 '14 at 16:20
  • 2
    \$\begingroup\$ @grovesNL I have to disagree there. You are asking about the concept of throwing exceptions as a method of flow control. And that means, you should have headed for Software Engineering in the first place... The scopes for Programmers and Codereview are a little difficult to distingush here, though. But as you provided abstracted code, this question is definitely off-topic for CR. And thus migration to Programmers.SE was the right step to take. \$\endgroup\$ – Vogel612 Apr 23 '14 at 7:46
6
\$\begingroup\$

Code Review excels at reviewing every detail of real, working code that is specific to your project. By renaming all of your functions to have generic names, you've turned a concrete question into a conceptual one, of interest to all programmers.

From the Programmers Help Center

proper scope for questions on Programmers Stack Exchange

I have therefore migrated the question to Programmers.

\$\endgroup\$
  • \$\begingroup\$ So simply renaming the methods from their original names (which are essentially meaningless, at least without context) to generic names, causes the question to become ineligible for this site? \$\endgroup\$ – grovesNL Apr 22 '14 at 17:01
  • 5
    \$\begingroup\$ Yes, because details matter — often in unexpected ways. \$\endgroup\$ – 200_success Apr 22 '14 at 17:35
  • 2
    \$\begingroup\$ From your answer it sounds a bit like "If the question is applicable for all programmers, it doesn't belong on CR". I don't think that's necessarily the case at all times, but it is definitely the case for this question. \$\endgroup\$ – Simon Forsberg Apr 22 '14 at 19:05
  • \$\begingroup\$ If I may chime in: Effectively, yes, because that implies that the question is not about this particular chunk of code, but rather about this style of programming. \$\endgroup\$ – Hannele May 2 '14 at 13:52
5
\$\begingroup\$

The current version of the code is ...

try
{
    if (GetDataFromServer())
    {
        ProcessData();
    }
    else
    {
        return null;
    }
}
catch(Exception ex)
{
    return null;
}

... which makes sense.

The version which you originally posted said, DoSomethingElse(); instead of return null;.

The original version didn't make sense to me, because I didn't know:

  • Might DoSomethingElse() throw an exception?
  • Is it be harmful or expected to invoke DoSomethingElse() twice?

Because it didn't make sense I didn't answer it.

The reason why it didn't make sense is that you altered (i.e. obfuscated) it before you posted it on CR.


In this comment, your wrote,

This answer demonstrates exactly why I tried to keep my code generic: I didn't want to list out every single exception that I actually make in my code; I didn't want to list the actual method names; I didn't want to list the method declaration; I didn't want syntax suggestions. I had a single question about whether throwing exceptions for flow control was okay, which was promptly answered by B2K.

That's another reason why code snippets and 'example code' are off-topic: What topics can I ask about here? says,

Simply ask yourself the following questions. To be on-topic the answer must be yes to all questions:

  1. ...
  2. ...
  3. Is it actual code from a project rather than pseudo-code or example code?
  4. ...
  5. ...
  6. Do I want feedback about any or all facets of the code?

You're saying you don't want feedback about any/all aspects of the code, and that you're hiding aspects of the code to avoid feedback.

In summary, you have a specific question about Programming, and you don't want a code review.

\$\endgroup\$
  • 2
    \$\begingroup\$ +1, and it is also unclear if GetDataFromServer and ProcessData can throw any exceptions, which is highly relevant. \$\endgroup\$ – Simon Forsberg Apr 23 '14 at 8:47

You must log in to answer this question.

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