latex添加代码注释

Stop me if you’ve heard this one before…

如果您之前听过我的话,请阻止我...

“Good code is self-documenting.”

“好的代码是自我记录。”

In 20+ years of writing code for a living, this is the one phrase I’ve heard the most.

在20多年的代码编写中,这是我最常听到的一句话。

It’s cliché.

这是陈词滥调。

And like many clichés, it has a kernel of truth to it. But this truth has been so abused that most people who utter the phrase have no idea what it really means.

像许多陈词滥调一样,它具有真实性。 但是这个真理已经被滥用了,以至于大多数说这个短语的人都不知道它的真正含义。

Is it true? Yes.

是真的吗 是的

Does it mean you should never comment your code? No.

这是否意味着您永远不要注释您的代码? 不行

In this article we’ll look at the good, the bad, and the ugly when it comes to commenting your code.

在本文中,我们将讨论注释代码时的好,坏和丑陋之处。

For starters, there are really two different types of code comments. I call them documentation comments and clarification comments.

对于初学者来说,实际上有两种不同类型的代码注释。 我称它们为文档注释澄清注释

文档注释 (Documentation Comments)

Documentation comments are intended for anyone who is likely to consume your source code, but not likely to read through it. If you are building a library or framework that other developers will use, you need some form of API documentation.

文档注释适用于可能使用您的源代码但不太可能阅读您的源代码的任何人。 如果要构建其他开发人员将使用的库或框架,则需要某种形式的API文档。

The further removed from the source code your API documentation is, the more likely it is to become outdated or inaccurate over time. A good strategy to mitigate this is to embed the documentation directly into the code and then use a tool to extract it.

您的API文档从源代码中删除的越多,随着时间的推移,它越有可能变得过时或不准确。 减轻这种情况的一种好策略是将文档直接嵌入代码中,然后使用工具将其提取。

Here’s an example of a documentation comment from a popular JavaScript library called Lodash.

这是来自流行JavaScript库Lodash的文档注释示例 。

If you compare these comments to their online documentation, you’ll see it’s an exact match.

如果将这些评论与他们的在线文档进行比较 ,您会发现它是完全匹配的。

If you write documentation comments you should ensure that they follow a consistent standard and that they are easily distinguishable from any inline clarification comments you may also want to add. Some popular and well supported standards and tools include JSDoc for JavaScript, DocFx for dotNet, and JavaDoc for Java.

如果您编写文档注释,则应确保它们遵循一致的标准,并且易于与您可能还想添加的任何在线澄清注释区分开。 一些受欢迎且受到良好支持的标准和工具包括JavaScript的JSDoc ,dotNet的DocFx和Java的JavaDoc 。

The downside of these kinds of comments is that they can make your code very “noisy” and harder to read for anyone who is actively involved in maintaining it. The good news is that most code editors support “code folding” which allows us to collapse the comments so we can focus on the code.

这些注释的缺点是,它们会使您的代码非常“嘈杂”,而对于任何积极参与维护代码的人来说,它们都更难阅读。 好消息是,大多数代码编辑器都支持“代码折叠”,这使我们可以折叠注释,以便我们专注于代码。

澄清评论 (Clarification comments)

Clarification comments are intended for anyone (including your future self) who may need to maintain, refactor, or extend your code.

澄清注释适用于可能需要维护,重构或扩展您的代码的任何人(包括您将来的自己)。

Often, a clarification comment is a code smell. It tells you that your code is too complex. You should strive to remove clarification comments and simplify the code instead because, “good code is self-documenting.”

通常,澄清注释是代码的味道。 它告诉您代码太复杂。 您应该努力删除澄清注释并简化代码,因为“好的代码是自我记录的”。

Here’s an example of a bad — though very entertaining — clarification comment.

这是一个不好的澄清示例 ,尽管很有趣。

/*  * Replaces with spaces  * the braces in cases  * where braces in places  * cause stasis.**/ $str = str_replace(array("\{","\}")," ",$str);

Rather than decorating a slightly confusing statement with a clever rhyme — in amphibrach dimeter, no less — the author would have been far better off spending time on a function that makes the code itself easier to read and understand. Maybe a function named, removeCurlyBraces called from another function named sanitizeInput?

与其用灵巧的韵律来修饰一个稍微令人困惑的语句( 至少用amphibrach dimeter来装饰) ,作者最好花时间在使代码本身更易于阅读和理解的函数上。 也许是从另一个名为sanitizeInput函数调用的,名为removeCurlyBraces函数?

Don’t get me wrong, there are times — especially when you are slogging through a crushing workload — where injecting a bit of humor can be good for the soul. But when you write a funny comment to make up for bad code, it actually makes people less likely to refactor and fix the code later.

别误会我的意思,有时候,尤其是当您在繁重的工作量中苦苦挣扎时,注入一点幽默可能对灵魂有益。 但是,当您编写有趣的注释来弥补错误的代码时,实际上会使人们不太可能在以后重构和修复代码。

Do you really want to be the one responsible for robbing all future coders of the joy of reading that clever little rhyme? Most coders would chuckle and move on, ignoring the code smell.

您是否真的想成为一名负责抢劫所有未来编码人员的人,以使他们阅读这种聪明的小韵律? 大多数编码人员会轻笑并继续前进,而忽略了编码的气味。

There are also times when you come across a comment that is redundant. If the code is already simple and obvious, there’s no need to add a comment.

有时您会遇到多余的评论。 如果代码已经很简单明了,则无需添加注释。

Like, don’t do this nonsense:

就像,不要胡说八道:

/*set the value of the age integer to 32*/int age = 32;

Still, there are times when no matter what you do to the code itself, a clarification comment is still warranted.

尽管如此,有时无论您对代码本身做什么,仍需要澄清说明。

Usually this happens when you need to add some context to a non-intuitive solution.

通常,当您需要向非直观解决方案中添加一些上下文时,就会发生这种情况。

Here’s a good example from Lodash:

这是Lodash的一个很好的例子:

function addSetEntry(set, value) {     /*    Don't return `set.add` because it's not chainable in IE 11.  */    set.add(value);      return set;  }

There are also times when — after a lot of thought and experimentation — it turns out that the seemingly naive solution to a problem is actually the best. In those scenarios it is almost inevitable that some other coder will come around thinking they are much smarter than you are and start messing with the code, only to discover that your way was the best way all along.

有时,经过大量的思考和实验,事实证明,似乎天真的解决问题的方法实际上是最好的。 在这些情况下,几乎不可避免地会有其他一些编码器出现,以为它们比您聪明得多,并开始弄乱代码,只是发现您的方式一直是最好的方式。

Sometimes that other coder is your future self.

有时,其他编码员是您未来的自我。

In those cases, it’s best to save everyone the time and embarrassment and leave a comment.

在这种情况下,最好节省每个人的时间和尴尬并发表评论。

The following mock-comment captures this scenario perfectly:

以下模拟注释完美地捕获了这种情况:

/**Dear maintainer: Once you are done trying to 'optimize' this routine,and have realized what a terrible mistake that was,please increment the following counter as a warningto the next guy: total_hours_wasted_here = 42**/

Again, the above is more about being funny than being helpful. But you SHOULD leave a comment warning others not to pursue some seemingly obvious “better solution,” if you’ve already tried and rejected it. And when you do, the comment should specify what solution you tried and why you decided against it.

同样,以上内容是关于有趣而不是有所帮助。 但是您应该发表评论,警告其他人,如果您已经尝试并拒绝了它,则不要追求一些看似明显的“更好的解决方案”。 并且,当您这样做时,注释应指定您尝试了哪种解决方案以及为什么反对该解决方案。

Here’s a simple example in JavaScript:

这是JavaScript中的一个简单示例:

/* don't use the global isFinite() because it returns true for null values*/Number.isFinite(value)

丑陋的 (The Ugly)

So, we’ve looked at the good and the bad, but what about the ugly?

因此,我们研究了好与坏,但是丑陋呢?

Unfortunately, there are times in any job where you’ll find yourself frustrated and when you write code for a living, it can be tempting to vent that frustration in code comments.

不幸的是,在任何工作中,有时您会发现自己感到沮丧,而当您以谋生为目的编写代码时,可能很想在代码注释中消除这种沮丧。

Work with enough code bases and you’ll come across comments that range from cynical and depressing to dark and mean spirited.

使用足够的代码库,您会遇到各种评论,从愤世嫉俗和沮丧到沉闷和刻薄。

Things like the seemingly harmless…

貌似无害的事情……

/*This code sucks, you know it and I know it.  Move on and call me an idiot later.*/

…to the downright mean

…… 直率的意思

/* Class used to workaround Richard being a f***ing idiot*/

These things may seem funny or may help release a bit of frustration in the moment, but when they make it into production code they end up making the coder who wrote them and their employer look unprofessional and bitter.

这些事情看似很有趣,或者可能会在瞬间缓解一些挫败感,但是当他们将其纳入生产代码时,最终使编写这些代码的程序员和他们的雇主显得不专业且痛苦。

Don't do this.

不要这样

If you enjoyed this article, please smash the applause icon a bunch of times to help spread the word. And if you want to read more stuff like this, please sign up for my weekly Dev Mastery newsletter below.

如果您喜欢这篇文章,请多次敲击掌声图标,以帮助宣传。 如果您想类似的内容,请在下面注册我的每周开发精通通讯。

翻译自: https://www.freecodecamp.org/news/code-comments-the-good-the-bad-and-the-ugly-be9cc65fbf83/

latex添加代码注释

latex添加代码注释_在代码中添加注释:好的,坏的和丑陋的。相关推荐

  1. html中如何添加php代码注释_html在源代码中插入注释标签!--...--

    实例 HTML 注释: 这是一段普通的段落. 浏览器支持 IE Firefox Chrome Safari Opera 所有浏览器都支持注释标签. 定义和用法 注释标签用于在源代码中插入注释.注释不会 ...

  2. java代码识别_识别Java中的代码气味

    java代码识别 作为软件开发人员,我们不仅要编写有效的代码,而且还要编写可维护的代码,这是我们的责任. Martin Fowler在他的<重构:改进现有代码的设计>中将代码气味定义为: ...

  3. python以什么表示代码层次_在Python中,采用代码缩进和( )区分代码之间的层次。_学小易找答案...

    [填空题]29 号元素 Cu 基态时的电子排布式为 ___ __ ,该元素在周期表中位于 __ _ 周期. __ __ 族. [单选题]"我要努力实现梦想,以弥补小时候吹过的牛"小 ...

  4. android添加图片控件代码,如何在android studio中添加图标图像按钮

    我想设计这种类型的应用程序的布局(如图所示).在此布局中,当我们单击圆形图标时,它将移至下一页.我想知道它是如何完成的. 解决方法: 在抽屉文件夹中创建circle_background.xml并将此 ...

  5. eclipse让实现类也添加上接口的注释_您的Spring框架注释指南,请注意查收

    这是关于几乎所有Spring Framework注释的简化,包括Core,Spring Cloud,Spring MVC,Spring REST和Spring Boot. 在本文中,我们将介绍Spri ...

  6. react中嵌入网页_在网站中添加 React

    根据需要选择性地使用 React. React 从一开始就被设计为逐步采用,并且你可以根据需要选择性地使用 React.可能你只想在现有页面中"局部地添加交互性".使用 React ...

  7. dreamweaver后缀名_在 Dreamweaver 中添加或编辑识别的文件扩展名

    如果您要对 Dreamweaver 中的以下问题进行故障诊断,请阅读本文: 打开文件时,出现此错误消息:"找不到此文件扩展名的有效编辑器." 代码视图中的颜色编码与所需文件类型的颜 ...

  8. java创建的窗口无法关闭_在Java中添加canvas后无法关闭窗口(Can't close window after adding canvas in Java)...

    在Java中添加canvas后无法关闭窗口(Can't close window after adding canvas in Java) public class Screen extends Ca ...

  9. springmvc开启事务_在Controller中添加事务管理

    写这篇文章之前先说明一下: 1. Controller中添加事务管理,是可行的,但是强烈不推荐,因为不符合实际开发场景,还会导致一系列问题 2. 事务请在Service处理,所有的业务逻辑请写在 Se ...

  10. python如何执行代码漏洞_任意代码执行漏洞

    背景介绍 当应用在调用一些能将字符串转化成代码的函数(如php中的eval)时,没有考虑到用户是否能控制这个字符串,将造成代码注入漏洞.狭义的代码注入通常指将可执行代码注入到当前页面中,如php的ev ...

最新文章

  1. 线性代数 第二章 矩阵及其运算
  2. tensorflow kears GPU CUDA Cudnn 各种版本问题
  3. python加上子类的特性_Python里的元编程:控制产生的实例对象的特性以及实例
  4. eclipse 主题设置
  5. 打印机显示服务器连接错误怎么回事,打印机处于错误状态是怎么回事 打印机处于错误状态如何解决【图文详解】...
  6. SSIS(简单数据抽取过程介绍)
  7. 如何自定义设置macOS的开机启动项
  8. 怎么查看html页面,网页浏览记录如何查看_怎样查历史网页浏览记录
  9. 读书印记 - 《南北战争三百年》
  10. 如何在家免费使用知网?
  11. java 左移和右移理解
  12. matlab 回声状态网络ESN的时间序列预测
  13. 2022年11月(下半年)信息系统项目管理师考试-案例分析真题及解析
  14. DOM4J+JAXEN
  15. AJAX如何将参数带到并传给另一个页面?
  16. python语言实现冒泡算法(附代码)
  17. 抽中H1B回国有风险? 还没消息代表没中签? H1B审理期间常见问题解答
  18. 一款超好的省市区三级联动插件citypicher的使用(数据分从数据库获取或从js文件获取)
  19. 什么是AudioEffect
  20. mybatis面试(MyBatis怎么读)

热门文章

  1. git merge fast-forward squash no-ff
  2. win10计算机管理员的权限才能删除,Win10文件夹删除不了需要管理员权限怎么办?Win10文件夹删除不了需要管理员权限的解决方法...
  3. git lfs linux,Git LFS 操作指南
  4. 为什么我的淘宝店铺动态评分清零了?
  5. 什么叫诚实_诚实是人类最珍爱的美德之一,但是什么是诚实?
  6. Unity (三) NavMeshAgent之:分层路面导航(王者荣耀,英雄联盟中小兵分三路进攻敌方)...
  7. spring cloud各个微服务之间如何相互调用(Feign、Feign带token访问服务接口)
  8. 手机邮箱怎么注册申请?教你用手机号如何注册电子邮箱地址
  9. 元组定义 元组运算符
  10. 计算机中安装杀毒软件 的作用,360杀毒软件的功能及其使用方法介绍