Like other people are saying - the toolchain should have caught this. And it should have, I don't remember how it'd been disabled...
If you must write code, errors follow, and “defence in depth” is applicable. Use an editor that serves you well, use compiler flags, use your linter, and consider Yoda Notation, which catches classes of errors, but yes, not every error.
if (env('LOG_LEVEL') = 3) {}
Would throw and everything would be ok. Otherwise, use constants.
Also, lint against assignment in if/while conditions. If you want to assign in those conditions, disable linting for the line and make it explicit.
1) Outside of initialization, assignment is done with the `<-` operator, so you're only potentially confused in the opposite direction (assignments incorrectly being boolean comparisons).
2) Return types and inputs are strictly validated so an accidental assignment (returning a `void`/`unit` type), would not compile as the function expects a bool.
3) Immutable by default, so even if #1 and #2 were somehow compromised the compiler would still halt and complain that you were trying to write to something unwriteable
Any of the above would terminally prevent compilation, much less hitting a code review or getting into production... Correctness from the ground up prevents whole categories of bugs at the cost of enforcing coding discipline :)
if (myVar) {
//
}
Instead of `myVar == true/false`.The accidental assignment is much less common due to the way equality is tested in Java.
Also, `null` comparisons being assigned will fail to compile (assuming var is a String here):
TestApp.java:6: error: incompatible types: String cannot be converted to boolean
if (var = null) {
As an experienced programmer I have written if-statements so many times in life that I never ever, even by mistake, type:
I always type: by muscle memory. It has become a second nature. Unless of course where I really mean it, like: