← Back to context

Comment by beermonster

4 years ago

> I wrote almost a decade ago was found to be the most copied snippet on Stack Overflow. Ironically it happens to be buggy.

I don’t find it ironic, I find it quite normal that even small snippets of code contains bugs (given the daily review requests I receive).

I think when copying code literally from StackOverflow what’s more important is understanding what the code does, and why , rather than copying it ad-verbatim by copy & pasting it into your production code.

I also often find on StackExchange et al that quite often the most upvoted is the one that ‘fixes it’ for ‘most people’ yet the correct answer is down at number 3 or 4. Again, understanding the answer and why it applies, helps give you the context to understand if this is actually the solution to your problem or just treats the symptom.

What I realized years ago is that the upvote on Stack Overflow don't mean "I tried this and it works for me" or "I'm an expert and this is the answer". No, the upvotes on Stack Overflow are along the line of the upvotes/likes one would find on Reddit or HN. More like "you sound confident" or "I was looking for this but I haven't tried it yet"

  • > No, the upvotes on Stack Overflow are along the line of the upvotes/likes one would find on Reddit or HN. More like "you sound confident"

    I think you're right that online scoring systems tend to incentivise false confidence. This happens with blog posts too, where a student of some topic writes a confident and subtly incorrect blog post, and it then ends up on the HN front-page. Only someone with a relatively deep knowledge of the topic can then call out the errors. Ideally it should always be made clear upfront that the author is new to the material.

    Somewhat related: Stack Overflow's unfortunate norm of calling out mistakes in answers in a way that goes beyond confidence and strays into condescension and borderline hostility. For a lot of people it seems it's not enough to be seen to be right, they also feel the need to paint someone else as clueless, while just about passing as acceptably polite by keeping the aggression passive. If challenged, they'll brush it off as 'directness'.

    • experts can edit the stackoverflow answers (assuming their stackoverflow rating is high enough)

  • The number of times I've seen the only correct answer being a terse explanation with a short code snippet and having zero upvotes astounds me.

    They may not have been the attention seekers like other posters. But they provided exactly what was asked for. And when I come across their post years later I upvote.

  • > More like "you sound confident"

    Meh, I'm usually there looking for how to do something, and if a response helps me do whatever I was looking to acomplish, or at least on the right track, it was helpful and worth an upvote. I've never upvoted just because someone sounded confident.... at least not on SO.

    • There's also the possibility that the answer was correct when written. Especially with web stuff a year old answer could be completely wrong now.

One of the best tips I have gotten from the internet is to never copy and paste code you have not written yourself. Even rewriting it verbatim makes you think about what it is you are actually copying.

It's a pretty neat rule to have in mind.

  • Ladislav Vagner, a legendary programming tutor at FIT CTU, is a known proponent of being extremely cautious when copying code, even your own. He gives programming proseminars where students guide him as he codes the solution to some problem, e.g. mathjax-like typesetting in C++. It is a common theme in the proseminars that a bug is introduced by copying code. Probably on purpose, like many of the other bugs that students are supposed to point out.

  • I think that’s true if you’re trying to learn a new tool or technology. You probably won’t learn as much following the Rails or Django tutorials if you’re just pasting all the code. But if you’re just looking for some esoteric workaround for some very specific tool and use case, I think it’s fine to paste. And the latter makes up the overwhelming majority of my Stack Overflow visits.

  • It's also good legal advice. It's now legally possible for you to copy and paste code directly from stack overflow because they made an effort to assert a compatible license over works published on their site. However, the same can't be said for most other code snippets flying around out there.

    • Good point in case: license of the code in this exact article is very likely incompatible with your production code.

  • When you get into this habit it also makes it easier to translate solutions from other languages too

  • Yes, and:

    I didn't really grok Test Driven Development until I worked thru the book, line-by-line, experiencing the workflow.

    Knowledge vs experience.

> I also often find on StackExchange et al that quite often the most upvoted is the one that ‘fixes it’ for ‘most people’

And also first, or at least early, and subject to a reinforcing cycle of 'sufficiently good' or 'fixed it enough' that it achieves stratospherically more votes than an 'even more good' or 'fixes it properly' answer that came in too late for the same traction.

  • > also first, or at least early, and subject to a reinforcing cycle of 'sufficiently good' or 'fixed it enough'

    So exactly the solution most project managers are after? /s

    • Ha, well even if there's an argument for that, it quite often leads to (and is even more reinforced by in terms of votes) the original asker 'accepting' the solution; then the answer that 'fixed it for them, first' is forever ranked highest, even if it rarely works for others and something else is more up-voted.

This works better when the problem does not 100% match the issue you are tackling. It makes you think about how you can reshape what you found into something useful.