Did you know? (It’s worthwhile including attribution for code snippets!)

I believe that everyone who writes software of any size will reuse code written by someone else at some time. I’m talking about referring to a library or API, but to copy & paste reuse; fully adopting someone else’s source code. Further, I believe that there are lots of great reasons to do so. Developers are quite used to performing internet searches or using dedicated code repositories to locate candidate code snippets that they will import into their projects in whole or part, and possibly modify to their own purpose.

Much of the code you’ll find this way is freely available (it will cost you no money), but may have additional licensing requirements that you should be aware of and conform to. One of the most common requirements is attribution.

attribution (noun) The act of attributing, especially the act of establishing a particular person as the creator of a work of art.ref

In today’s entry, I’m going to make a case for including proper attribution for all the third-party code we reuse, and suggest a consistent way to do so. My examples will focus on Google Apps Script, but this can apply to any programming language.

The reasons for code reuse are pretty straight-forward, including:

  • You can often come up with working solutions much faster than you might have if you had to write it all yourself.
  • You can focus your effort your own added-value modifications, while reusing existing snippets for less important or routine operations.
  • (You hope) that code is extensively tested and therefore headache-free.

The value of including attribution

It takes effort to embed attribution information in code, so why bother? Here’s a few reasons:

  • Doing so shows respect to the creator / author of the code you’re re-using. (…and if there are any bugs, they must be their fault, too!)
  • We avoid the appearance of plagiarism by clearly representing others work as theirs, not ours.
  • When you come back to the code in future, you’ll have a pointer to help understand it in its original context. This will greatly simplify maintenance.
  • The original posting may include future updates and bug fixes that you need.
  • In many cases, attribution is required under licensing terms. (See Legal Concerns.)

Recommended format for attribution

I’ve gotten into the habit of embedding a simple comment that refers to the ultimate source of the code I’ve adopted or adapted. Something like:

// Adapted from: http://gist.github.com/mogsdad/39db2995989110537868

When adopting code without changes, it’s sufficient to say where we obtained it. If we’re making changes, we should at least indicate we’ve adapted the code, and may optionally include details of our changes.

You’ll note I said ultimate source. Since good code has a tendency to get copied, there’s a chance that a code snippet that we’ve found is itself a copy from some other website or repository. By investing the effort to locate the earliest source of a piece of code, we make sure that we clearly identify the correct origination.

Attribution on StackOverflow

One of the most common sources of Google Apps Script code snippets (one-liners, blocks, functions, all the way up to whole small programs) is StackOverflow.com. It’s worthwhile to consider the attribution requirements for code you may contribute to or extract from there.

The Creative Commons license that applies to StackOverflow content includes a clause requiring attribution in its Terms. I believe that we should provide attribution for code we pick up from StackOverflow, whether or not code is considered an excerpt of full questions or answers.

A link to the original question or answer that contained the code should be sufficient. For example:

// extract extension using RegEx 
// from http://stackoverflow.com/a/680982/1677912 
var extensionfinder = /(?:\.([^.]+))?$/;
var ext = extensionfinder(fname)[1];

Say you’re asking a question about a script that you originally found in an answer on StackOverflow. By including that attribution right in the code you post in your new question, you can help set the context for anyone helping you, and for future readers who may get a richer understanding of the problem through links back to the answers that provided your code.

Here’s a little bit of magic: StackOverflow will automatically provide a “linked questions” list on sidebar for questions & answers you reference in a question. This includes references via code comments, as long as the comment includes the text of a URL for a question or answer! (I stumbled onto this, and haven’t found any documentation about it.) This is neat because it facilitates automatic cross-referencing to answers that you’ve adapted, and provides clear attribution without distracting from the core of your undoubtedly fantastic answer.

This may be obvious and is arguably a waste of words, but here it is: I’m not a lawyer, and nothing in this article constitutes legal advice. 

As a developer, my advice when you decide to re-use another’s code is this:

  1. Ensure that you know what license the creator or author applied. This may require that you do some digging beyond grabbing the code you’ve found on the interwebs, in case some intermediary has ignored the license themselves. Their oversight in doing so can become your problem.
  2. Follow through with all licensing obligations. Source attribution is the topic of this article, and is a common requirement of open licenses, but it’s not the only one. The GPL, for example, requires that derived works must be distributed under the same terms as any GPL-licensed snippet; this may be problematic if you also include pieces that are incompatible with those terms.
  3. Make decisions within your scope. If you are an employee, your employer may have concerns about the obligations that you may confer on them by your decision to use third party code. Be sure it’s your decision to make, before you make it.

Even if you’re developing a tiny little script just for your own personal use, and never expect to make it public in any way, make it a habit to be aware of the licensing of any software you adopt or adapt, and the obligations that fall upon you.

Including a disclaimer saying something like “I don’t claim the copyright on this code” is NOT a way to conform with any license, nor is it respectful to the original author.

Closing remarks

Copyright and software licensing is messy – I’m not convinced lawyers understand it fully, but I know that I don’t. That may make it difficult to know what is the “right thing to do” when reusing code written by other developers, but we can at least follow the golden rule, and treat those developers as we’d like to be treated. Give credit where credit is due – let’s all include attribution notices right in our source code.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s