59

I use SO a lot, and I find it appropriate to cite the authors of some code/algorithm/method/ when I re-use it. When the question only has one answer, or contains itself relevant information, I just link to the full question page using the URL as shown in the browser.

But when I want to link to a specific answer, I have to either:

  • use the "share" button as-is. (This is horrible as the question title is hidden AND it leaks my user ID).

  • click the "share" button, copy the URL, paste it and let the browser resolve that to the full URL to the answer (cumbersome, and the URL contains duplicated anchor ID).

  • hack the source and build the URL myself (I did that before I realized I could let the browser resolve the "share" thing).

Not only none of these processes is satisfying, but I think this hinders a process that we should all be naturally following while programming and getting the help of others: proper citation.

Currently, not only do I link to the answer/question, but I also try to cite the author. Even if it's a nickname, but except if it's "userXYZ", I put something like (real example):

// Credit: Chris Lutz
// http://stackoverflow.com/questions/1493936/faster-approach-to-checking-for-an-all-zero-buffer-in-c/1493988#1493988
bool isBufferEmpty(uint8_t* buf, uint16_t size)
{
    assert(buf);
    assert(size >= 1);
    return buf[0] == 0 && memcmp(buf, buf + 1, size - 1) == 0;
}

The problem is that this format varies (depending on the day, my mood, my typos, etc.) Scientific literature has very strict guidelines (and requirements for citations). I think we could be inspired by this - although we don't need to be as rigorous.

Now my question:

Can we have a "cite" button, ideally located just next to the "share" button, that provides a proper citation with the name of the author(s), possibly the date, and the full URL to the answer ? We should be able to paste the snippet as-is directly in our code, with minimal need for reformatting. The dialog could typically have a few configuration buttons for the style of comment (maybe //, /* */, # and %)

Edit (clarifying why I think long, descriptive URLs are better than short permalinks, and also why I never use URL shorteners): I think there are two arguments here.

  • The first one is cognitive. When one reads source code, or any other form of documentation, one does not have enough time to read all references - this is just impossible. Back when I was a researcher, I would scrape the list of references, then maybe peek at 2-3 papers (of course it depended on how on-topic with my research the paper was). I think the same applies to source code when reviewed by me or my peers. If, as a reviewer, I see a short URL, I have no idea what is there, if it's relevant to what I am doing or not. If I see a descriptive title, I can read the content linked to if I am interested. I simply have more information to drive my decisions. Just imagine if reference in papers were only DOIs (document identifiers)!

  • The second may be of less importance, but anyway: if for some reason the content disappears (because the question is deleted, because StackOverflow goes out of business, etc.) we still have some keywords. Chances are there is some similar content somewhere on the web. The key is: these keywords can help you be back on track faster if the source disappears.

10
  • 15
    Question titles can change, so your preferred long form of the URL will not necessarily match the actual question for perpetuity. I don't see the problem with "leaking" your user ID, since it's available for all to see in your profile. Can you clarify why you dislike the "share" URL? Commented Feb 9, 2016 at 15:46
  • 2
    @ThisSuitIsBlackNot: I don't care if the question title changes over time, it will be still way more descriptive of the content I am linking to than raw numbers (plus the percentage of affected questions is very low). The user ID is useless, it is just noise in that context. I am not documenting my code saying "user Tibo likes this post", I am saying "you will find information here" or "this is the source". I dislike the "share" URL because I am not "sharing" anything, I am citing a post on the web just as I would cite a paper in a journal. Do these have links back to the person who cites?
    – user948581
    Commented Feb 9, 2016 at 15:55
  • 1
    That makes sense, thanks for the explanation. I think that would be good info to include in your question; it wasn't entirely clear to me why the shortened URL was unacceptable. Commented Feb 9, 2016 at 16:02
  • 3
    Even with the clarification, I still disagree with your dislike of the shortened URLs. If you're leaving this in source code, it most likely will stay there for a long while. That will increase the likelihood of those titles changing. Also, a question may only have one answer NOW, but that could easily change, even on old questions. I have done the same thing, and just used that "Share" link. That also keeps your comments more concise.
    – krillgar
    Commented Feb 9, 2016 at 16:05
  • 4
    @Tibo As far as I'm aware, the Share link contains the UserID solely for badge counting (there are badges for sharing links viewed by x-number of people).
    – J...
    Commented Feb 9, 2016 at 16:42
  • 2
    @krillgar http://meta.stackoverflow.com/questions/316496/citation-for-linking-to-answers/316529#316529 is a link to Michael Dibbets's answer to this question. It doesn't matter if somebody else adds an answer, that link will always point to Michael's answer (unless the answer is deleted). If the title changes, the link will still work; for the OP's use case, as long as the title in the URL is a clear description of the problem, I don't think it would be a big deal if the title is changed to not match the original link. Commented Feb 9, 2016 at 18:21
  • 3
    For what is worth, you can delete your user ID from the "Share" link. Both http://meta.stackoverflow.com/q/316496/1935077 and http://meta.stackoverflow.com/q/316496 works the same (except it won't count for badges).
    – Petr R.
    Commented Feb 9, 2016 at 20:17
  • 1
    @krillgar: I don't really see why title changes would be an issue. Currently, you can use pretty much anything you want as a title, and SO will point you at the right spot given you are using the appropriate IDs (check http://meta.stackoverflow.com/questions/316496/the-sum-of-all-unicorns-is-42#316529). So if the title changes, the URL will still work. Besides, I consider the short URL obfuscated rather than concise. (OTOH, there are sites that have awful deep URLs, but that's not the case of SO).
    – user948581
    Commented Feb 9, 2016 at 22:07
  • @Tibo :( I was really hoping to see the code used to calculate the sum of all unicorns. Commented Feb 9, 2016 at 23:08
  • 5
    Math.SE actually has this, but it outputs BibTeX and the like rather than raw URLs.
    – Kevin
    Commented Feb 9, 2016 at 23:27

2 Answers 2

13

Since we are suggesting possible userscripts for this, I figured I would offer one as well. In addition, this uses the CMOS style for online attribution. For those not aware, almost every aspect of writing is covered in the Chicago Manual of Style - it is a great resource.

In my opinion, the closest official citation structure for Stack Overflow is the blog style (http://www.chicagomanualofstyle.org/tools_citationguide.html)

  1. Jack, February 25, 2010 (7:03 p.m.), comment on Richard Posner, “Double Exports in Five Years?,” The Becker-Posner Blog, February 21, 2010, http://uchicagolaw.typepad.com/beckerposner/2010/02/double-exports-in-five-years-posner.html.

In the example post you list, this format would look like

Chris Lutz, February 10th, 2016, answer on robjb, "Faster approach to checking for an all-zero buffer in C?", Stack Overflow, Sep 29 '09, https://stackoverflow.com/a/1493988.

In order to accomplish this structure, everything already exists on the page. Also note that the issue of vagueness from the link is gone because the title of the question is included in the design.

This could be included using an existing framework in a link like this

enter image description here

And the popup for existing "share" could also be used

enter image description here

Explanation (not for the feint of heart)

We will be inserting a cite link in between every share and edit link on every answer. The cite link will open the same dialog that share opens. The text for copying will be the formatted CMOS citation. The dialog will behave the same as the share dialog.

Same dialog

This part is easy, simply copied the html for the dialog. Placed it into a template pattern. Done. You can see this in the citeDialogFactory object.

Same behavior

Okay, so let's set out doing that in a non hackish way. The first thing is to use the existing Stack Exchange constructs for their menus. [...] Processing complete, located their menu code. The piece we need is the menu removal process, and that is seen below in the variable named closeHandler.

Every answer has a link

This part is pretty easy. Took the code for the share link, modified it for the cite link, made another template factory for it, and then added one in between each share and edit link.

End Result

Package this on your own, or download a free extension of this exact code and nothing else for chrome here: https://chrome.google.com/webstore/detail/stackcite/olmokikmchbjafcnlngakgnohcoejcfj?hl=en-US

The code.

(function(){

    var closeHandler = function(t, n) {
        if (t) {
            var i = $(t).filter(":not(.popup-stack-hidden)");
            if (!i.length) return;
            i.each(function(e, t) {
                var n = $(t),
                    a = n.data("_popup"),
                    r = n.data("_lightbox");
                a && (i = i.add(a)), r && (i = i.add(r))
            }), i = $($.unique(i.get())), i = i.not(".popup-closing").addClass("popup-closing");
            var a = {
                    "closeTrigger": n
                },
                r = $.Event("popupClosing", a);
            if (i.trigger(r), r.isDefaultPrevented()) return i.removeClass("popup-closing"), void 0;
            i = i.not(".popup-closed").addClass("popup-closed"), i.filter(":not(.esc-hide)").fadeOutAndRemove(), i.filter(".esc-hide").fadeOut(function() {
                i.removeClass("popup-closing").removeClass("popup-closed")
            }), i.trigger("popupClose", a)
        }
    };

    var citeDialogFactory = new function(){
        var template = $('<div class="share-tip cite-tip">cite this answer<input type="text" style="display:block; width:292px;" value="https://stackoverflow.com/a/1498741/1026459"><div id="share-icons"></div><a class="close-share-tip" style="float:right">close</a></div>');
        this.Create = function(answer){
            var dialog = template.clone();
            var username = $(answer).closest('.answercell').find('.user-details a:first').text();
            var today = new Date().toDateString();
            var asker = $('.user-details a','.post-signature.owner').text();
            var title = $("#question-header").text().trim();
            var askdate = $('.user-action-time span:first','.post-signature.owner').text();
            var answerlink = $(answer).closest('.answercell').find('.short-link:first')[0].href;
            var citestr = [username,today,asker,'"'+title+'"',askdate,answerlink].join(', ');
            dialog.find('input:first').val(citestr);
            $('.close-share-tip',dialog).click(function(){ closeHandler('.cite-tip'); });
            return dialog;
        };
    }

    var citeLinkFactory = new function(){
        var template = $('<a title="citation for this answer" class="cite-link">cite</a><span class="lsep">|</span>');
        this.Create = function(){
            var link = template.clone();
            link.click(function(){
                var dialog = citeDialogFactory.Create(link);
                $(this.parentNode).append(dialog);
            });
            return link;
        };
    };

    $('.lsep:first','.answercell').each(function(){
        citeLinkFactory.Create().insertAfter(this);
    })

})()
0
6

A simple script to hand you the variables you need, user name, post link, etc...

Maybe not exactly what you want, but it might be something to build upon in the meanwhile.

$('.answer').each(function() {

  /** 
    * generic preprocessing of data to filter out the raw data
    */
  var $answer = $(this);
  var $button = $('<button>Click to get citation details</button>');
  var link = document.location.href + '#' + $answer.attr('id');

  var $author = $answer.find('.user-info');
  var authordetails = {};
  var $link = $author.find('.user-details').find('a').first();
  authordetails['name'] = $link.text();
  authordetails['profile'] = document.location.host + $link.attr('href');

  /**
    * The final object that'll get passed to our clean event 
    */
  var details = {author:authordetails, 
                 link: link,
                 answer: $answer
               };


  $button.on('click',function() {
      $answer.trigger({type:'quoteDetails',details:details});
  });

  /**
   * possibly add some css to the button to align it at a desirable spot
   */
  $answer.append($button);

});

$('.answer').on('quoteDetails',function(event) {
    /** Basically do here whatever you want, popup a div, paste it in your clipboard, whatever... **/
    console.log(event.details);
});

Maybe make a bookmarklet out of it(minfy it and preceed it with javascript: in your bookmarks manager) Or make it into a stack snippet.

2
  • 5
    This is a smart hack. Although a built-in solution would certainly be better for the community, I like this. Unmaintainable, hard to share, but deliciously hacky :)
    – user948581
    Commented Feb 9, 2016 at 22:14
  • 2
    Keep calm and hack the world :-) Commented Feb 9, 2016 at 22:35

You must log in to answer this question.