As a new contributor, I have read up on how to answer questions (and there is a lot of information related to that), and am slowly finding my feet, but haven't been able to find any information specifically relating to this.
Some of my answers are very long and took considerable time to write and provide references for (e.g. storing a js value from 1 ActionResult to use in another ActionResult) - these sometimes end up arriving after another answer has been accepted. Some, or rather one, (How to use a UriBuilder and HttpUtility.ParseQueryString to store a URL and parse it) are the opposite - short and concise (I recently read a "meta" discussion on which are better, long answers or short "fire and hope you get the points first" answers, and one of the seemingly popular suggestions was to write a quick, accurate, reply, and then embellish it over time).
The latter of those two answers was downvoted almost instantly, unfortunately with no comment so I can't improve the answer. I have read about and understand the anonymous vote process, so this is not a question about that. I don't take these things personally but would like to learn from it to provide better answers in the future. I did originally provide very little context in the code sample, and then expanded on that in an edit when I re-read my answer.
(brief background: in this case the OP was going from string to object, to dictionary, to string. The regex approach I suggested simply goes from a string to a string - a viable alternative. I understood that the OP was not asking about NameValueCollections, but how to approach the problem).
So, my questions are:
- Is it frowned upon to provide alternatives to the direction the OP is going in if you think they are either better, or both valid and thought provoking?
- Should I have expanded at length to explain why the alternative is attractive?
- Should I, in future, rigidly stick to answering the OP's question (though in this case I felt I was as one of his questions was "Am I on the right track with my code?")
I am hoping I can learn from this one and find the right balance between massive missives for answers which take a long time to write and even longer to read, and terse five-liners that require more effort from the reader to understand why that answer was given.
(And yes, the irony is that I'm also currently wondering if this is an acceptable question to ask, and if this is the right place to ask it).
Edit: the link to the Fastest Gun in the West Problem I mentioned above.
Edit 2: I realise that as I'm probably going to edit my answer as a result of feedback, I should probably record my original answer or this question will become meaningless:
Could you use a regex to parse it instead?
string uri = "website.com/stuff/?referrerPage=1&productID=1234567&tab=Tile"; var rgx = new Regex("productID=(?<pid>[0-9]+)", RegexOptions.IgnoreCase); string pid = rgx.Match(uri).Groups[1].Value;
site.com/path/?notproductId=42&productId=1337
. Using regex when there's a perfectly fine URL parser available just complicates things and adds potential bugs for no good reason.This is how you accomplish x, but this is how I think you could do it better
. That way you are still answering the question directly and still providing the valuable alternative.