V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
LinkT
V2EX  ›  程序员

写注释也要解放

  •  
  •   LinkT · 210 天前 · 3294 次点击
    这是一个创建于 210 天前的主题,其中的信息可能已经有所发展或是发生改变。

    不知道诸位公司有没有这样的要求,所有的代码改动都需要加注释。 比如这样:

    diff --git a/test.java b/test.java
    index 70fd330..941c28d 100644
    --- a/test.java
    +++ b/test.java
    @@ -1,5 +1,7 @@
     public class HelloWorld {
         public static void main(String[] args) {
    -        System.out.println("World, Hello!");
    +        //zhangsan modify this for xxxx. 2023-02-26 start
    +        System.out.println("Hello, World!");
    +        //zhangsan modify this for xxxx. 2023-02-26 end
         }
     }
    

    一般这种注释都是最终代码验证 ok 上传时才加。 如果代码的修改分散在很多不同的位置,那么加这个注释就很痛苦。

    作为懒人,实在不想做这种麻烦事,于是写了个小脚本为添加到暂存区的修改添加注释:

    使用方法

    ./icomment path "comment"
    

    发出来给各位大佬看看还有没有可以改进的地方,请尽情批评。 https://github.com/LinkTTT/icomment

    22 条回复    2023-02-27 17:53:55 +08:00
    yolee599
        1
    yolee599  
       210 天前 via Android
    个人觉得这种注释很丑陋,多此一举,在 commit 填写描述就行了。
    LinkT
        2
    LinkT  
    OP
       210 天前
    @yolee599 是的,我也这么觉得,但是公司规定,没办法。
    f1ush
        3
    f1ush  
       210 天前
    这要是重构改几十个文件的时候,不是看着人都麻了
    LinkT
        4
    LinkT  
    OP
       210 天前
    @f1ush 是的,但是你说这种情况是小 case.
    最恐怖的是注释外面套注释,我和我另一个同事戏称为:叠杀人书。 哈哈哈哈~
    yan17
        5
    yan17  
       210 天前
    公司这规定有点离谱啊,那要是后续再次修改,原来的注释要删除吗?
    ```
    public static void main(String[] args) {
    //zhangsan modify this for xxxx. 2023-02-26 start
    + //zhangsan modify this for xxxx. 2023-02-27 start
    + System.out.println("Bye, World! ");
    + //zhangsan modify this for xxxx. 2023-02-27 end
    //zhangsan modify this for xxxx. 2023-02-26 end
    }
    }
    ```
    不删不就变这样了?
    jfcherng
        6
    jfcherng  
       210 天前   ❤️ 1
    人肉 git blame?
    f1ush
        7
    f1ush  
       210 天前
    @LinkT 套注释?后续再修改真不删啊,那还需要 git 做什么啊哈哈哈
    aver4vex
        8
    aver4vex  
       210 天前
    见过。也写过。确实比较头大。
    Pastsong
        9
    Pastsong  
       210 天前
    是你们公司没人会用 git blame ?还是你们不用 git
    codingbody
        10
    codingbody  
       210 天前 via iPhone
    你们公司是人工版本控制嚒
    adoyle
        11
    adoyle  
       210 天前   ❤️ 1
    喊你们的 CTO 来给大家解解惑。干嘛不用 git blame 或者 git log -S ,非要用这套蹩脚的注释。
    如果一次提交只是删除代码是不是还得加上两行注释?多删改几次这文件不就满屏都是注释了?

    如果不会用命令行操作,那我推荐 [git-history]( https://github.com/pomber/git-history),网页浏览变更历史,能本地部署,也有 VS Code 插件。不香么?
    MajestySolor
        12
    MajestySolor  
       210 天前
    传说中的手工版本控制🐶
    vsyf
        13
    vsyf  
       210 天前 via Android
    我猜是 aosp ,chromium 之类吧。
    darkengine
        14
    darkengine  
       210 天前
    前公司会让加 JIRA 上的 issue 号,其实比较合理,看到注释之后可以很方便的查到为什么要改。
    imnpc
        15
    imnpc  
       210 天前
    这个是在不用 git 的时代采用的方法, 有了 git 以后 不会在用这种了
    xuanbg
        16
    xuanbg  
       210 天前
    没 Git 的时候也不需要这么写。又没写原来是啥 xx 改了啥,有个 P 用。
    Ally
        17
    Ally  
       210 天前
    破规定,毫无用处,还拉低效率
    cp19890714
        18
    cp19890714  
       209 天前
    git 是摆设吗?
    Andy223
        19
    Andy223  
       209 天前
    why don't u use 'git blame'?
    hxndg
        20
    hxndg  
       209 天前   ❤️ 1
    我印象中,无论是 gitblame 还是 svn 都有渴死的功能吧。。。。
    这以后代码多了就变成一层一层的梯田了吧。。。。
    simplereasy
        21
    simplereasy  
       209 天前
    大改动应该写
    Goat121
        22
    Goat121  
       209 天前
    你领导咋想的啊,这种代码看一眼都觉得烦,还怎么工作
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3738 人在线   最高记录 6067   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 04:22 · PVG 12:22 · LAX 21:22 · JFK 00:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.