Keep your code ignorant and lazy!
November 26th, 2008
This post is a follow up to Busting the great TDD Myth. If you haven’t already read it, please check it out.
Lets review the code from the last post:
function TLogin.GetConnectionString : string; var reg : TRegIniFile; begin reg := TRegIniFile.Create('Software\MyApp'); Result := reg.ReadString('Database', 'ConnectionString', ''); FreeAndNil(reg); end; function TLogin.Execute(const UserName, Password : string) : Boolean; var UserQuery : TADOQuery; DBUserPassword : string; begin UserQuery := TADOQuery.Create(nil); try UserQuery.ConnectionString := GetConnectionString; UserQuery.SQL.Text := 'SELECT Password FROM Users WHERE UserName = ' + QuotedStr(UserName); UserQuery.Open; DBUserPassword := UserQuery.FieldByName('Password').AsString; UserQuery.Free; Result := DBUserPassword = Password; except Result := False; end; end;
Now lets review the problems:
- Hidden dependencies - TLogin does not publish the fact that it relies on a connection to the database and needs a connection string from the registry.
- Mixing concerns - Business logic and object creation occur in the same block of code. This means you can’t test business logic without creating the dependencies.
All of these problems can be summed up as: Too much ceremony and too little essence. What is this class trying to do? Does creating a connection to the database, querying a table and handling exceptions have much to do with the purpose of logging in to the system?
Recent Comments