417 points · by edward · 6y ago · 185 comments
If I need to use stackoverflow, these days I make sure I do a few things:
- read a number of answers, comments included. Just because an answer is accepted doesn’t mean it’s the best one
- read API docs for anything I’m unfamiliar with, and make sure I understand how the solution works
- never copy paste; instead use the general idea or concept to rewrite a solution in a safe, future proof manner
- if an answer works but doesn’t feel like the right solution, be prepared to do my own research to find a better approach
Hopefully this results in a better engineered product, but one of the main benefits is learning and expanding knowledge of APIs/techniques.
StackOverflow content is licensed under CC BY-SA, so you're legally required to do that if you copy code from an answer. (Unless it's so trivial the code is not copyrightable, but it's better to be on the safe side and attribute the source correctly anyway.)
It already happened to me a couple of times that I had a problem, googled it, and found a helpful SO answer .. by myself some years ago. So this is definitely a helpful suggestion.
One of the differences i've noticed between myself and some of the junior devs i've worked with. I would usually use the official docs as my first resource, and they would use stack overflow instead.
I'm impatient and Stack overflow can often give me an easy solution. But just as often, it can't and I will spend 30 minutes unproductively trying things with little progress.
As I become a better developer I become more inclined to spend 30 minutes with the documentation instead.
Of course most of interesting problems are not described in documentation.
I know it'll be worth it in the long run, but it feels like I'm wasting time because "I'm not doing anything" - I'm not committing any changes while my coworkers are busy adding new features.
I would use whichever googles first and most relevant results are. Also what the issue is matters.
I don't always go to MDN for javascript issues.
But if I want to know why something is the way it is, I will.
The people who made the mistake in the OP probably did exactly that.
Namely, "some dude's gist or blogpost [or stack overflow answer]" not getting updated. Not just that they didn't but that they couldn't.
Unsurprisingly, which you should rely on depends heavily on context. It takes experience and patience to figure out which one is correct based on circumstances.
IDE type search and doc comments are much more likely to be useful.
Recently I found an interesting snippet of C code which was using an sprintf to write a string into a char array. Unfortunately, it contained an off-by-one error where the null terminator would be written outside the char.
I proposed to fix the answer, but for incomprehensible reasons the edit was rejected (!)
If an answer is factually wrong (as opposed to just poorly formatted or missing a few details), generally its better to leave a comment rather than try to edit it.
I agree its not ideal, but suggested edits go into a site-wide review queue, so not everyone reviewing them will necessarily be familiar with C. This makes edit reviews a poor tool for evaluating factual correctness; the comments section and voting system on answers is a better tool for that job.
That's not correct; the questioner can accept any answer, the criteria is that the questioner chooses which answer helped them. There is no connection between votes and acceptance, no claim that "accepted" is endorsement by StackOverflow the company, by subject matter experts, or by "the community".
It is a good thing to test and to do more research, it is even better to add them to the post. Share your knowledge :)
The fastest correct looking answers on SO are usually the accepted answers. I read an post or tweet a few years back suggesting that you have the quickest draw if you want SO rep: create a bare-bones answer, with no research, so that it gets accepted. I understand why: my answers have been on the receiving end of this - I spent way too much time and effort answering and my answer dropped into obscurity.
That's why I no longer answer on SO.
Questions are often long abandoned, and the original questioner won't come back and choose a new correct answer. On a rarely looked at question, it would take years for a correct answer to raise up the ranks.
I've seen various answers which used cryptography dangerously, and asked if there was any way they could be fixed, and I just got the answer you gave. That shouldn't be acceptable when dangerously bad code is being showed to people.
With sufficient "rep" (2000, where an upvote on one of your questions or answers is worth 10), edits don't even require approval from others (before that, they must be peer reviewed before the edit takes effect)
SO is perfectly happy to accept better answers, especially to those which have gone stale and perhaps no longer applicable.
If you are offering further information that can benefit the reader its always welcome.
Its one of the reasons a lot of the more concise answers are not always the accepted answers. Because people do ask quite often : "Okay but why did that fix it"
In-fact : Answering without giving a proper reason for the solution is frowned upon mostly.
Agreed. I find that the "accepted" answer usually gets refined/improved/made more generic after acceptance.
This usually means that the "best" solution over all similar problems is usually further down the list of answers.
* * Cunningham’s law is the one I’m thinking of. https://meta.m.wikimedia.org/wiki/Cunningham%27s_Law
I've spent WAY too much time banging my head on the keyboard while trying to implement a solution posted 3-4 years ago, which doesn't apply in the slightest for the latest version of whatever I'm doing.
Do not copy/paste code from stackoverflow. SO is not a code writing service. It is a problem solving/question answering service. Any code you see in the answers should be takes as reference only. If you don't understand what the code does, re-read the answer, check the documentation, understand and then write your own code. What happened here is such a stupidly trivial bug. If someone only took a second glance at this code it would be blindingly obvious what is going on here. I saw it as soon as I looked at the SO code sample and I don't even write C#. Do people at Docker even do code reviews?
Also, when I write answers in SO, I try (not 100% of the time, but I do try) to make my code generic, so it solves the problem the user is having, but does not include any specifics from the question. I suggest you all do the same.
P.S. I know that this rant of mine is rather idealistic. Nobody is going to stop copy/pasting code and these things will keep happening. But writing this was therapeutic.
Take for example libcurl (thus, implementation in PHP, Ruby, Go, etc). There are sooo many posts with cURL example code where CURLOPT_SSL_VERIFYPEER is disabled. These are often answers with thousands of upvotes, that have been there for years.
Inexperienced developers copy/paste this code into production without understanding what it does.
Say the answer was there without that option. Next comment "It didn't work, I get 'certificate verification failed' error", next comment "I had to use this option to get around that", next comment "thanks it worked".
For example, part of writing safe code is adding an appropriate level of error handling, but this also dilutes it. If you're trying to understand a new concept, the code with lots of error handling will be harder to parse.
Also those with less English ability may not understand the comments that call out code as being wrong, reading the comments may be too high effort to happen naturally.
I can totally see how this happened, and I would definitely not say that I would never make this mistake, especially under time pressure etc. But a careful programmer would a) desire to understand what the code did, and thus spend enough time reading it to spot the `GetType()`, and b) test that the "prevent more than one instance from running" behaviour worked properly, which would involve both testing that you can't run two instances of the program, and testing that if you change the assembly identifier, you can run two instances.
Furthermore, it's a lot easier to take the time to understand what a few lines of code from SO does than it is to audit a dependency thoroughly.
I have thankfully avoided the world of NPM, but when I've had to use it I've found it very difficult because a lot of packages don't really have API docs.
Also, you should look at least at the docs of the packages you install.. And since most SO answers are shorter that introductory section of most docs, this means reading and understanding entire answer.
Brb reading the docs for every micro-package installed as a dependency for what I just installed that provides functionality that could've been hand written in five minutes.
Gonna spend the next few months reading about a thousand package README's for this single package I installed and all its dependencies down the tree, be right back.
There was a post recently that noted that React is up to over 800,000 dependencies. Good luck with that.
Or at least you have no idea whether it actually solves any problem besides the exact scenario you tested it for, and no idea if it's even possible to maintain it.
Wait... WHAT??
Who the hell copy pastes code into their program without first examining and understanding what each step is actually doing, how and why the pieces fit together as they do, and how it works as a whole to solve the problem?
Almost everybody at some point or another?
https://marketplace.visualstudio.com/items?itemName=matsz.pa...
He got shit done and that was what mattered to his bosses... of course it was all a steaming pile of unmaintainable garbage but that didn't matter to anyone.
If he couldn't find it on Stackoverflow he would ask another developer who would tell him how to do it... then Mr Senior Developer would ask that other developer if he could just "send him the code".
Last I heard he's still at that company and still making bank doing nothing but copying and pasting other people's work.
The developers of Docker for Windows and the Razor Synapse driver management tool. Among others.
The people who write the software that runs the airplane you’re on.
Has anyone about 40 lines of code with an average line width of 80 characters? I have this rather spare region that doesn't look nice on the mini map.
But seriously, I think the reasons are mundane and developers often have an ample list of problems to solve, so easy solutions for some of those are used as much as possible. Ensuring only one process runs at a time is probably just a feature that invites negligence.
It shouldn't happen, but I am not surprised.
Usually have to dig in all the responses to get a balanced overview, then apply those concept(s) to your problem at hand....
It's been raised with management, and nothing has been done about it.
Quite a lot of people do that. It's pretty much the reputation of Stackoverflow. And of course everybody knows it's bad, but it saves time and it works and there's deadlines so they move on and ship it to production.
How to find this particular bug is not that hard, though: you're looking for a way to ensure that your program can run only once, so you use that mutex. But you want to be sure that mutex is unique to your program and not shared with others that use the same method, so you create another program that uses the same method and test if they conflict. If they do, then your mutex is not unique to your program.
The trick of it is, it's only "bad" if you care about code quality and/or maintainability, fuzzy attributes which are seldom measured or optimized for. It's the only logical conclusion to "move fast and break things."
p.s. - the good news is, most of us are working on software that doesn't really matter terribly much, so the fact that it's bad also doesn't matter very much
Unfortunately, if they don't, you only know that there exists one program which does collide with your app's mutex. Nothing more, nothing less.
> But you want to be sure that mutex is unique to your program
This one's actually impossible to test. Or to be pedantic, we know it can't be done - another app can be using your app's guid for its mutex, but in realistic cases, it's just really hard to test for. There could be even a real use case for collision - independent agents walking the process tree and relying on locks based on guids to not step on each other.
Millions upon millions of people learning via googling
But yeah hopefully not in production & ideally sandboxed
What do you think the chances are that you could make exact references to
Assembly.GetExecutingAssembly().GetType().GUID
return a compile-time warning?That could make this go away very quickly.
(For bonus points, add relevant telemetry to VS. :P)
When I first read the Twitter thread I was sort of dumbfounded that whoever did this originally didn't actually check it. That's not a SO or .NET problem that needs to be caught. It's a developer problem.
If someone is copying code into their project that they've never seen before, they should, at bare minimum, make sure it produces the expected result for the first instance they are using it. That developer should have checked to see that the GUID was actually the one from their assembly. Even if someone is copying some complex code that they aren't 100% sure how it operates, they can still test it to make sure it produces what they think it does.
The idea that someone working for Docker just copy, pasted, and then didn't check that it actually did what it was supposed to do is sort of mind-blowing to me. Not sure why no one else is talking about that. I've never used Docker before, but this doesn't instill confidence. It's one thing to have something like this in your end product - that's bad enough. It's another thing entirely when your company builds tools for other developers to use. That shit needs to be rock solid and this is one of those things that seriously worries me when I see it because it's about a lack of effort/testing and someone just blindly took code and didn't even bother to check it.
I remember reading something about reasoning through a very simple (couple of divisions or something) math problem, where a deliberately (and later obviously) wrong working-out was initially defined, then corrected, to illustrate the importance of end-to-end understanding.
On the other hand,
a) Programming is equivalently complicated to "two-story-high whiteboard full of Greek letters and integration functions", but is never acknowledged by industry as such because of the associated responsibility nobody wants anything to do with
b) We live in a world where entire industries - ML-based AI - are literally!! based on the idea of not understanding the structure of the solution to the problem.
My preferred solution is simpler --- CreateMutex takes a mutex name of at most MAX_PATH characters. That's almost begging you to make it the path to the running executable.
char name[MAX_PATH];
GetModuleFileName(0, name, sizeof(name));
I know some of you may be thinking "but what if there are two copies of the application in different directories?" Yes, that would result in two instances, but how often is that a problem? If it is, then make it the binary name-only without the path pname = strrchr(name,'\\');
and then unless you name your application something very generic like test.exe, there can only be one. I guess the lesson here is to not overthink things --- I see that often in SO answers too.Doesn't a hardware maker want to make it as simple as possible to use their hardware from any OS?
I used to use a Razer mouse when it had a switch on the mouse itself to toggle between different speed settings.
But I changed away from them when that switch disappeared in later iterations of their mice and instead depended on software that didn't work in Linux.
[1]: https://www.razer.com/gaming-mice/razer-deathadder-elite
>Truly a second class platform.
Now maybe some Windows users will see how Linux users have felt for over 2 decades.
I suspect that most hardware companies cultures treat software engineering as as second class activity.
A home-made .NET-to-Java transpiler that requires custom builds of the CLR and JVM?
The 300MB of random data at the end of the Windows XP install CD?
An encrypted archive containing the set of folders comprising the entire source code history (the top level of which has an alarming number of entries named like "revert_v5_bug [Final] - ACTUAL CURRENT") (because some developer has OCD about backups)?
[1] not sure what happens since they "illegally" retrospectively relicensed all the content a few months ago.
- The asker can misunderstand the response.
- The asker can accept a response that for the general public's interpretation of the question is incorrect. It doesn't matter what the asker thought the question means to them if beyond their control their answer becomes the first result on Google years later for a problem that might not be exactly the same.
- Technologies change and new libraries or language features can be added since the question was asked. I've counted several times where people bunch up below the accepted answer to say this is true, and 90% of the time it's the better answer for my use case.
- The question is similar to another that is far more popular and happens to share the same SEO keywords but is completely unrelated, so another answer with 1,000 upvotes in comparison to the original answer's 100 is added. People have no choice but to put comments saying "this is the wrong answer if you're doing X, use the other one," because the comment section of the accepted answer is the highest up they can be seen. But the average user might not listen or see the smaller comment text and use the wrong answer, just because it was the accepted one.
This is because a single person's decision on what "correct" means is valued more highly than the correctness of the more important answer which thousands more people actually support, and is more critical to support because of unintended search indexing.
Sometimes SO questions become used for a purpose completely unrelated to the original questions and answers because of search engines. There should be some kind of acknowledgement of this instead of being unable to protect clueless people from themselves.
This isn't to support editorialization of old answers in general, it's a problem of unintended consequences out of anyone's control because of what gets indexed as the first search result on Google. Nobody can change what's there if nobody but the original authors can edit the answer and they happen to not do so for whatever reason, so we'd be stuck being misled forever. And as we've seen people will copy and paste anything in the code box and not even read the description and caveats if there's a fat green checkmark next to it like a Pavlonian reflex. I'm guilty of this myself.
Users who are callous enough to ignore such comments get what their lack of due diligence deserves.
However - you can configure the mouse, then uninstall the drivers and the settings persist.
So I continue to use steelseries.
> Both programs want to ensure you only run one copy of themselves. So they create a global mutex using the GUID of their .NET assembly, right?
> So what happens is that both of them are creating a global mutex to ensure only one copy runs, but instead of basing the GUID on their own code, they're both using the GUID of a part of .NET itself. > And they're using the same one!
> Back in 2009, the user "Nathan" asked how to get the GUID of the running assembly. Twelve minutes later, "Cerebrus" answered. And that answer was wrong.
> A year and a month later, it was pointed out (by "Yoopergeek") that it gives the wrong GUID.
> Three years later, Cerebrus returns and fixes the answer. They can't delete it, because it was accepted
> That flawed stackoverflow post is here: https://stackoverflow.com/questions/502303/how-do-i-programm...
Someone at Docker and someone at Razer both copy-pasted the same faulty code from Stack Overflow for preventing multiple instances of their program running at once. Unfortunately the code took the assembly identifier and called GetType() on it which returns the same value for all programs.
The Stack Overflow answer was later fixed but since the programmers no doubt both felt they'd "solved" the problem, they didn't see the fixed answer.
I'm sure someone has written a thingy to upgrade npm dependencies, rerun tests, and bisect to tell you about the breaking upgrade if anything reaks.
Left-pad, come back, all is forgiven.