From c3fbfa99178a1899172038c781b566311ed7fa07 Mon Sep 17 00:00:00 2001 From: Naman Anand Date: Tue, 26 Mar 2024 01:52:20 +0530 Subject: [PATCH 1/2] feat: Clean comparison operator `$x == true` => `$x` --- .grit/patterns/java/equality_comparison.md | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 .grit/patterns/java/equality_comparison.md diff --git a/.grit/patterns/java/equality_comparison.md b/.grit/patterns/java/equality_comparison.md new file mode 100644 index 00000000..b918024a --- /dev/null +++ b/.grit/patterns/java/equality_comparison.md @@ -0,0 +1,34 @@ +--- +title: Clean comparison operator `$x == true` => `$x` +tags: [good-practice] +--- + +Update redundant comparison `($x == true)` to achieve clearer code logic and avoid unnecessary repetition. + +```grit +language java + +`if($var == true) { $body }` => `if($var) { $body }` +``` + +## $x = true + +```java +class Bar { + void main() { + boolean myBoolean; + if (myBoolean == true) { + continue; + } + } +} +``` + +```java +class Bar { + void main() { + boolean myBoolean; + if(myBoolean) { continue; } + } +} +``` \ No newline at end of file From 0c047098608f8de9562304335666c34fb6f40d67 Mon Sep 17 00:00:00 2001 From: Naman Anand Date: Wed, 27 Mar 2024 02:43:12 +0530 Subject: [PATCH 2/2] chore: update pattern and example --- .grit/patterns/java/equality_comparison.md | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/.grit/patterns/java/equality_comparison.md b/.grit/patterns/java/equality_comparison.md index b918024a..23fbff42 100644 --- a/.grit/patterns/java/equality_comparison.md +++ b/.grit/patterns/java/equality_comparison.md @@ -3,12 +3,15 @@ title: Clean comparison operator `$x == true` => `$x` tags: [good-practice] --- -Update redundant comparison `($x == true)` to achieve clearer code logic and avoid unnecessary repetition. +Assignment inside a condition like this `$x = false` is usually accidental, this is likely meant to be a `$x`. ```grit language java -`if($var == true) { $body }` => `if($var) { $body }` +`$var == true` => `$var` where { + $var <: within `if ($cond) { $body }`, + $var <: within $cond, + } ``` ## $x = true @@ -28,7 +31,9 @@ class Bar { class Bar { void main() { boolean myBoolean; - if(myBoolean) { continue; } + if (myBoolean) { + continue; + } } } ``` \ No newline at end of file