6

I am just getting into using Github to showcase my code to potential employers. The primary use of my Github is really just to show employers my coding ability.

What is the right thing to do when I copy and paste a chunk of code from a site like stackoverflow? Should I add some sort of a comment?

Here is an example

I am looking for a fast way to calculate the ceiling of an integer division and copy and paste an answer into my code and upload it to Github. Should it look something like this?

q = (x + y - 1) / y;//taken from https://stackoverflow.com/questions/2745074/fast-ceiling-of-an-integer-division-in-c-c

Or I want a function that compares floats to double

/*function found at https://stackoverflow.com/questions/17333/most-effective-way-for-float-and-double-comparison*/
bool AreSame(double a, double b)
{
    return fabs(a - b) < EPSILON;
}

At work I know other people copy from Stack Overflow to solve a problem and I don't consider this immoral. But I do feel unfair if I copy it, put it on my Github and use it to get hired.

Considerations:

  1. I try to understand the code that I'm copying.
  2. I can try to obfuscate that I copied and rename things but this doesn't make it feel fair to me.
  3. I can somewhere mention that I get answers from SO

The above examples are fairly small but I have seen larger segments of code offered on other sites. I mean I'm not going to copy a full program and make it look like my own, but where is the line crossed from "inspired by someone else's code" to copying it? I don't consider changing around variables to really make it better.

So I'm asking 2 questions

  1. When should I cite a function that is more or less copied?
  2. How should I do it?
8
  • Can you reference it in the top of the file? I think that would be better than somewhere in the code. It looks a little messy
    – Sabine
    Commented Mar 24, 2016 at 9:34
  • 1
    Your example of comparing floats within an epsiolon tolerance does not need to be cited. But I suppose you could if you want. It would give too much credit to whoever posted that in Stack Overflow.
    – Brandin
    Commented Mar 24, 2016 at 9:35
  • 4
    That's being heavily discussed :) meta.stackexchange.com/questions/272956/…
    – Ajasja
    Commented Mar 24, 2016 at 9:48
  • I would be hesitant in displaying your stackoverflow skills. There are a certain subset of managers who despise the use of stackoverflow since they feel people can just copy and past things. I would instead make your own application. Something simple with a common task that everyone understands like building a baseball team, or writing a quick API that returns the time. The end result is showcasing your knowledge of building applications and finding a solution to a task.
    – Dan
    Commented Mar 24, 2016 at 12:27
  • 1
    Not enough for an answer, but this is in the doc block for a method in a project I'm working on now * Search through an array of objects to see if a given index matches a given value * http://stackoverflow.com/questions/4742903/php-find-entry-by-object-property-from-a-array-of-objects I modified the method slightly from what's there, but if I or another developer ever need help with what's going on, and why I chose that code, we know where to look. Commented Mar 24, 2016 at 12:56

3 Answers 3

4

From my point of view, copying code is a perfectly valid thing to do, as long as it solves the problem at hand, i.e. if you change the issue at hand to have the code fit is not a good approach. Finding a solution that works in 5 minutes is more efficient than writing a solution that works in 5 hours. As long as you understand how the code works (like you mention).

Citing the source of where you copied it from is the right thing to do. If you used a source but modified the code, you should still specify that you've used it as an inspiration.

Now, about the format of the citation. Really, it doesn't matter, as long as you're consistent across all your code. An employer will like to see that you're thorough, ethical, consistent and can follow a coding standard. (Make sure you also make the code you copied fit in your own coding standard: indentation, casing, braces positions, spaces, etc.)

2

I answered a meta question a while back asking a similar question.

Stack Overflow presents knowledge. Just like a book. If you gain knowledge from a book, then using this knowledge doesn't need citation. You don't cite your grammar school book on use of the English language with any word you type either, right?

Now if you quote verbatim, you need citations. But only on something that actually has creative value. If I quote Shakespeare, I better do so properly. But somebody has said "good morning" first. And I sure won't mention his name every morning for about 10-20 times.

Your examples are things that are really just common knowledge. Comparing floating point values? Calculating something from two integers? That's not things worth attributing. Everybody does that. It is obvious that nobody had programming just implanted. We all learned it somewhere and from somebody.

If you actually learn something from Stack Overflow (and I realize not everybody does), you don't need to copy it verbatim. You will adapt it to your needs, even if your needs are just different variable naming or braces. And if you adapt what you learned, you don't need to attribute it to someone. It's your product, even if you had help creating it. We all have help creating things. Nobody can create things out of thin air.

4
  • 3
    From now on I when I write "Good morning!" I will put a link back to this page, to let everyone know that I have adapted the phrase from the idea posted here. Changes made: Capitalised the first word and added an exclamation point. Date of last change: 24 March 2016 13:27:25 GMT.
    – Brandin
    Commented Mar 24, 2016 at 13:27
  • It sometimes confuses me, we're born knowing nothing so anything we know we learned from somewhere. How do you know when you're supposed to cite something?
    – BruceD
    Commented Mar 25, 2016 at 1:10
  • @BruceD If you quote Shakespeare, you need to cite him. If you read so much Shakespeare you are talking like him, you don't.
    – nvoigt
    Commented Mar 25, 2016 at 7:22
  • @nvoigt say I only ever read one book with an example of implementing a linked list in C. I study it well. I then, without referring too the book, implement the linked list in a small project. Should the book be sited?
    – BruceD
    Commented Mar 25, 2016 at 11:50
0

Part of being a professional software developer is having a handle on software licenses and how code can be reused. If you abide by these rules, you can easily argue to a hiring manager that you followed standard operating procedure.

Each website will have a different license, but this recommendation is provided when pulling code from Stack Overflow (or any Stack Exchange site):

But to future-proof your work, we recommend you do one of these 2 things, or both:

A) Add a comment to your code that links back to the post where you found it, or

B) Comply with the MIT as it’s typically used, by including the full license text in your source

You’re doing option A already, right? This is just standard operating procedure when it comes to finding code on the internet – a hyperlink comment ensures you’ll be able to debug down the line. But under the new terms a hyperlink comment is more than just pragmatic, it’s a hat-tip, and it’s a tit-for-tat that ensures you’ve complied with a contributor’s terms

In general, using code from the internet is fine but I would caution you about overdoing it. If every function in your repository contains a citation to Stack Overflow, I would question your ability to perform on more specific problems.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .