Edited to improve the code samples slightly. Also still tweaking the CSS to get the code to display better.
Update 2: just found the preserve code formatting plugin. Fighting wordpress (which was completely screwing up the code tag) was no fun.
A lot of people recommend include a practical test as part of an interview for a programming position. Quite a few people, including some notable people, recommend doing this on a whiteboard.
I think that this stinks: somebody trying to write code on a whiteboard is no reflection on their abilities as a programmer. It isn’t just that it is so different to the way people normally write code: it penalises people who write code well. It is good programming practice to design the skeleton and then to put some flesh on those bones. For example, I have got into the habit of writing closing braces for blocks as soon as I write the opening brace. In my there is no question that this is a good idea, but this is based on the assumption that the space between the braces is effectively infinitely expandable, which is the case when writing normal code but not when writing code on paper or on a whiteboard.
Let’s take a simple function, that retrieves some data from the database (C#, illustrative purposes only, not tested), writes it to the screen. I write code in multiple passes. The first pass through might look something like this:
// TODO: retrieve data
// TOD: loop through data
// TODO: write totals
The next pass would fill some of that in:
// retrieve data
DataTable data = this.GetData();
// loop through data
foreach (DataRow row in data)
{
TableRow row = new TableRow();
TableCell cell1 = new TableCell();
cell1.innerText = row["label"].ToString();
TableCell cell2 = new TableCell();
cell2.innerText = row["amount"].ToString();
this.Results.Rows.Add(row);
}
// TODO: write totals
And some more in the next pass:
// retrieve data
DataTable data = this.GetData();
int total = 0;
// loop through data
foreach (DataRow row in data)
{
TableRow row = new TableRow();
this.AddCell(row, row["label"].ToString());
this.AddCell(row, row["amount"].ToString());
total += Convert.ToInt32(row["amount"].Value);
this.Results.Rows.Add(row);
}
// write totals
TableRow total = new TableRow();
this. AddCell(total, “Total”);
this. AddCell(total, total.ToString());
this.Results.Rows.Add(total);
private void AddCell(TableRow row, string value)
{
TableCell cell = new TableCell();
cell.innerText = value;
row.Cells.Add(cell2);
}
And probably a final pass, to alternate colours on the rows and set some styling on the total:
// retrieve data
DataTable data = this.GetData();
int total = 0;
// loop through array
for (DataRow row in data)
{
DataRow row = data.Rows[i];
string style = “background-color:” + (i % 2 == 0 ? “#FFFFFF” : “#CCCCCC”) + “;”;
TableRow row = new TableRow();
this.AddCell(total, row["label"].ToString(), style);
this.AddCell(total, row["amount"].ToString(), style);
total += Convert.ToInt32(row["amount"].Value);
this.Results.Rows.Add(row);
}
// write totals
TableRow total = new TableRow();
this.AddCell(total, “Total”, “font-weight:bold”);
this.AddCell(total, total.ToString(), “font-weight:bold”);
this.Results.Rows.Add(total);
private void AddCell(TableRow row, string value, string style)
{
TableCell cell = new TableCell();
cell.innerText = value;
if (style.Length != 0) cell.Attributes["style"] = style;
row.Cells.Add(cell2);
}
And normally this would have been broken out into a number of functions, but I think the point is clear. One of the most frustrating experiences of my life, technology-wise, was hand-writing code as part of an exam.
In more complex code this is even worse: when you are writing the code it is not clear how long a block is.