How to cite code borrowed from other sources
To cite code borrowed from other sources, the main concern in avoiding plagiarism is that you acknowledge that it is not your own work. Most fundamentally, you need to add a comment in your code that makes two things clear:- The code you are using is not your own original work; and
- what is the source of the code you borrowed, usually indicated with a URL.
For example, suppose I am writing a JavaScript program. I need a function that sorts an HTML table by columns, and I find the following code on the site http://example.com/sort.js:
function sortTableArray(tableToSort) { // Here is the code of the original function }
If I borrow this function exactly in my own code, with no changes (except perhaps changing the function name), then I could cite it like this:
/* Code copied from function sortTableArray obtained from http://example.com/sort.js */ function sortTable(tableToSort) { // Here is the code of the original function }
If I borrow this function with some modifications (whether minor or major) in my own code, then an appropriate way to cite code modified in this manner would be:
/* Code modified from function sortTableArray obtained from http://example.com/sort.js */ function sortTable(tableToSort) { // Here is the code of the original function }
Note that the only difference between the two ways to cite code is that exact copies say “code copied from…” and modifications (whether minor or major) say “code modified from …”. In either case, you satisfactorily acknowledge that you are not the original author of this portion of code and you reference the source so that others can find it.