aboutsummaryrefslogtreecommitdiff
path: root/docs/dev/style.md
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-10-07 11:57:49 +0100
committerAleksey Kladov <[email protected]>2020-10-07 11:57:49 +0100
commit6976494781f810193535eebc73e5bda73ba9eddf (patch)
treead1ae69a1161c7afd9794faa5b586f90b66fdf99 /docs/dev/style.md
parentfdf2f6226b4ce58a2407d7d3fa3b700f6e76e60a (diff)
Add comparisons guideline to style
Diffstat (limited to 'docs/dev/style.md')
-rw-r--r--docs/dev/style.md27
1 files changed, 27 insertions, 0 deletions
diff --git a/docs/dev/style.md b/docs/dev/style.md
index 17626f3fd..cc06d4122 100644
--- a/docs/dev/style.md
+++ b/docs/dev/style.md
@@ -144,6 +144,20 @@ fn foo() {
144In the "Not as good" version, the precondition that `1` is a valid char boundary is checked in `is_string_literal` and used in `foo`. 144In the "Not as good" version, the precondition that `1` is a valid char boundary is checked in `is_string_literal` and used in `foo`.
145In the "Good" version, the precondition check and usage are checked in the same block, and then encoded in the types. 145In the "Good" version, the precondition check and usage are checked in the same block, and then encoded in the types.
146 146
147When checking a boolean precondition, prefer `if !invariant` to `if negated_invariant`:
148
149```rust
150// Good
151if !(idx < len) {
152 return None;
153}
154
155// Not as good
156if idx >= len {
157 return None;
158}
159```
160
147## Getters & Setters 161## Getters & Setters
148 162
149If a field can have any value without breaking invariants, make the field public. 163If a field can have any value without breaking invariants, make the field public.
@@ -382,6 +396,19 @@ fn foo() -> Option<Bar> {
382} 396}
383``` 397```
384 398
399## Comparisons
400
401Use `<`/`<=`, avoid `>`/`>=`.
402Less-then comparisons are more intuitive, they correspond spatially to [real line](https://en.wikipedia.org/wiki/Real_line)
403
404```rs
405// Good
406assert!(lo <= x && x <= hi);
407
408// Not as good
409assert!(x >= lo && x <= hi>);
410```
411
385## Documentation 412## Documentation
386 413
387For `.md` and `.adoc` files, prefer a sentence-per-line format, don't wrap lines. 414For `.md` and `.adoc` files, prefer a sentence-per-line format, don't wrap lines.