Do you copy & paste a code from books, articles, msdn whatever or are you always trying to come up with your own solution or implementation (the book etc. is just an inspiration - there are always 2 or more ways how to do it)? Or do you make difference between your noncommercial and commercial code, i.e. if you are writing a noncommercial code copy & paste is ok, but not ok for the commercial one?
Blind copy-paste is bad, period - you copy-paste bugs and risk copyright violations. Also that code might not meet the code style requirements of your company. It's a good idea to review code and rewrite it - this is quite easy and solves all the three problems.
I copy and paste sometimes, but always ensure I fully understand every line before I even compile it, let alone use it.
I sometimes copy-paste small functions straight off, but larger sections I want to write for myself to aid understanding.
Not so much since a colleague pointed out a consequence of Sod's Law: the more times you paste a piece of code, the more bugs are in it!
Considering your probably looking for it in the first place because you didn't know something, i prefer to just use the code i find as a reference. This way you might learn something and you wont be looking for it the next time ;)
I don't.
It's lazy and disrespectful to the code snippet author.
I always try to understand what's happening and how it is happening. I catch the idea and then write my own code. It may come as a surprise, but code examples are not published to be copied straight out, but to demonstrate some technique, because we know a code example speaks better than a thousand words.
If I do, it's only short sections - I'm usually looking for code to solve a very specific problem - and I re-write it to do one or more of the following:
- Bring it into my coding standards (namespaces, class names etc.)
- Understand what it does.
- Remove any extra functionality I don't need.
- Tweak the output so it matches what I actually want - as you'd have to be very lucky to find a piece of code that did exactly what you wanted.
Doing this means that you give the code a review and you end up stepping through each line in the debugger so you end up knowing the code quite well.
In fact the end result usually ends up bearing little relationship to the code I originally copied.