Ever seen code like this?
select *
from EMP
where 1=1
and sal > 10
and dept_no = 20
You would probably wonder: Why is there a “WHERE 1=1” predicate? Surely that does nothing?
And you would be absolutely right…IT DOES NOTHING. I should clarify – it does nothing, if you were referring to the optimizer.
But to me, it doesn’t do nothing. It does one thing and that one thing is tell me there’s a developer on the team who isn’t getting a Christmas card 🤣
I mean … c’mon man. Why pollute your SQL with predicates you don’t need? I see people justifying it with:
“Oh, when I am dynamically adding predicates I can just use ‘AND’ for all of them”.
Seriously? You know what that tells me? It tells me you can’t count, because here you are – entrusted to building enterprise grade applications for customers, whilst also probably charged with the security of their data and you’re telling me its too much trouble to know if you are dealing with the first predicate versus the subsequent ones?
Not only does it tell me you can’t count, it tells me that you can’t count TO ONE ! 🤣
And even if there was some tiny sliver of benefit to having it when you are dynamically adding predicates, it behooves me to ask
- Why is our app dynamically adding predicates all over the place? That sounds like a requirements/design concern to me, not an excuse to slam “1=1” everywhere.
- How on earth did that lead to adding it to static SQL in your application? What the heck is that about?
Don’t bother coming at with your “It’s practical”, “It makes coding easier”, “I can comment it out easier”. I’m not buying that for a moment, because in the same breath you’ll be telling me we need to dedicate sprint 17 of our next build cycle to refactoring the code base to remove redundant code from the Java layer, all while you’re slamming an extra line of SQL into every database access you make! 🤣
So next time you see this code
select *
from EMP
where 1=1
and sal > 10
and dept_no = 20
I think you should refactor it to be
select *
from EMP
where 1=1
and 2=2
and sal > 10
and dept_no = 20
and the next time after that
select *
from EMP
where 1=1
and 2=2
and 3=3
and sal > 10
and dept_no = 20
select *
and so on. I mean, how are the fans of “1=1” going to criticise that? “Hey, you added some redundant code?” LOL… You started it! 🤣
Ho Ho Ho! Merry Christmas! Or maybe BAH HUMBG is a better sign off for this post 🤣




Leave a reply to Erik van Roon Cancel reply