If you are in a degree/program where writing code is expected, then you must write code.
However, let's say you are in a physics program, and you are doing something for physics, and it just so happens that you found a piece of code that helps with your research, then maybe you don't need to write it, you can reference it.
For my Master's thesis, I referenced a document that was not publicly available, so I included it as an appendix as reference (with permission of the author). I explicitly stated that it wasn't my work, and just there as reference.
Ask your advisor.