Let's talk about copyright, licencing, and who owns the code
As a freelance developer, the most common conversation I have with new clients is about copyright, licensing, and code ownership. It isn't a well understood area for many people, and many other freelancers who I talk to don't really get it, either. So let's talk.
In general, when you create something, you are automatically assigned copyright over it. This means that you have the right to determine who, when, and how the thing that you created is copied. For me, as a developer, any code that I write is automatically copyrighted to me. I can, through a general agreement or a contract give copyright of my code to someone else, but it's mine to begin with.
Say you want to share something that you've created with other people, but don't want to give them copyright. What you do is you license it to them. When you buy Microsoft Windows, you are actually buying a license to use the software - not the software itself. This can be a tricky distinction, which can be made even more confusing with some particularly permissive licenses. In short, though, a license will allow you to use software that is copyrighted to another person. How you may use it is often described in the license agreement.
Who owns the code?
Now we get to the meat of the discussion. Let's say that you hire me to build you an e-commerce system. You have some magic secret up your sleeve that will revolutionise e-commerce, and will take the world by storm. When we're discussing the terms of the project, you read through my terms of trade, and encounter this:
Unless otherwise specified I retain ownership of all original artwork, software and HTML code, in any media, including digital files, whether preliminary or final. If You require ownership/copyright of any materials then this must be nominated in the Schedule of Work prior to quotation and will become part of the quotation and final contract.
Oh no! Does that mean that Sam will own my business? How can I get changes made if Sam owns my code? Am I locked into using him as my developer forever? What if he gets hit by a bus?
The answer is simple. I have copyright over the code, and anything that I produce. Your logo, or that funny cat picture you took that you want to use on your homepage? They're yours. You own the copyright to them (so far as I'm concerned, at least - they're not mine). The only thing that my ownership of the copyright means for you is that you may not copy it. That's it. My code is able to be used for this project, and this project alone. A little bit further down is this:
Unless otherwise agreed, all materials You provide to Me will remain Your property or the property of the respective owner.
That means that I can't copy your intellectual property. I can use it for this project, and this project alone. Ok. So what's mine is mine, and what's yours is yours. But what if you need to change what's mine, for your business?
Upon full payment of My fees, You will be granted an exclusive licence to allow Your commercialisation, use and exploitation of the Product.
When you have paid me, you are granted a license to use my copyrighted code. This license lets you modify it, make money off it, change how it works, etc. The only thing that this license doesn't let you do is copy it. That means you can't start selling parts of my code to other companies (unless you sell the entire code base and your license, in say, a corporate take over), and you can't start using my code in your other projects.
Similarly, I can't use your intellectual property in my other projects. If you designed the brilliant approach to e-commerce, then that's yours. I simply implemented it. You own the design, and I own the implementation. Neither of us can re-use the other person's work without their permission and appropriate licensing. I can't re-license the implementation to other people, because the license I gave you is exclusive.
So why do I need to keep copyright?
Finally, this is the crux of the issue. If I can't use the code again, why do I need to keep copyright?
Again, I cannot use the implementation of your idea again, but the implementation of one big idea is made up of the implementation of many, many smaller ideas. My ideas. My ideas that I use over and over again, because they're good ideas. It might just be something small like my favourite way to sort things, or it might be my way of implementing text-search over people's names. Each one is just a nut or a bolt in the bigger picture, but they're useful to me.
If I hand over copyright to the code that I've written, I need to re-design every one of those little nuts and bolts again. This would be like a carpenter handing over all of their own customised tools when they finish making a bench. Those functions are the culmination of years of experience, education, and training. They cost a lot to build in the first place (years to perfect, in some cases), and I can't replace them every time I work on a new project. The cost of these pieces of code has been spread out over the large number of projects that I do, and will continue to do into the future.
If you absolutely, positively, need the copyright to my code, then tell me. We can come to an arrangement, and I will re-design all of my tools just for you. Of course, you will have to shoulder the entire cost of this - there's no sharing the load if you're taking the copyright. If, however, you just want to sell copies of the code, then we can arrange a different license.
I hope that that clears it up. I own the code, you own the license to use the code, and the world keeps chuggin' along.
NB: I am not a lawyer, and the laws around copyright in your region may differ. This is my approach to the issue. Also remember that sometimes third party code may get used, which could affect licensing or copyright.