SQL Server NULL values and "Order By" order

I have a few tables that contain a column called "Order", which is used to sort by when retrieving the data. The purpose is to keep the data in a certain order when displayed to the end user.

Black LinenNULL
Navy Blue LinenNULL
Dark Green LinenNULL
Burgundy LinenNULL
Ivory VellumNULL
Grey FeltNULL
Natural LinenNULL
White Coated Two Sides1
White Cast Coated One Side2
White SemiGloss Coated One Side3
White Smooth4
White Linen5

The problem is that SQL Server puts null values above non-null values when doing an "order by". To reverse this behavior, this was the most elegant and efficient solution that I found:

Select FooValue
From foos
Order by (Case When [Order] Is Null Then 1 Else 0 End), [Order]

I found information about the original problem here, and the solution was from Tim in the comments. Thanks!

Like this post? Please share it!

See a mistake? Edit this post!

Object does not match target type in GridView

I created a shopping cart for a website that can display multiple types of items that implement IShoppingCartItem. When the GridView would display items that were different type, I would get this exception:

Exception Details: System.Reflection.TargetException: Object does not match target type.

I found a lot of solutions that I didn't really like. For example, every shopping cart item type could implement ITypedList.

What I ended up doing was creating a CartGridItem class that implements IShoppingCartItem:

public class CartGridItem : IShoppingCartItem
    private readonly IShoppingCartItem _baseCartItem;
    public CartGridItem(IShoppingCartItem baseCartItem)
        _baseCartItem = baseCartItem;
    #region IShoppingCartItem Members
    public string ProductCode
        get { return _baseCartItem.ProductCode; }
//...end of code sample...

This has worked great, and I don't have to make any changes when I create a new item that implements that interface!


Like this post? Please share it!

See a mistake? Edit this post!

Writing code that you're proud of

Almost every time that someone shows me a snippet of their code, I hear a comment like this:

  • "This isn't finished, I have a lot of cleanup to do"
  • "I wrote this code a long time ago, it's not very good"
  • "Ignore all these bugs, I haven't had time to fix them yet"

We've all done it. It's very rare that I hear a developer that is truly proud of their code.


So why aren't you proud of your code? Part of the reason is that we often combine planning with writing code. I know that purists don't like that, but the reality is that there are so many details in software that perfect planning is impossible.

The other reason is that we get our code working, and keep telling ourselves that we'll go back and clean it up later. Then you tell yourself "why fix it if it isn't broken?" You move on to the next task, and the cycle continues.

Common things that are overlooked:

  • Proper exception handling and failure modes
  • Properly disposing of unmanaged resources (IDisposable)
  • Inefficient coding style
  • Missing documentation
  • Well thought out architecture
  • Comprehensive unit tests & code coverage
  • Following established coding standards
  • Simplifying to minimize LoC to maintain

This post was inspired by a number of personal projects that I'm releasing as open source. I found myself doing a lot of cleanup work before uploading the source. I'm regretting not spending more time on it when I had written it.

So here is my challenge for you. Write code that you're proud of! Do a personal code review before checking in your code. Then do a peer code review. If you're embarrassed by the code you've written, go back and fix it. It's commonly known that fixing the problems early on will have massive returns in the long run.

Like this post? Please share it!

See a mistake? Edit this post!

Jason Young I'm Jason Young, software engineer. This blog contains my opinions, of which my employer - Microsoft - may not share.

@ytechieGitHubLinkedInStack OverflowPersonal VLOG