Title: Sometimes there is a shorter solution
Slug: sometimes-there-is-a-shorter-solution
Date: 2008-08-26 11:30:00
Author: Kartones
Lang: en
Tags: Databases, Development, Databases, SQL, Patterns & Practices
Description: The content discusses the tendency to settle for the first solution found, using a SQL example.

 <p>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.</p> <p>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:</p> <p align="center"><img src="https://images.kartones.net/posts/screenshots/shorter_solution_diagram001.png"> <br><b><font color="#008040">#1 - Wanted</font></b></p> <p align="center"><img src="https://images.kartones.net/posts/screenshots/shorter_solution_diagram002.png"> <br><b><font color="#008040">#2 - Wanted</font></b></p> <p align="center"><img src="https://images.kartones.net/posts/screenshots/shorter_solution_diagram003.png"> <br><b><font color="#008040">#3 - Wanted</font></b></p> <p align="center"><img src="https://images.kartones.net/posts/screenshots/shorter_solution_diagram004.png"> <br><b><font color="#008040">#4 - Wanted</font></b></p> <p align="center"><img src="https://images.kartones.net/posts/screenshots/shorter_solution_diagram005.png"> <br><b><font color="#804040">#5 - Unwanted</font></b></p> <p align="center"><img src="https://images.kartones.net/posts/screenshots/shorter_solution_diagram006.png"> <br><b><font color="#800000">#6 - Unwanted</font></b></p> <p>In order to acomplish the four first situations, this would be the SQL comparisons needed:</p> <p><b>B1&gt;=A1 AND B2&lt;=A2<br>+<br><b>B1&lt;=A1 AND B2&lt;=A2</b><br>+<br><b>B1&gt;=A1 AND B2&gt;=A2</b><br>+<br><b>B1&lt;=A1 AND B2&gt;=A2</b></b></p> <p> </p> <p>But, there's sometimes a shorter solution... Like this one:</p> <p><b>A1&lt;=B2 AND A2&gt;=B1</b></p> <p>Beautiful, don't you agree? Just by switching Bs we reduce to just one AND, covering all four desired causistics... </p> <p>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).</p>
