Sometimes there is a shorter solution

Sometimes, while we're developing, we find solutions to problems that work correctly. And sometimes, we think it is the best solution and stop thinking of other possibilities.

I had a recent example few days ago, while I was coding a SQL Server stored procedure that filtered date ranges, returning elements that fit in the first four of this six available outcomes:


#1 - Wanted


#2 - Wanted


#3 - Wanted


#4 - Wanted


#5 - Unwanted


#6 - Unwanted

In order to acomplish the four first situations, this would be the SQL comparisons needed:

B1>=A1 AND B2<=A2
+
B1<=A1 AND B2<=A2
+
B1>=A1 AND B2>=A2
+
B1<=A1 AND B2>=A2

But, there's sometimes a shorter solution... Like this one:

A1<=B2 AND A2>=B1

Beautiful, don't you agree? Just by switching Bs we reduce to just one AND, covering all four desired causistics...

Sometimes we see a narrow passage (play with the lesser/greater operators) when there is more than one solution (playing with the Ax/Bx operands).

Sometimes there is a shorter solution published @ . Author: