Writing Code

Writing code is similar to academic writing in that when you use or adapt code developed by someone else as part of your project, you must cite your source. However, instead of quoting or paraphrasing a source, you include an inline comment in the code. These comments not only ensure you are giving proper credit, but help with code understanding and debugging.


When should I cite a source in my code?

  • When you copy code from an external source. Whether you are copying a snippet of code or an entire module, you should credit the source.

  • When you copy the code and adapt it, you should still credit the source. You were not the original developer of the code.


How should I cite the code?

  • Generally, the URL and the date of retrieval are sufficient. Add more details if it will help the reader get a clearer understanding of the source.

  • If you adapted the code, you should indicate “Adapted from:” or “Based on” so it is understood that you modified the code.

  • Your instructor may have specific instructions on how you should or should not cite your sources. If you are not clear on what is acceptable, ask your instructor.


Use of Open Source Software

When you use code from an open source project, you need both to attribute the source and follow the terms of any open source license that applies to the code you are using. Keep in mind:

  • When you download the source, the license is typically part of the download.

  • Also, the source code itself will typically contain the copyright and terms of use.

  • When you incorporate open-source-licensed code into a program, it is good practice to duplicate the copyright in your code, and/or store the license in a file with the code.

  • If you don’t obtain the license with the download, you should be able to find it on the site of the open source project, such as Apache HTTP Server site, or on the Open Software Initiative (OSI) site.


Instructors determine the specific expectations around re-use of code in each class.

Often, the requirements are described in the collaboration policy for the class. If policy is not clearly described in the course materials and you are not sure what is acceptable, ask your instructor.

Collaboration policy from Spring 2012 6.005 Elements of Software Construction:

(used with permission of  Professors Saman Amarasinghe and Armando Solar Lezama, Dept of Electrical Engineering & Computer Science)

For the team projects, you are encouraged to collaborate with your partners on all aspects of the work, and each of you is expected to contribute a roughly equal share to design and implementation. You may reuse designs, ideas and code from your own work earlier in the semester (even if it was done in a team project with a different partner). You may also use material from external sources, so long as: (1) the material is available to all students in the class; (2) you give proper attribution; and (3) the assignment itself allows it. In particular, if the assignment says "implement X," then you must create your own X, not reuse someone else's.

Problem sets are intended to be primarily individual efforts. You are encouraged to discuss approaches with other students but your write-up must be your own. You should not make use of any written solutions or partial solutions produced by others. Material from external sources can also be used with proper attribution, but only if the assignment allows it. You may not use materials produced as course work by other students in the course, whether in this term or previous terms, nor may you provide work for other students to use. During code review, you will see classmates' solutions to a pset. While it is fine to take inspiration from their approach, do not copy their work.

Copying work, or knowingly making work available for copying, in contravention of this policy is a serious offense that may incur reduced grades, failing the course and disciplinary action.


Although it is common practice to adapt code examples found on the web,

  • You should never copy code from other students. Your peers are not considered an authorized source.

  • You should not simply re-use code as the solution to an assignment. Like academic writing, your code can incorporate the ideas of others but should reflect your original approach to the problem.


Examples of citing code sources:

Example 1:

In describing the method SwizzleMethods in the Mozilla Firefox nsToolkit source code, the developer notes the method is “Based on the ideas and code” from the CocoaDev wiki and includes a URL:

420 // An alternative to [NSObject poseAsClass:] that isn't deprecated on OS X

421 // Leopard and is available to 64-bit binaries on Leopard and above. Based on

422 // ideas and code from http://www.cocoadev.com/index.pl?MethodSwizzling.

423 // Since the Method type becomes an opaque type as of Objective-C 2.0, we'll

424 // have to switch to using accessor methods like method_exchangeImplementations()

425 // when we build 64-bit binaries that use Objective-C 2.0 (on and for Leopard

426 // and above) …//

(Source:  Mozilla Firefox source code http://lxr.mozilla.org/firefox/source/widget/src/cocoa/nsToolkit.mm#422 retrieved on Aug 12, 2012.)

Example 2:

In describing the class PluginProxyUtil in the Apache Project source code, the developer cites the source as a post in a forum and includes the URL, author and date:

/**

* A utility class that gives applets the ability to detect proxy host settings.

* This was adapted from a post from Chris Forster on 20030227 to a Sun Java

* forum here:

* http://forum.java.sun.com/thread.jspa?threadID=364342&tstart=120

[…]

*/

(Source: Apache Project source code http://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/contrib/org/apache/commons/httpclient/contrib/proxy/PluginProxyUtil.java retrieved on August 12, 2012.)

Example 3:

In the function OutputTraceToStream in the Google Chrome stack_trace_win source code, the developer cites the source code as the Microsoft Developer Network and includes a URL:

// Code adapted from MSDN example:

// http://msdn.microsoft.com/en-us/library/ms680578(VS.85).aspx 



(Source: Google Chrome source code http://src.chromium.org/svn/trunk/src/base/debug/stack_trace_win.cc retrieved on, August 12, 2012.)


Example of open-source-licensed code:

At the top of the Google Chrome stack_trace_win source file, note the copyright and reference to the open source license:

// Copyright (c) 2012 The Chromium Authors. All rights reserved.

// Use of this source code is governed by a BSD-style license that can be

// found in the LICENSE file.

If you incorporate this code into a program, you should follow the terms outlined in The Chromium Authors' open source license file, which is shown below. While this license only requires that you duplicate the copyright and license if you are redistributing the code, it is good practice to always duplicate the copyright in your code, and/or store the license in a file with the code. This way, if you want to redistribute the code later, intellectual property reviewing becomes much easier.

// Copyright (c) 2012 The Chromium Authors. All rights reserved.

//

// Redistribution and use in source and binary forms, with or without

// modification, are permitted provided that the following conditions are

// met:

//

//* Redistributions of source code must retain the above copyright

// notice, this list of conditions and the following disclaimer.

//* Redistributions in binary form must reproduce the above

// copyright notice, this list of conditions and the following disclaimer

// in the documentation and/or other materials provided with the

// distribution.

//* Neither the name of Google Inc. nor the names of its

// contributors may be used to endorse or promote products derived from

// this software without specific prior written permission.

//

//

// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS

// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR

// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT

// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,

// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT

// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,

// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY

// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT

// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE

// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

//

(Source: The Chromium Authors license file http://src.chromium.org/svn/trunk/src/LICENSE retrieved on, August 12, 2012.)