Monday, July 09, 2007

SQL Controversy: Capitalizing Keywords

Do you write code like this?

set ROWCOUNT 10
select Products.ProductName as TenMostExpensiveProducts, Products.UnitPrice
from Products
order by Products.UnitPrice desc

Or like this?

SET ROWCOUNT 10
SELECT Products.ProductName AS TenMostExpensiveProducts, Products.UnitPrice
FROM Products
ORDER BY Products.UnitPrice DESC


Do we need to capitalize the keywords, functions and statements when we have syntax coloring built into the product?
Look if you use SPUFI with DB2 I understand (see image below)






Here is another example this time without color.


set ROWCOUNT 10
select Products.ProductName as TenMostExpensiveProducts, Products.UnitPrice
from Products
order by Products.UnitPrice desc


SET ROWCOUNT 10
SELECT Products.ProductName AS TenMostExpensiveProducts, Products.UnitPrice
FROM Products
ORDER BY Products.UnitPrice DESC

And yes I agree the bottom query is much easier to read

But with syntax coloring do you still need this? It is a big pain in the neck to use that CapsLock/Shift key every time you type a keyword. There are tools of course like SQL Formatter which will make it much easier.
Remember Hungarian notation, In VB you would write sLastName(string), iCounter(integer)? Well that is gone also, who needs it when you have IntelliSense?
I think the lowercase sql code is easier on the eyes. So what do you think?

8 comments:

Vidas M said...

I allways use capital letters for keywords. I strongly believe that this does make code easier to read, even now we have color coding. I especially see benefit on queries that are bigger and sometimes takes pages. It is also helpful when you print your code using non-color printer.

Matthew said...

Along the line of Vidas, you (or someone else) may not always be using a syntax highlighting IDE/tool to view the SQL. Having keywords in caps helps readability in a monochromatic environment, and maintains backward visibility.

SQL said...

I admit when writing 'production code' I will use the capitalized syntax

I was just wondering if a lot of people still did this

chrissie1 said...

I capitalize.

SQL said...

Chrissie, I thought you were using NHibernate/Hibernate ORM tools which write the SQL for YOU. So yeah you don't really capitalize anything ;-)

chrissie1 said...

I happen to use insert, update and delete stored procedures with that. Or rather INSERT, UPDATE and DELETE statements.

You can let nhibernate make the SQL for you but since 1.2 you can use Stored procedures.

SQL said...

I know, I was just messing with you. Have you looked at LINQ/DLINQ yet?

Brett said...

CAPS are not hard with tools like SQL Prompt.

CAPS are not hard when I leave my right-pinky finger STUCK ON THE SHIFT KEY WHILE TYPING AWAY.

CAPS for keywords is still more distinguishable than the color for me, especially in this age where I usually ignore the blue underlne of a hyperlink while scanning.

I can SCAN tsql code much easier when the IMPORTANT points are CAPITALIZED.

I've been burned many times by reading past a join statement, but somehow a JOIN statement just pops out at me.