George V. Reilly

Review: Programming Sudoku

Programming Sudoku
Title: Pro­gram­ming Sudoku
Author: Wei-Ming Lee
Rating: ★ ★ ½
Publisher: Apress
Copyright: 2006
Pages: 214
Keywords: pro­gram­ming, in­tro­duc­to­ry
Reading period: 22 February, 2009

I was Toast­mas­ter of the Day at this evening’s meeting of Freely Speaking Toast­mas­ters. My theme was software de­vel­op­ment and I wanted to give the non-developer audience a taste for what it’s like to write a program. I talked about writing a simple Sudoku game.

Yesterday, I read Pro­gram­ming Sudoku for background. I bought this book for Emma after reading about it on Scott Hansel­man’s blog. It’s targeted at beginning pro­gram­mers and walks them through building a Sudoku game and solver. I was hoping to get Emma more interested in pro­gram­ming—un­suc­cess­ful­ly. She found it rep­e­ti­tious and a little confusing, and she found some typos in the code.

Ped­a­gog­i­cal­ly, the book is good. It starts by creating a simple WinForms ap­pli­ca­tion in Visual Basic to play a Sudoku game. Then it builds a solver for simple games and refines the solver to handle harder games. Next, it adds a puzzle generator. It concludes with a brief chapter on a similar game, Kakuro. The ex­pla­na­tion of gameplay is clear; the approach seems reasonable.

The code, however, is horrible. It’s ugly, it’s verbose, and it’s repetitive. Consider that the code for doing some operation to a row is almost identical to doing the same operation to a column, but no attempt is made to abstract such operations into helper functions.

Or how about this un­ex­plained fragment to see if a column is complete, which is repeated often, with minor variations:

pattern = "123456789"
For r = 1 To 9
    pattern = pattern.Replace(actual(c,r).ToString(), String.Empty)
If pattern.Length > 0 Then
    Return False
End If

To me, it’s obvious that this is a poor man’s set difference operation. To a novice programmer, I doubt it.

Examples should be exemplary and held to a higher standard than code that is not intended for public view. All too often, sample code ends up in production. When I wrote samples for classic ASP, I took care to make them good code.

The book is short. The author could have shown some ugly code as an initial solution, then cleaned it up and explained why the new code was better. That would have done his readers a greater service.

I cannot recommend this book to novices: they won’t learn good habits from it.

blog comments powered by Disqus
Gay Bingo » « Safari 4: FAIL!