你遇到过的源代码中最好的注释是什么?


当前回答

下面是我在不同时间放入代码中的一些代码。有些不是严格意义上的评论,但它们是相同的概念。

在一个跨平台项目中,只需要在一个平台上执行一些特殊代码:

//If defined, will include all the Windows-specific code.
#define LOSE

#ifdef LOSE
#include <windows.h> //WIN32. Duh.
#endif


---------------------------------------------------


//Stolen from other_project_name.cpp


---------------------------------------------------


/*
 * These comments have been lifted from propagate() and, though they no longer apply to the code, they may still be of value somewhere. Original tabbing and structural elements have been preserved.
 */
    //CAUTION: This has a major Bobby Tables risk. Even if a rulebuilder is used, there's still the risk of something getting corrupted in the database itself.
    //Reading text from anywhere and simply slotting it into an SQL statement is a major security risk. (With thanks to xkcd for the name "Bobby Tables".)
    //Requirement: Eliminate one Bobby Tables by changing [redacted] to be not just straight SQL.
[lots more comments that are not as funny]
/*
 * End of lifted comments. There should not be any executable code between these markers.
 */


---------------------------------------------------


        /*
        Okay. It's unrecognized. Why is this a fatal error? It's actually very closely akin to the miswart of botched #includes being a fatal. When writing a C/C++
        program, you need your headers, and if you don't have one, chances are there'll be a million cascaded errors; so by making "unable to open asdf.h" a fatal,
        the compiler suppresses all those errors about undefined symbols and potentially misspelled type names.
        */


---------------------------------------------------


    //If someone tries to import 'id' as a field name, it won't work. (We already have our own id.) But I think the probability is so low that I can afford to be funny.
    if (!stricmp(ptr,"id")) {warn(0,"Import","","'id' is a reserved word and cannot be used as a column name. (Try 'ego' or 'superego'.)"); return;}


---------------------------------------------------


//Need a place to squirrel away SQL statements somewhere
char *uts[1024]; //Unified Temporary Storage. (Why? Because I said so.)
int nuts=0; //What is it that squirrels keep? Ha!
int utsid[sizeof uts/sizeof *uts];


---------------------------------------------------


        /**************************************\
         * NOTE: This sets tilde.action. If a *
         * tilde header does not exist in the *
         * import file (not the _content_, if *
         * the entire column isn't there), it *
         * will duplicate down through all of *
         * the rows. This is fine for ~id, as *
         * that will never be changed; and if *
         * ~Quantity is blank, that throws an *
         * error in 'Add'. With ~Action, I am *
         * not so certain. I THINK it'd be OK *
         * to dup-down most of the time... if *
         * the user only ever imports Adds or *
         * Revises, but never both at once in *
         * a single import. So for safety, to *
         * allow a blank ~Action to revise OR *
         * add, I'm breaking the check out to *
         * a new variable - the curaction. In *
         * most cases, it won't be needed, so *
         * it's a waste; but it isn't like it *
         * has to copy the entire tilde.*, so *
         * it's only a small waste. So it can *
         * waste a register... big deal. OK ! *
        \**************************************/


---------------------------------------------------


            //if (!response) // we're going to crash
            //if (!items) // we're going to crash
            //TODO: Don't crash


---------------------------------------------------

我的很多评论都隐晦地提到了电影或音乐剧,但如果你不了解这部剧,它们就不会那么有趣了。

其他回答

写得很好的Lisp是最容易阅读的语言之一,我喜欢它。但是写得不好的Lisp可能是比糟糕的Java更糟糕的噩梦,等等。

在这里,我们需要创建一个“组文件”,如果一个原始文件有3个变体,后缀为a, b和c。我一直试图追踪一个奇怪的错误,我们得到了不必要的组文件……

  (let ((varianta (format nil "~aa" problem))
        (variantb (format nil "~ab" problem))
        (variantc (format nil "~ac" problem)))
    ;;if the A and B variants exist, create a group file
    ;;(why not just check for a? I don't know, this just feels right)
    (when (and (probe-file varianta)
               (probe-file variantb))
      ...)))

Bug: 1, Gut: 0。

显然,写这篇文章的人没有想到,也许检查所有三种变体是一个好主意。当然,这是我在最初编写这段代码十年之后(它比第一个SVN日志更早)最终追踪到的错误。

我在遗留代码中注意到的修改日志

05/17/99 d琼斯评论出来 血腥的auzies代码(02/19/99) k罗宾逊该死的德克萨斯人 不能正确拼写aussie(没有代码 改变-只是一个评论)

在drivers/net/sunhme.c (Linux内核)中:

/* Welcome to Sun Microsystems, can I take your order please? */
if(!hp->happy_flags & HFLAG_FENABLE)
        return happy_meal_bb_write(hp, tregs, reg, value);

/* Would you like fries with that? */
hme_write32(hp, &tregs->frame,
            (FRAME_WRITE | (hp->paddr << 23) |
             ((reg & 0xff) << 18) | (value & 0xffff)));
while(!(hme_read32(hp, &tregs->frame) & 0x10000) && --tries)
        udelay(20);

/* Anything else? */
if(!tries)
        printk(KERN_ERR "happy meal: Aieee, transceiver MIF write bolixed\n");

/* Fifty-two cents is your change, have a nice day. */
// Empty constructor to satisfy the stupid compiler
 Public ServletHandlerClass () { } 
when :orientation
## Avoid matching gay people with straight people - they hate it, they do, they really do.
query_parameter = "(users.orientation = 'Bi' OR (users.orientation = 'Straight' AND users.gender IN ('#{user.opposite_genders.join('\',\'')}')) OR (users.orientation = 'Gay' AND users.gender IN ('#{user.same_genders.join('\',\'')}')))"

从一个约会网站…