So True, So True

If Comedy is born of Pain, and Pain is born of Truth, then this post by the enigmatic Phil Factor should have you laughing through your tears. Figuratively speaking, of course. There’s no crying in software development.

If you’re one of those people who doesn’t like to click through without a taste of what is being clicked through to, Phil (not his real name, I hope) has broken down the phases of a software development project into the following:

  1. The Euphoric Phase
  2. The Chill Wind
  3. The Slough of Despond
  4. The Search for the Guilty
  5. The Depressive Position
  6. The Death March
  7. Recriminations
Posted in IT Follies | Tagged , | Leave a comment

Code Formatting In 4/4 Time

I like to listen to blues on internet radio while I work.

I also format my code as I go. I use a lot of whitespace- I generally find that I save time overall by always using clearly formatted code. It is much easier to read, and to see the structure of the query.

I realized this morning that if I like the song that is playing, I hammer the TAB and arrow keys in time, so it’s arrow-arrow-tab-tab, tab-tab-arrow-arrow, tab-tab-tab-tab (etc).

I also have to check this blog more often. Graham left a comment a month ago that I just approved. Sorry Graham!

Posted in Site Notes, T-SQL | Leave a comment

ISNULL vs COALESCE

One of the ways you can tell code that I have written from code that most other developers have written is that I use the ANSI92 compliant function COALESCE() instead of the more common proprietary ISNULL().

I do this for several reasons. First, COALESCE is not proprietary, and so it is highly unlikely that it will ever be dropped. Second, COALESCE is more flexible. In case you haven’t encountered these two functions, they have similar functionality.

ISNULL(arg1, arg2) returns the second argument if the first is NULL. If your database is rigidly designed and normalized to 3NF, you probably have little use for ISNULL and COALESCE. If, on the other hand, your database is like 99% of databases and it contains a mess of columns that allow NULL, you probably make extensive use of ISNULL.

COALESCE() takes a list of arguments of arbitrary length and returns the first non-NULL value it finds. This means that instead of long chains of ISNULL(ISNULL(ISNULL(col_a, col_b),col_c),col_d), you can write COALESCE (col_a, col_b, col_c, col_d). This is much easier to parse and leads to more logical, maintainable code.

This morning I encountered another reason to use COALESCE.

In my experience, genuine bugs (as opposed to behavior somebody doesn’t like) are rare in SQL Server. I don’t know whether this is a bug, or merely an allowed deficiency but:

SELECT ISNULL(‘abcd’ + null,‘1234567890′)

I would expect this to return ‘1234567890′. However, it does not. On SQL Server 2005, this returns ‘12345′.

On the other hand,

SELECT COALESCE(‘abcd’ + null,‘1234567890′)

returns, as expected, ‘1234567890′.

This is a subtle deficiency, one that could be difficult to track down, and is yet one more reason to use COALESCE.

Posted in ANSI Standard, T-SQL | 3 Comments

Uh Oh

You know you’re in trouble when the stored procedure you wrote has been modifed by another developer, and you find that what had been an elegant INSERT has been tarted up with something like this at the end:

WHERE NOT EXISTS(
    SELECT  *
    FROM    History
    WHERE   UserID  = STACKS.UserID
    AND     LevelID     =   STACKS.LevelID
    AND     StackNumber = STACKS.StackNumber
)
) A  INNER JOIN RewardLevel RL
    ON A.LevelID = RL.LevelID
WHERE
(
    EXISTS(SELECT 1 FROM CurrentLevel GS
            INNER JOIN RewardLevel GSR
                ON GS.LevelID = GSR.LevelID
            WHERE GS.UserID = A.UserID
            AND A.LevelNumber <= GSR.LevelNumber AND A.StackNumber < GS.CurrentStackNumber)
    OR
    EXISTS(SELECT 1 FROM CurrentLevel GS
            INNER JOIN RewardLevel GSR
                ON GS.LevelID = GSR.LevelID
            WHERE GS.UserID = A.UserID
            AND A.LevelNumber < GSR.LevelNumber)
)

It’s actually much worse. The original insert was a fairly complex one of about 90 lines (that’s deceptive, because I format code long rather than wide – other developers might format this to 50 lines) but due to other developers taking the short way round and doing the quickest patch possible, it has ballooned to a nested set of statements over 500 lines in length, of which this particular add-on is only a part.

Somehow, the structure of the original SELECT has been altered such that date calculations that depend on the ordinal position of the rows no longer work.

This is how code begins to approach unmaintainability.

Posted in SQL | Leave a comment

SQL Blog

This is intended to be a place to put SQL and SQL Server related material. A blog like this should be simple and needs a different set of categories and tools than a regular blog.

I don’t know how much posting will be done here. Guess we’ll see.

Posted in Site Notes | Leave a comment