Skip to content

Commit 6976494

Browse files
committed
Add comparisons guideline to style
1 parent fdf2f62 commit 6976494

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

docs/dev/style.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,20 @@ fn foo() {
144144
In the "Not as good" version, the precondition that `1` is a valid char boundary is checked in `is_string_literal` and used in `foo`.
145145
In the "Good" version, the precondition check and usage are checked in the same block, and then encoded in the types.
146146

147+
When checking a boolean precondition, prefer `if !invariant` to `if negated_invariant`:
148+
149+
```rust
150+
// Good
151+
if !(idx < len) {
152+
return None;
153+
}
154+
155+
// Not as good
156+
if idx >= len {
157+
return None;
158+
}
159+
```
160+
147161
## Getters & Setters
148162

149163
If a field can have any value without breaking invariants, make the field public.
@@ -382,6 +396,19 @@ fn foo() -> Option<Bar> {
382396
}
383397
```
384398

399+
## Comparisons
400+
401+
Use `<`/`<=`, avoid `>`/`>=`.
402+
Less-then comparisons are more intuitive, they correspond spatially to [real line](https://en.wikipedia.org/wiki/Real_line)
403+
404+
```rs
405+
// Good
406+
assert!(lo <= x && x <= hi);
407+
408+
// Not as good
409+
assert!(x >= lo && x <= hi>);
410+
```
411+
385412
## Documentation
386413

387414
For `.md` and `.adoc` files, prefer a sentence-per-line format, don't wrap lines.

0 commit comments

Comments
 (0)