Unless you are angling for commercial use of your code, you should post the code publicly, to GitHub, your own website, etc.
To be clear, you are almost definitely under no obligation to provide your code. You have completed your Master's. Absent an express agreement regarding the IP in your source code (you have not mentioned one, so I'm assuming there isn't one), you don't have to provide it to anyone at this point, any more than you would have to provide your research notes if you submitted a thesis on Shakespeare's Hamlet. (See more information on this point below.)
If you are merely concerned that you receive intellectual credit—or, as you indicate, that the undergraduate is able to differentiate between your work and their contribution—the easiest thing to do would be to post the code publicly, for example to a GitHub repo with an open source license, and then just send the professor the link to the repo. As a bonus, posting your code publicly is a good way to show off your work.
Regarding specifically the concern that the undergraduate will rely too much on your code, not enough on their own additions, and the professor won't know the difference, there are three points:
- That's not really your concern, it's the concern of the professor supervising the project.
- Are you certain that the new student's project is a coding project? Perhaps you wrote some analytical code, and the new student's project is to actually run it against various datasets and analyze the results.
- If you post it to GitHub, the new student can fork the repo and the professor (or you!) can then very easily use diff to view the changes.
Mostly, I understand the desire to get credit for one's own small contribution to the growth of knowledge (this is not to belittle your work—most of us, myself included, will only ever get to make small contributions). The best way to do that is to share your code with everyone rather than keep it from one person with an immediate need for it.
Additional notes regarding university IP in student work product:
A commenter has suggested that the university has IP in the student's work. In their field (chemistry) they were required to turn in all notebooks, images produced by electron microscopes, etc.
I reviewed my own university's policies on Inventions and Patents. Graduate students are included in a long list of roles who automatically assign IP rights to the university. Undergraduates are not. Work produced on the person's own time, not using university equipment, is not included.
Does this turn on whether OP was programming on a university computer or a personally-owned laptop? I poked around online, and came up with an interesting FAQ by the World Intellectual Property Organization. (Note, I am not an IP lawyer, just a university social science faculty member, so I can't evaluate the legitimacy of this source. But it does on the face of it seem credible.) The full result can be read at https://www.wipo.int/about-ip/en/universities_research/ip_policies/faqs/index.html, but the gist is this:
Who owns IP generated by students?
Most universities recognize as a general principle that students who are not employees of the university own the IP rights in the works they produce purely based on knowledge received from lectures and teaching. However, there may be some circumstances where ownership has to be shared or assigned to the university or a third party. Typically, these include:
- Students who are sponsored. […]
- Students working on a sponsored research project. […]
- Students working on research, theses or publications in collaboration with academic staff. […]
- Use of university resources. […]
While it is important to address these issues in the university IP policy, the university would usually need to have an express agreement from the student before he or she embarks on the research. This is because students are not automatically bound by the policies of the university.
My takeaway from this, and thoughts on the current question are:
- Unless OP believes there is commercial potential for this code, there is probably no reason not to provide it to the faculty member.
- If it is shared, there is no reason not to share it widely (and some reason to share it widely).
- It is hard for me to imagine the professor pursuing an IP claim against the student. If the student refuses to share the code, that will probably be the end of it.