V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
go522000
V2EX  ›  问与答

麻烦哪位截取一下图,让我看看什么才算是优雅的代码,不限语言。

  •  
  •   go522000 · 2023-07-07 17:08:39 +08:00 · 1831 次点击
    这是一个创建于 385 天前的主题,其中的信息可能已经有所发展或是发生改变。

    没有挑剔的意思,单纯想看看了解一下。

    比如 redis 的代码,我就觉得这代码相当漂亮,工整,注释也写得好。当然也有人不这么认为,觉得注释太多,觉得好的代码不需要注释等等。

    萝卜白菜各有所爱,希望各位截下图或分享一下代码。

    18 条回复    2023-07-07 23:27:35 +08:00
    zzNaLOGIC
        1
    zzNaLOGIC  
       2023-07-07 17:20:56 +08:00   ❤️ 1
    大家都是摸鱼的时候吹牛逼才这么说的,你还当真了
    IceBay
        2
    IceBay  
       2023-07-07 17:27:23 +08:00   ❤️ 1
    自己写的时候,好的代码不需要注释。
    看别人的代码,该喷为什么不写注释了。
    alteremliu
        3
    alteremliu  
       2023-07-07 18:11:02 +08:00
    Zzyomirog
        4
    Zzyomirog  
       2023-07-07 18:14:34 +08:00
    @alteremliu 想到了睡眠排序
    tool2d
        5
    tool2d  
       2023-07-07 18:17:30 +08:00
    rookie4show
        6
    rookie4show  
       2023-07-07 18:21:19 +08:00
    编程之美吧
    neptuno
        7
    neptuno  
       2023-07-07 18:27:43 +08:00 via iPhone
    @Zzyomirog #4 还有猴子排序
    likeme
        8
    likeme  
       2023-07-07 18:34:14 +08:00
    有没有 Java 项目看看的
    TWorldIsNButThis
        9
    TWorldIsNButThis  
       2023-07-07 18:50:45 +08:00 via iPhone
    代数建模
    业务与代码同构
    LaTero
        10
    LaTero  
       2023-07-07 18:59:31 +08:00 via Android
    BQN ,APL 之类的语言够不够优雅
    zachlhb
        11
    zachlhb  
       2023-07-07 18:59:47 +08:00 via Android
    自己写的代码永远最优雅,别人写的什么玩意
    YsHaNg
        12
    YsHaNg  
       2023-07-07 19:00:49 +08:00 via iPhone
    Prime Sieve in APL (2=+⌿0=(⍳X)∘.|⍳X)/⍳X
    storyxc
        13
    storyxc  
       2023-07-07 19:02:21 +08:00
    linus 在 TED 的一期节目里说了他自己认为有品味的代码:
    ```
    remove_list_entry(entry)
    {
    // The "indirect" pointer points to the
    // *address* of the thing we'll update

    indirect = &head;

    // Walk the list, looking for the thing that
    // points to the entry we want to remove

    while ((*indirect) != entry)) {
    indirect = &(*indirect)->next;
    }

    // .. and just remove it
    *indirect = entry->next;
    }
    ```

    与之相反,不够 good taste 的代码
    ```
    remove_list_entry(entry)
    {
    prev = NULL;
    walk = head;

    // Walk the list
    while (walk != entry) {
    prev = walk;
    walk = walk->next;
    }

    // Remove the entry by updating the
    // head or the previous entry
    if(!prev) {
    head = entry->next;
    } else {
    prev->next = entry->next;
    }
    }
    ```
    dingdangnao
        14
    dingdangnao  
       2023-07-07 19:13:04 +08:00 via iPhone   ❤️ 1
    if error return "OK"
    gowl
        15
    gowl  
       2023-07-07 20:09:54 +08:00
    dosomethingcool
        16
    dosomethingcool  
       2023-07-07 21:19:09 +08:00
    工作中项目代码最大的问题一般都是注释太少,写需求的时候一哪有那么多时间一行行读别人的代码
    cslive
        17
    cslive  
       2023-07-07 21:23:31 +08:00 via Android
    if true return true
    humpy
        18
    humpy  
       2023-07-07 23:27:35 +08:00
    垠神用 parser combinator 写的 lisp parser:

    (:: $open
    (@or (@~ "(") (@~ "[")))

    (:: $close
    (@or (@~ ")") (@~ "]")))

    (:: $non-parens
    (@and (@! $open) (@! $close)))

    (::= $parens 'sexp
    (@seq $open (@* $sexp) $close))

    (:: $sexp
    (@+ (@or $parens $non-parens)))

    (:: $program $sexp)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1115 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 23:35 · PVG 07:35 · LAX 16:35 · JFK 19:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.