Kindle for Programmers

Introduction

The past few months, I seemed to have absorbed information like a sponge. My amazon.com’s orders history said I had bought 9 books this year. And there were books I purchased from my neighborhood bookstore that I didn’t keep track. I didn’t see the pattern would stop and my bookshelf had no more vacancy. I had to put away some books in boxes. I had books scattered on tables and the floor. I needed a solution; I bought a Kindle.

Kindle DX Graphite (Image courtesy of Amazon.com)

My Kindle is the DX kind with 9.7″ display. I’ve had it for two weeks and I’ve finished a few books. I read books a programmer would read–programming or technical books. To name a few, these are the books I’ve read or are currently reading on my Kindle.

I’ve been content with traditional books other than the fact that they take up so much space. The Kindle’s solved that. But are there any trade-offs?

No More Dual Pages

Dual Monitor Workstation

Imagine you walked in one morning to your cubicle only to find that one of the two monitors you had had been taken away. Now we all know the benefits of dual monitor setup. What are the drawbacks of seeing only a single page at a time?

A Programming Book

The technical books that we, programmers, read contain code listing, diagrams, and/or screenshots. A traditional book will have one of those illustrative elements (code listing, diagrams, or screenshots) and the corresponding, explanatory paragraphs immediately on the same page or on the opposing page. When we read on a Kindle with its single-page “feature,” we don’t have the convenience of having all correlated content in front of us at the same time. We can’t as seamlessly switch back and forth between reading the paragraphs and examining the illustration as we can a traditional book. But we can always turn the page back and forth, can’t we? <sarcasm>Have fun with that. (Read on.)</sarcasm>

Slow Refresh Rate

It takes two seconds to turn a page on Kindle. It’s not a big deal when we only have to do it once every few minutes. But when we have a big chunk of code which takes up one full-page, the explanatory paragraphs must go to the next page. The routine is to read a portion of code listing, read two or more sentences, and repeat. But the routine becomes irritating when it’s interrupted with 2-second delays of page turning.

When we finish reading a chapter or a section of a book and arrive at another, sometimes we want to know how big the section is before we commit to continuing reading. To accomplish that, we have to turn few pages ahead until we find the beginning of the next section. With the slow refresh rate of Kindle screen, turning 2 pages or more is a chore.

Inconsistent, Less Readable Code Listing

Code Listing with Nonmonospace Font

There are three formats of e-book that we can read on a Kindle: PDF, mobi, and kindle. The display of code listing seems to vary by the format, publication date, and the publisher. (The preceding sentence is mostly speculation because I don’t have enough samples.)

Code Listing Containing Light Shade

Unlike the content where the same font family is used across all e-books (excluding PDFs), code listings in different e-books use different font family, with varying sizes (some are too large which cause unnecessary wrapping), and resizability (whether we could resize the font). Some books still sticks to using images to display code listings. I will post more detailed observation and samples on the code listing from various books in part 2 of the post.

Code Listing with Image (as Opposed to Text)

Some code listings use lighter shades which reduces contrast, and, consequently, readability. Some other do not use proper indentation which makes the code block to not have clear structure.

Books Availability

So far I’ve managed to get the books that I wanted in kindle (from amazon.com) or mobi format (from the respective publisher). mobi format in overall reads as well as the kindle format. Some features in Kindle are not available for mobi format: synchronizing to furthest page read, viewing book description, and viewing popular highlights; I rarely or do not use those features.

If a book is available electronically in PDF format only, I advise against purchasing it. The text may come out too small to comfortably read. Resizing the page to optimize viewing is somewhat exasperating. I could zoom in and set the view port to the center (where the content is), but when you turn the page, the view port will go to the left of the page and I have to re-position the view port back to center.

When a book is available electronically in PDF format only, there may be a chance that the PDF has certain feature that Kindle is not able to open it. The PDF of “Applying Domain-Driven Design and Patterns: With Examples in C# and .NET,” for example, can’t be read in Kindle because it’s, I’m guessing, watermarked.

Conclusion

I went to the bookstore the other day and I was tempted to bring home a book that I liked. Yes, traditional books are still that much better than e-books. Unlike music collection, you only need one book at a time. For sure I didn’t buy Kindle for its portability. But I seriously don’t have space for things that will be used for a short period of time and drown my room most of the time. In the end, Kindle has solved my storage problem.

Verbose WHERE Clause with LINQ to SQL

Table Persons

If you come from the world of always-use-stored-procedures or at least you always write your own SQL query, you’d be curious how LINQ to SQL translates your C# code to SQL query. Say you have table Persons with fields PersonId, FirstName, LastName, and Age. PersonId is the primary key of the table Persons.

In this example, we want to update a particular row (PersonId = 1) with a new Age value. So, we write the following block of code in our application.

Entities.DataContext context = new Entities.DataContext();

int personId = 1;
Entities.Person person =
    context.Persons.SingleOrDefault(p => p.PersonId == personId);
person.Age = 24;
context.SubmitChanges();

To see the generated SQL query, we want to redirect the log of DataContext object to console.

Entities.DataContext context = new Entities.DataContext();
context.Log = Console.Out;

int personId = 1;
Entities.Person person =
    context.Persons.SingleOrDefault(p => p.PersonId == personId);
person.Age = 24;
context.SubmitChanges();

Now, when we run the application, you will see the SQL queries generated by your code in console window. The SQL query will look something similar to the following.

UPDATE [dbo].[Persons]
SET [Age] = @p4
WHERE ([PersonId] = @p0) AND ([FirstName] = @p1) AND ([LastName] = @p2) AND ([Age] = @p3)
-- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [1]
-- @p1: Input NVarChar (Size = 4; Prec = 0; Scale = 0) [John]
-- @p2: Input NVarChar (Size = 5; Prec = 0; Scale = 0) [Smith]
-- @p3: Input Int (Size = 0; Prec = 0; Scale = 0) [23]
-- @p4: Input Int (Size = 0; Prec = 0; Scale = 0) [24]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.4926

If you’re used to writing your own query, you probably wonder why the WHERE clause is verbose (or wordy, an English major will say). You see the query tries to find the row to update by matching every single field in the table with their old values. “Well, that’s dumb, ain’t it?” you say. “We can find the row to update by matching only the value of the primary key, PersonId.” Here’s how.

Properties Window

  1. Go to your Solution Explorer in Visual Studio and double click the .dbml file.
  2. Click the property PersonId on the class diagram.
  3. Go to Properties window (it’s on bottom right on default window setup) and find the property Update Check. The current value should be Always.
  4. Change it to Never.

Repeat step 2-4 for all properties (FirstName, LastName, and Age).

Update the application to update the Age to 25 (previously 24). Rebuild and re-run the application. You will see the update statement that you probably always write.

UPDATE [dbo].[Persons]
SET [Age] = @p1
WHERE [PersonId] = @p0
-- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [1]
-- @p1: Input Int (Size = 0; Prec = 0; Scale = 0) [25]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.4926

So, what’s the purpose of the property Update Check? The property description says it all. It’s to control the frequency of optimistic concurrency checking.

Use Cases (Visitors)

A visitor to the web site (Framing the Sky) will have the ability to browse uploaded photos and leave feedbacks. When arriving at the front page, the visitor will see the latest uploads and their corresponding patterns. The visitor will have the choice to navigate to the next page (older uploads) or view more detailed information of a photo.

  1. Click More Info link below the photo
  2. Hover mouse over the photo to display annotated patterns
  3. Click an oval region
  4. Click thumbs up to indicate that the pattern is indeed used properly

The visitor will have the following abilities as well after selecting a region.

  • Suggest rename pattern
  • Suggest merge pattern

The visitor will have the ability to leave a comment to a photo.