如何写 dart 注释

简介: 如何写 dart 注释

无论对于哪门语言,写注释都是很难的,但是难也得写。可能你在设计程序的时候灵光一闪,想到了一个绝佳的思路,但是过了半年一年之后,可能这个思路就想不起来了,代码也看不懂了,更不要说别人能看懂你的代码了。

  1. 不需要出现在文档中的注释都应该使用 //
  2. 需要生成文档的注释 使用 /// 文档注释来注释成员和类型。
  3. 要在文档注释开头有一个单句总结 让文档注释的第一句从段落中分开。
/// Deletes the file at [path].
///
/// Throws an [IOError] if the file could not be found. Throws a
/// [PermissionError] if the file is present but could not be deleted.
void delete(String path) {
  ...
}
复制代码
  1. 如果一个属性同时包含 getter 和 setter,请只为其中一个添加文档。 dart doc 命令会将 getter 和 setter 作为同一个属性进行处理,而如果它们都包含文档注释,dart docs 命令会将 setter 的文档忽略。
/// The pH level of the water in the pool.
///
/// Ranges from 0-14, representing acidic to basic, with 7 being neutral.
int get phLevel => ...
set phLevel(int level) => ...
复制代码
  1. 推荐使用名词短语来开始库和类型注释。
/// A chunk of non-breaking output text terminated by a hard or soft newline.
///
/// ...
class Chunk { ... }
复制代码
  1. 考虑 在文档注释中添加示例代码。
/// Returns the lesser of two numbers.
///
/// ```dart
/// min(5, 3) == 3
/// ```
num min(num a, num b) => ...
复制代码
  1. 给变量,方法,或类型等名称加上方括号,则 dartdoc 会查找名称并链接到相关的 API 文档。括号是可选的,但是当你在引用一个方法或者构造函数时,可以让注释更清晰。可以链接到类的命名构造函数,或 变量,方法,类型等。
/// Throws a [StateError] if ...
/// Similar to [Duration.inDays], but handles fractional days.
/// To create a point, call [Point.new] or use [Point.polar] to ..
复制代码
  1. 把注释文档放到注解之前。
/// A button that can be flipped on and off.
@Component(selector: 'toggle')
class ToggleComponent {}
复制代码

不要把 @Component 放到第一行,要放到注释之后。

  1. 可以用 markdown 来格式代码,但不要滥用。
///
/// This sentence has *two* _emphasized_ words (italics) and **two**
/// __strong__ ones (bold).
///
/// A blank line creates a separate paragraph. It has some `inline code`
/// delimited using backticks.
///
/// * Unordered lists.
/// * Look like ASCII bullet lists.
/// * You can also use `-` or `+`.
///
/// 1. Numbered lists.
/// 2. Are, well, numbered.
/// 1. But the values don't matter.
///
///     * You can nest lists too.
///     * They must be indented at least 4 spaces.
///     * (Well, 5 including the space after `///`.)
///
/// Code blocks are fenced in triple backticks:
///
/// ```dart
/// this.code
///     .will
///     .retain(its, formatting);
/// ```
///
/// The code language (for syntax highlighting) defaults to Dart. You can
/// specify it by putting the name of the language after the opening backticks:
///
/// ```html
/// <h1>HTML is magical!</h1>
/// ```
///
/// Links can be:
///
/// * https://www.just-a-bare-url.com
/// * [with the URL inline](https://google.com)
/// * [or separated out][ref link]
///
/// [ref link]: https://google.com
///
/// # A Header
///
/// ## A subheader
///
/// ### A subsubheader
///
/// #### If you need this many levels of headers, you're doing it wrong
复制代码
  1. 可以用反引号来标代码
/// You can use [CodeBlockExample] like this:
///
/// ```dart
/// var example = CodeBlockExample();
/// print(example.isItGreat); // "Yes."
/// ```
复制代码

总之写注释的时候要简洁,清晰,切中要害。

目录
相关文章
|
4月前
|
Dart 安全 前端开发
【教程】混淆Dart 代码
代码混淆是一种将应用程序二进制文件转换为功能上等价,但人类难于阅读和理解的行为。在编译 Dart 代码时,混淆会隐藏函数和类的名称,并用其他符号替代每个符号,从而使攻击者难以进行逆向工程。
|
8月前
|
自然语言处理 安全 测试技术
如何写出优秀的代码
如何写出优秀的代码
|
13天前
|
存储 缓存 运维
如何写好代码?一个提升代码可读性的小技巧
如何提高代码的可读性,使得代码变得整洁,甚至赏心悦目。本文会从“控制流”的角度分享一下作者对提高代码可读性的一些思考。
|
4月前
|
Python
在编写代码时,注释对于代码的可读性和维护性非常重要。
在编写代码时,注释对于代码的可读性和维护性非常重要。
31 0
|
5月前
|
设计模式 算法 程序员
如何写出好的代码注释?
作为程序员,想必大家在日常开发中必写注释,而且在软件开发过程中,给代码写注释是一项至关重要的工作,也是一名合格的程序员该具备的编程素养。恰当的注释可以提高代码的可读性和可维护性,方便其他人理解熟悉和修改代码,但是不恰当或过度的注释可能会导致混乱和误导,会起到适得其反的作用。那么本文就来分享一些关于如何正确地给代码写注释的方法和指导原则,并提供一些减少注释但仍能让他人理解代码的方法。
67 3
如何写出好的代码注释?
|
8月前
|
Rust 前端开发 编译器
Rust的注释与文档
Rust的注释与文档
37 0
|
算法
如何写出高质量的代码注释
在软件开发中,代码注释是一个至关重要的部分。它们可以使代码更容易理解、更易于维护和调试,并帮助团队合作。但是,注释也可以容易被滥用或者过度使用,导致代码变得混乱或者难以理解。因此,写出高质量的代码注释是一项关键的技能,本文将分享一些有用的建议。
82 0
|
前端开发 Java 开发工具
对代码注释的一些思考
本文浅谈对Java程序代码注释的一些思考
86 0
|
缓存 供应链 架构师
代码注释的艺术,优秀代码真的不需要注释吗?
一段糟糕的代码,往往大家最低的预期是把注释写清楚,最合理的做法通常应该对代码做优化。如果我们将代码真正做到了优秀,我们是否还需要注释?
代码注释的艺术,优秀代码真的不需要注释吗?
|
设计模式 前端开发 安全
如何写出高质量的代码
如何写出高质量的代码
166 0
如何写出高质量的代码