微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么不注释源代码

如何解决为什么不注释源代码

每门编程课程都告诉我们,我们应该使用尽可能多的注释来使事情变得清晰,这才有意义。因为如果你以后再读它,你会忘记你做过什么。

为什么专业的源代码几乎没有注释?它不会让它更具可读性吗?

来自 C++ 的示例 (istream):

  template<typename _CharT,typename _Traits>
    class basic_istream : virtual public basic_ios<_CharT,_Traits>
    {
    public:
      // Types (inherited from basic_ios (27.4.4)):
      typedef _CharT                    char_type;
      typedef typename _Traits::int_type        int_type;
      typedef typename _Traits::pos_type        pos_type;
      typedef typename _Traits::off_type        off_type;
      typedef _Traits                   traits_type;

      // Non-standard Types:
      typedef basic_streambuf<_CharT,_Traits>      __streambuf_type;
      typedef basic_ios<_CharT,_Traits>        __ios_type;
      typedef basic_istream<_CharT,_Traits>        __istream_type;
      typedef num_get<_CharT,istreambuf_iterator<_CharT,_Traits> >
                            __num_get_type;
      typedef ctype<_CharT>                 __ctype_type;

    protected:
      // Data Members:
      /**
       *  The number of characters extracted in the prevIoUs unformatted
       *  function; see gcount().
      */
      streamsize        _M_gcount;

    public:
      /**
       *  @brief  Base constructor.
       *
       *  This ctor is almost never called by the user directly,rather from
       *  derived classes' initialization lists,which pass a pointer to
       *  their own stream buffer.
      */
      explicit
      basic_istream(__streambuf_type* __sb)
      : _M_gcount(streamsize(0))
      { this->init(__sb); }

问题是关于软件公司对评论的实际政策。

解决方法

原因?

许多软件开发人员已经精通某种语言,在这种情况下就是 C++。虽然对于想要了解代码含义的初学者来说,注释确实让学习变得更容易,但随着程序员知识水平的提高,它们变得越来越没有必要。例如,Python 代码可能需要一些注释才能向初学者解释:

class House:
    def __init__(self,rooms,people):
        self.rooms = rooms
        self.people = people
myHome = House(3,5)
print(myHome.rooms)

但是高级程序员可能会立即知道它是一个创建对象的脚本。

如果你是...

  • 谈到专有公司,您说得对。闭源程序不打算修改,而是作为二进制文件分发。公司可能会这样做来混淆代码,因此没有人知道它的含义。可能只有一条评论,也可能只出现在文件顶部,说明版权。
  • 开源开发人员则不同。大多数情况下,代码可以自由编辑和分发。在这里,可能会有评论。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。