When a person contracts you for a project, would they not gain ALL rights to your work? (including the source code)
In most jurisdictions, the default is that the client would own copyright on the program as a "work for hire", but that's just the default. The actual disposition of rights is determined by the terms of your contract.
Personally, when I take on projects where the code is unlikely to contain trade secrets or have other similar issues, I offer my clients a discounted rate for any portions of the code which will either be released publicly under a Free/Open Source Software license or which they will allow me to retain copyright (in which case I, of course, either release it myself under a FOSS license or at least grant them an unlimited license to use, modify, redistribute, and create derivative works of the code). As a result, I have retained copyright ownership on two of the largest projects I've been hired to complete over the last five years.