I wish that were always true.
I worked at a place where I was working on a feature that had to integrate with some code that a consulting group had developed for us. As I looked at their code, I knew that it had been developed by a single developer in their group, but it was odd in how completely disjointed the code was... even in individual source files; it was like it was developed by a team that didn't talk or plan together: no consistency in naming, functional boundaries, code style, etc.
This had raised some red flags for me... so I took some snippets and googled them. Sure enough, the code was largely an assembly of cut/paste code units from various blogs, Stack Overflow, etc. that had been glued together with only small changes made to get the disparate bits to talk to each other.
The applications we were building were IP sensitive and the developer and consulting group had represented this as original work. No indication of where the code originated came in any form or communication. Truth is that enough of this copied code was sufficiently verbatim that there would have been no question as to copyright ownership (not us or the consulting group) and with many questions regarding as to if we had copyleft license terms, permissive terms... or had no license at all to use the works.
Naturally this led to much unhappiness on our part and had both sides going for their contract terms. It also didn't help that our company's products were substantially business legal services and close to half of the employees at that time were attorneys.
This isn't to say we didn't use many of the same resources in our internally developed code: but we did so as you suggested... as documentation and knowledge sharing. We also were fairly scrupulous about stating where ideas and techniques we learned this way originated via code comments including links to the source we relied on.