Wednesday, May 15, 2013

Developers Need To Fail

Is failure the opposite of success?  If you fail does it mean you didn't succeed?  Does failing bring you closer to success?

Fear of failure is actually worse than failure.  Everyone has a fear of failing.  This often keeps us from doing something with all of our effort.  If we put in all the effort and we fail, does that mean that we are failures as human beings?  Our culture and our media would have us believe that was true.  Our ego would prevent us from coming to that conclusion, so it's just better to not try our best.  We're better off just doing a good job and holding something back.  That way when we fail we can always say "Well, I didn't really try all that hard, did I?"

Let's look at this with an example.  Let's say that your long term goal is to be in a relationship.  You want a girl who is beautiful, intelligent, witty, friendly, and a delight to be around.

You find a girl who meets these qualifications and are excited about her.  Do you ask her out?  What if she says no?  If she says no, does that mean you are less of a human being?  Does that mean you aren't worthy of a girl like that?  What would that do to your self-esteem?  Is failure at getting this girl you've chosen freezing you to inaction?

Here's the most important question.  If you never ask her out, will you ever go out with her?  If you ask her out and she says no, and you silence your ego, doesn't that make it easier to ask out the next girl?  If you ask out three girls and three say no, but the fourth one says yes, did you fail three times and succeed once?  Or are you just successful with women. 

When you've been married for twenty years, will it really matter that three girls rejected you before you found the one you ended up with?  Will anyone list your failures?  Will anyone really care?

But I can promise this, you'll never go out with her if you don't ask her.  It will just never happen, and you'll have failed before you even started.  It is much, much better to try, fail, and improve, then it is to never try at all.

This is a weird post for a software development blog.  My point is that developers need to feel like they are in a safe environment to fail.  If they think that failing is dangerous, then they will never do anything dangerous.  Dangerous things are scary, but when they succeed, they have the ability to change an entire organization, and leave the business stake holders gasping for breath at the magnificence of it all.  

Allow developers to fail and fail often.  Just make sure it is leading them to success and they are learning from it.  Eventually, they will have a bold success.  And that will make all the failures worth it, and you'll make back any lost money and then some. 

Developers who are afraid to fail just write boring code or they just watch YouTube videos all day.  They never do anything awesome.  And awesome is where the fun is.  Awesome is what we really want.

Saturday, May 4, 2013

The problem with NULLs and T-SQL

I was reading Itzik Ben Gan's T-SQL Fundamentals book as part of the reading assignment for the SQL Pass Book Readers Group.

He made a great point about dealing with nulls and the IN clause and he had a great example.  Rather than use his database, I wrote my own script to demonstrate the problem, and thought I'd post the issue here, succinctly.

First run the setup script.  This creates two tables and populates it with data.


create table customers
(custID int
, custName varchar(100));
go
create table orders
(orderID int
, custID int
, OrderDate datetime);
go

insert into customers
(custID, CustName)
values
(1, 'Ike Ellis')
, (2, 'John Ackerman')
, (3, 'Scott Reed')
, (4, 'Brad Cunningham')
, (5, 'Llewellyn Falco');
go
insert into orders
(orderID, custID, OrderDate)
values
(1, 1, '20130101')
, (2, 1, '20130102')
, (3, 1, '20130103')
, (4, 3, '20130104')
, (5, 3, '20130105')
, (6, 4, '20130106')
, (7, 4, '20130107')
go

Next, we want to find out how many customers have not placed orders:

select * from customers
where custid not in (select custid from orders)

We get two results, John Ackerman and Llewellyn Falco.

Now let's add a NULL into the Orders table:

insert into orders
(orderid, custid, orderdate)
values
(8, null, '20130108')

And now, let's run the same query:

select * from customers
where custid not in (select custid from orders)

And we get no results.  This is because the column allows NULLs and NULL comparisons always return false.  Essentially the NULL customerID could be John Ackerman or Llewellyn Faloc.  We don't really know if it's them or not, so instead of returning them, we just won't return anything.  This is by design.  So how do we get the results we want.  We can do three things:

1)  We can make sure that columns that don't need null values are created with NOT NULL, thus disallowing them to be inserted. 

2)  We can strip out the NULLs in the subquery:
select * from customers
where custid not in (select custid from orders where custid is not null)

3)  We can use exists, which only uses two-value predicate logic, not three-value logic, thus giving us the desired result:
select * from customers c
where not exists (select * from orders o where o.custid = c.custid)

Hope that little script helps!