30.06.2013 Views

SQL Server Team-based Development - Red Gate Software

SQL Server Team-based Development - Red Gate Software

SQL Server Team-based Development - Red Gate Software

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

1<br />

GO<br />

SELECT UserId ,<br />

UserName ,<br />

UserPassword ,<br />

IsActive ,<br />

IsDeleted ,<br />

IsSleeping<br />

FROM Users<br />

WHERE IsActive = 1<br />

AND IsSleeping = 1<br />

Listing 11-19: Refactoring the DDL to avoid the "Tell, don't ask" anti-pattern.<br />

347<br />

Chapter 11: <strong>SQL</strong> Refactoring<br />

By moving each status code to its own column, we don't have to keep the data in code;<br />

it's stored in the schema like it should be. It also means that we can mix and match the<br />

different statuses, as required.<br />

There are times when you might still use the "data in code" method, but it should be<br />

used only with caution. For example, we might consider using it if the UserStatus<br />

values really were static and used through multiple applications. I still don't like it,<br />

though. The hard part is changing your thinking processes so that you know when it's<br />

still needed and when it isn't.<br />

Summary<br />

In this chapter, we've looked at why refactoring is important, in the context of the<br />

broader picture of database development. Refactoring allows us to improve the<br />

performance, maintainability, and security of database code, without affecting any<br />

of the systems that access the database code.<br />

In order to do this, we need a set of unit tests that cover our database functionality,<br />

a database abstraction layer that isolates the underlying structure from the outside

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!