-21

Update: Wow this received much more negative attention than I expected!

I still think there is value in culling out the quality reusable code that lies in the SO repository. I don't have interest in hacks, or partial answers. Specifically I don't want to export all the code within SO like some of the comments have mentioned.

Rather, there are a number of SO answers where people have commented "Why hasn't MSFT put this into the .NET framework"!? and other samples that are better than Microsoft's own code sample database

I think there is a middle ground between closing this question, and promoting best practices that are buried within SO.


I've been collecting code samples from Stack Overflow and placing them in a Utils class for some time. Recently have been moving all relevant code samples into the namespace StackOverflow.System or StackOverflow.WCF etc...

Question Has anyone collected a list of "best of" Stack Overflow code, with a comment by each function linking back to the Q: where the data came from?

It is possible for someone to create an official repository?

Here is an example of what I'd like to see:

namespace StackOverflow.System
{
    /// <summary>
    /// Source http://stackoverflow.com/a/2575444/328397  alternate http://stackoverflow.com/a/263416/328397
    /// 
    /// Also it has extension method to provide a fluent interface, so you can use it like this:
    ///public override int GetHashCode()
    ///{
    ///    return HashHelper.GetHashCode(Manufacturer, PartN, Quantity);
    ///}
    ///or like this:
    ///
    ///public override int GetHashCode()
    ///{
    ///    return 0.CombineHashCode(Manufacturer)
    ///        .CombineHashCode(PartN)
    ///        .CombineHashCode(Quantity);
    ///}
    /// </summary>
    public static class HashHelper
    {
        public static int GetHashCode<T1, T2>(T1 arg1, T2 arg2)
        {
            unchecked
            {
                return 31 * arg1.GetHashCode() + arg2.GetHashCode();
            }
        }

        public static int GetHashCode<T1, T2, T3>(T1 arg1, T2 arg2, T3 arg3)
        {
            unchecked
            {
                int hash = arg1.GetHashCode();
                hash = 31 * hash + arg2.GetHashCode();
                return 31 * hash + arg3.GetHashCode();
            }
        }

        public static int GetHashCode<T1, T2, T3, T4>(T1 arg1, T2 arg2, T3 arg3, T4 arg4)
        {
            unchecked
            {
                int hash = arg1.GetHashCode();
                hash = 31 * hash + arg2.GetHashCode();
                hash = 31 * hash + arg3.GetHashCode();
                return 31 * hash + arg4.GetHashCode();
            }
        }

        public static int GetHashCode<T>(T[] list)
        {
            unchecked
            {
                int hash = 0;
                foreach (var item in list)
                {
                    hash = 31 * hash + item.GetHashCode();
                }
                return hash;
            }
        }

        public static int GetHashCode<T>(IEnumerable<T> list)
        {
            unchecked
            {
                int hash = 0;
                foreach (var item in list)
                {
                    hash = 31 * hash + item.GetHashCode();
                }
                return hash;
            }
        }

        /// <summary>
        /// Gets a hashcode for a collection for that the order of items 
        /// does not matter.
        /// So {1, 2, 3} and {3, 2, 1} will get same hash code.
        /// </summary>
        public static int GetHashCodeForOrderNoMatterCollection<T>(
            IEnumerable<T> list)
        {
            unchecked
            {
                int hash = 0;
                int count = 0;
                foreach (var item in list)
                {
                    hash += item.GetHashCode();
                    count++;
                }
                return 31 * hash + count.GetHashCode();
            }
        }

        /// <summary>
        /// Alternative way to get a hashcode is to use a fluent 
        /// interface like this:<br />
        /// return 0.CombineHashCode(field1).CombineHashCode(field2).
        ///     CombineHashCode(field3);
        /// </summary>
        public static int CombineHashCode<T>(this int hashCode, T arg)
        {
            unchecked
            {
                return 31 * hashCode + arg.GetHashCode();
            }
        }
    }
}
17
  • Isn't Stack Overflow itself a good repository, with alternatives (other answers), and context? (Bookmarks can be used to save your favorite thingies.) Also think about the CC+attribution requirements.
    – Mat
    Oct 18, 2012 at 13:45
  • 1
    -1 for violating YAGNI! Oct 18, 2012 at 13:46
  • @Mat It isn't possible to save your favorite answer, and answers with source code are hard to find. There is no "search for answers with code" that I'm aware of. Oct 18, 2012 at 13:46
  • 1
    Bookmarks work just fine for saving your favorite answers. You can even bookmark comments if you feel like it.
    – Mat
    Oct 18, 2012 at 13:48
  • @SulfurizedDemonbobby With YAGNI I should toss out all the bits of .NET that I don't use. The vetting, ubiquity, and shared knowledge of the .NET framework outweighs YAGNI in this case Oct 18, 2012 at 13:49
  • Yeah, that's why I use the Mono-Linker. :P Oct 18, 2012 at 13:49
  • 3
    What is best? Who is going to judge that? Just because something answered a question and got upvotes doesn't mean the code is particularly library-ready. A library/repository as you describe would be exactly something I would avoid.
    – Bart
    Oct 18, 2012 at 13:50
  • @Bart Is there a way for me to search for answers that have code samples? Oct 18, 2012 at 13:57
  • Not that I know of, no. Why?
    – Bart
    Oct 18, 2012 at 14:00
  • 1
    There's a code: search term you can combine with is:answer (see: meta.stackexchange.com/questions/55473/search-just-code), but I'm not sure it will be very useful for what you're trying to do. /cc: @Bart.
    – yannis
    Oct 18, 2012 at 14:06
  • @YannisRizos Ah, thanks for that. Any particular reason why it's not listed as part of the Advanced Super Ninja Search Options?
    – Bart
    Oct 18, 2012 at 14:08
  • @Bart I don't know, it might not be quite ready yet.
    – yannis
    Oct 18, 2012 at 14:11
  • I'm not sure that you appreciate just how random the bits of code scattered over Stack Overflow are. Nor how much of it is deliberately sketchy so that the point is not lost in jumble of setting up and tearing down of scaffolding. Oct 18, 2012 at 14:16
  • 2
    If you ever do this, please remove any answers of mine which include the word "evil"...
    – Jon Skeet
    Oct 18, 2012 at 15:53
  • @Mat even if I can save an answer as a hyperlink, that data isn't available to the SO Data Explorer (obviously) because it's not in a database. There are many requests on Meta for what I'm describing to become a feature of the main site Oct 18, 2012 at 18:11

1 Answer 1

14

It sounds like you're in the process of building the most disjointed, poorly organized library in the history of software. The top-voted answers on Stack Overflow cover a range of every possible kind of programming problem one could face. There is absolutely no way that these solutions belong in the same library.

Nevermind that most of the top-voted solutions are so specific as to be useless in a library, you'll still wind up with code for parsing an ini file next to code for managing keyboard shortcuts. It would just be nonsense.

So the answer is... No. Nobody has collected the various code fragments into one library because such a library would be a bloated unusable monster of half-baked, purpose-built code snippets that no sane person would include in their application.

3
  • +1 - I know some programmers who would include it though.
    – JonH
    Oct 18, 2012 at 18:15
  • There is a middle ground, I'm not looking for all code snippets. I'll bet you have code that you found from SO and sometimes see new/better ways of implementing it. If that process of discovery, evaluation, and recompiling was distributed beyond your own efforts you would be more successful as a coder. If I used the word "library" when I should have said framework then that was a misstatement. A counter argument to your answer is the .NET contains a breadth of functions that specifically includes the functions you mention. Oct 18, 2012 at 18:17
  • 1
    .Net contains many supporting libraries which contain functions which are sane within the context of their libraries. Not the same as a single framework full of random Internet code snippets.
    – user229044
    Oct 18, 2012 at 19:03

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