When should I cite, if using code from Stack Overflow? I already found a question handling the case if you got some information from Stack Exchange for my thesis. Unfortunately I did not find anything about the case that I get a solution (not necessarily from stackexchange) for a specific problem.

For example, if I have a problem with a specific part in my code, or if I have to design a circuit, and I run into a problem: What is the threshold of information, i.e. when should I cite the answer in my thesis? Should I always cite Stack Overflow (or the other web page), even if its just for two lines? Where is the threshold?  

And how should I do this in my code? Should I rather make a comment block around the specific part of the code, redirecting to SO, or should I simply cite it in my thesis?
The important distinction to make here is between:


Intellectual contributions to the actual research/science.
Technical help.


Contributions of the first kind are – roughly speaking – what warrants authorship or, if the contribution is already published, a citation. With other words: If the contribution would warrant authorship if unpublished, it has to be cited. As already stated, this is somewhat simplified and distinguishing between when to cite and when to include as an author can sometimes be tricky, but it should give you a rough idea, as to what you have to cite. Also note that there are other reasons for citing, such as context embedding, which do not apply here, however.

Two lines of code (to take this example) almost always fall under contributions of the second kind: They help you with the tools you need to use for your research but not with the actual subject of your research. It’s roughly comparable to the contribution of somebody fixing the microscope you need for some biological research. Of course, it might be a very helpful contribution that saved you months of work (in which case you should consider mentioning it in the acknowledgments), but it’s not a scientific contribution.

However, the decision may not always be that easy. Apart from the aformentioned authorship criterion (also note the authorship tag on this site for this), asking yourself the following questions might help to solve the question:


Is the kind of contribution to your work fundamentally different from:


designing the programming language you used,
designing or building your CPU,
designing or building your microscope?


If yes, consider citing.  
Does the contribution affect the results of your research or is it necessary for others to reproduce your research? By results I here mean something like:


  The 666th digit of π is 6.
  The proposed algorithm outperforms the current standard method.


and not:


  I managed to compute the results before the deadline.
  Unfortunately, the results do not make any sense, probably due to some bug.


If yes, then you should consider citing. If, on the other hand, you have good reason to assume that somebody implementing your solution in a totally different programming language or calculating everything with pen and paper would arrive at the same results, then you should not cite.


Contributions of the second kind are what, amongst others, you can put in your acknowledgements, where in a thesis it’s usually up to you, who and how many people you mention.