views:

93

answers:

7

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?

+3  A: 

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.

sharptooth
The question wasn't probably too exact. I do not mean algorithms. What I meant is i.e. in .net the implementation of a common OpenFileDialog - it is very similar, or the same, in all books, articles etc.
nubm
@nubm: I don't mean algorithms either. Copyright protects the source text. I only mean source text.
sharptooth
+1  A: 

I copy and paste sometimes, but always ensure I fully understand every line before I even compile it, let alone use it.

teedyay
+1  A: 

I sometimes copy-paste small functions straight off, but larger sections I want to write for myself to aid understanding.

Lizzan
+1  A: 

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!

dmuir
+1  A: 

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 ;)

red-X
+1  A: 

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.

Developer Art
+1  A: 

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:

  1. Bring it into my coding standards (namespaces, class names etc.)
  2. Understand what it does.
  3. Remove any extra functionality I don't need.
  4. 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.

ChrisF