question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Redundant boolean CASE WHEN

See original GitHub issue

I’m sure you have all seen these as I see them often!

select
    case
        when n > 0 then true else false end as is_positive
from table_a

I would like to add a rule to prefer

select
    n > 0 as is_positive
from table_a

A case when statement is redundant if it uses only one when which is turned into true or false

However, to add a “fix” the rule should also take in account the following:

select
    case
        when n >= 0 then false else true end as is_negative
from table_a

which should be turned into

select
    case
        not(n >= 0) as is_negative
from table_a

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
dmateuspcommented, Jun 13, 2022

hey so your code shouldn’t be breaking that rule, this rule should only apply to CASE WHENs that have a true/false return statement

I would say the statement you quoted is actually false ^^, I didn’t write

A case when statement is redundant if it uses only one when

I wrote

A case when statement is redundant if it uses only one when >> which is turned into true or false <<

That last piece might be unclear but see the examples in the description of the PR, that’s the cases that this rule aimed to fix.

It seems like a bug with the rule, which should be opened as a separate issue

thanks 😃

1reaction
dmateuspcommented, Feb 23, 2021

@rbetzler it’s not spam! Just go ahead an open the PR ^^, then we can have a discussion there.

You documented + tested the rule so I’d say it’s a 🥇 first go

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is setting a boolean to false redundant? - java - Stack Overflow
It is redundant in most cases. The exception is if it's a local variable, in which case it needs to be initialized before...
Read more >
Strange case of "Redundant boolean literal in conditional ...
Hello everyone! Today I was working at a comparison operator when I noticed Resharper highlighting the entire function block content.
Read more >
Switch statement and using boolean values as cases - SitePoint
Also the default section of this code is redundant; a boolean can only ever be true or false by definition, and both are...
Read more >
Boolean literals should not be redundant C# csharpsquid
My suggestion to remove an example#1 style as not a Noncompliant Code. For csharpsquid : S1125 With title Boolean literals should not be ......
Read more >
Consensus Theorem in Digital Logic - GeeksforGeeks
The consensus or resolvent of the terms AB and A'C is BC. It is the conjunction of all the unique literals of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found