File: ComplexBooleanConstant.md

package info (click to toggle)
error-prone-java 2.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 23,204 kB
  • sloc: java: 222,992; xml: 1,319; sh: 25; makefile: 7
file content (39 lines) | stat: -rw-r--r-- 1,016 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
When a boolean expression is a compile-time constant (e.g.: `2 < 1`, `1 == 1`,
`'a' < 'A'`), these expressions can be directly replaced with `true` or `false`,
as appropriate. In any context where these expressions are used, `true` or
`false` is a more readable alternative:

```java
if (2 < 1) {
  // Some code I don't want to run right now
}

while (1 == 1) {
  // Some loop that I will manually break out of
}

assert 1 != 2; // I want to force an AssertionFailure if assertions are enabled
```

```java
if (false) {
  // Some code I don't want to run right now
}

while (true) {
  // Some loop that I will manually break out of
}

assert false; // I want to force an AssertionFailure if assertions are enabled
```

When some boolean expression is a compile-time constant unexpectedly, it
generally represents a bug in the code:

```java
for (int i = 0; i < 100; i++) {
  System.out.println("Is " + i + " greater than 50?: " + (1 > 50));
}

// Prints "... false" 100 times, since i > 50 is mistyped as 1 > 50
```