Monday 24 December 2007

Blogger Code Formatting

I like blogger in general but it's probably not the standard geek's first choice; I suggest this simply because the editor has no UI functionality to preserve the formatting of the code samples most geeks want to paste in. HTML, God bless it, is HTML and Blogger doesn't help you write anything more than basic HTML. Moreover, Blogger's line break "helper" apparently interferes with the old standby tricks like pre, code, and blockquote. I say apparently because either there are a lot of untested myths out there or Blogger has changed how it deals with these tags over time.

So, to get my code samples to paste in effortlessly and to set the story straight, I present below the way I learned to beat the blogger editor and it's as simple, as--wait for it--the trusty pre tag. To paste in the sample below, I simply dropped into HTML mode, pasted in the class definition, wrapped it in a pre tag and fixed up a few line breaks. In my case, I've got the Convert line breaks option (Settings --> Formatting) set to to Yes.

Definitely better than before but pre does not preserve HTML and XML fragments and Blogger simply removes them from the post. The problem here lies mainly with the HTML and XML bits that look like HTML--I'm talking mainly about less than and greater than brackets. To get around this, I simply use an HTML encoder to drop in my markup, encode it, and then copy the output into Blogger. It's a pain but what can you do...


class Base
{
public virtual void DoWork ()
{
Console.WriteLine ("Base.DoWork");
}
}

Sunday 16 December 2007

Senior Developer Interview Questions

We had a couple of senior developer tenders go out at work recently and when it came time to interview the applicants I was asked to put together a test to flush out the wannabes. I've been on the receiving end of these tests in the past and considering this test would be written in the twenty to thirty minutes before a three-panellist interview, I thought I wouldn't be too hard. I'd say most of these questions are analyst programmer questions, if not junior dev questions, although I did through in a few tricks to gauge experience and I also left some questions fairly open in an attempt to understand the applicant's though process. If the applicant couldn't complete the test in the time period given, that would be okay too.

I've included the job criteria headers to help you figure out what I was looking for in each answer; not every question fits within a specific heading. The headings were printed on the test sheet.

The Perth .NET developer market has been on fire since I arrived two years ago and SharePoint work is going through the roof; as a result, we only had a single applicant put forward for the two or three tenders that went out. When and if we have more people across this test I'll attempt to generalise the results and post an update to this post on the effectiveness of this test.

I haven't included the answer here because like I said, some of them are fairly open and a lot of them should be fairly obvious if you've done an undergraduate CS course.

At least 5 year of software development experience and at least 2 year experience developing MS .NET applications

  1. Describe the difference between the execution environments for C++, C#, and Javascript
  2. Is C# a weakly-typed language? Why or why not?
  3. When should performance optimisation occur?
  4. List three problems with this code. Bonus: how could it be improved if you were using .NET 3.5?

    private string someVar = 1234;

    public string SomeVar
    {
    get
    {
    if (string.IsNullOrEmpty (someVar))
    {
    someVar = “5678”;
    }

    return SomeVar;
    }
    set
    {
    someVar = val;
    }
    }

  5. When would you mark a class with the internal visibility modifier?
  6. What’s the difference between a struct and a class? Are structs allocated on the heap?
  7. The property invocation below may return a null result. Rewrite the code to prevent the second line from throwing.

    object result = Employee.Address;
    result.ToString ();

Working knowledge of full lifecycle development methodologies, process and standards and project management; sound knowledge of object oriented system design and development...

  1. List one section heading you might expect to find in each of the following documents:

    · A functional specification
    · A technical specification
    · A UAT test case
  2. List three tasks you would complete before checking in a new class to the Tourism WA source control system.Name each component in the following diagram and briefly explain the relationship between Vehicle and Door:
  3. What is a use case?
  4. Briefly describe the difference between a class, an interface, and a type
  5. Briefly describe a software design pattern you’ve used on a past project and indicate how it helped or hindered code maintenance. Alternatively, describe a design pattern employed by ASP.NET.
  6. After designing a new system, your project manager asks you to estimate construction time for yourself and a junior developer. This is the first time the department has built this type of system and you haven’t previously worked with the junior developer. List three techniques you would consider to ensure your estimate is as accurate as possible

Solid understanding of internet protocols, web development mark-up languages and web standards...

  1. Draw a simple diagram showing the location of a reverse proxy in relation to a database server, a web server, the internet, and a client
  2. Using only inline CSS and DIV tags, write the HTML to produce a three-column, one row table (don’t worry about borders). Don't use tables.
  3. Will the Hello World! text be rendered green, red, or blue by the browser?

    <style>
    span { color: green }
    #makeItRed { color: red; }
    .makeItBlue { color: blue; }
    </style>
    <span id="makeItRed" class="makeItBlue">Hello World!</span>
  4. List three key components of this schema fragment. How many ReservationRequest elements are allowed in an XML document validated against this schema?

<xs:element name="Control">
<xs:complexType>
<xs:sequence>
<xs:element name="ControlID" type="xs:int" minOccurs="0" />
<xs:sequence minOccurs="0">
<xs:element name="ReservationRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="InDate" type="xs:dateTime" />
<xs:element name="Period" type="xs:int" />
<xs:element name="Adults" type="xs:int" />
<xs:element name="Children" type="xs:int" />
<xs:element name="Infants" type="xs:int" /> </xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:sequence>
</xs:complexType>
</xs:element>

Sound knowledge of the Microsoft .NET framework class libraries, ASP.NET and Web Services

  1. What improvements does the .NET 2.0 System.Collections.Generic namespace offer over the System.Collections namespace from previous versions of the framework?
  2. List two key differences between a user control and a web control
  3. Compare the .NET application cache and the ASP.NET output cache
  4. Write a call to the Format ( ) method of the String class to return the string “Sam is 38 years old.” Assume you have a variable declared and initialised as follows:

    int age = 38;

    Give an example of a situation when you would use a StringBuilder instead

  5. Describe the difference between application state, session state, and view state. Discuss session state in relation to scalability and suggest an alternative.
  6. List two techniques to secure web service communication on the internet

Ability to perform unit and integration testing...

  1. What purpose do the following serve when debugging a section of code:

    · The F11 key on your keyboard
    · The Immediate window

  2. Briefly describe the concept of “regression testing”
 

Copying a SharePoint Document Library Programmatically

Based on the number of posts out there about copying the content of a list or document library, I'm willing to suggest SharePoint hasn't made this particular task easy. Whether it's through the various UIs or programmatically, this task seems like it's more difficult than it should be. As I recently found out, even clearing the content in an existing list is a hassle.

Before I go on, a bit of background. We were initially using the in-built variation tools to copy content from a source language site to a number of target English sites--in other words, www.westernaustralia.com/en was being copied to the /uk, /au, /nz, and /sg sites. I won't bore you with the details but the variation tool was deemed too blunt for our requirements and one of the developers on my team wrote a custom variation tool to do exactly what we want. The custom variation tool copies sub sites and pages but unfortunately it doesn't copy documents in document libraries; we don't make extensive use of the Documents library you get when you create a new publishing site but a few speicific sites do contain PDFs that need to be pulled over.

Since a document library is a list at heart, I started by examining the SPList API, assuming it would provide me with everything I need to manage the list items. I also looked into the SPListItemCollection returned by the Items property of SPList, and the SPListItem class. SPList was pretty hopeless. SPListItemCollection was somewhat more helpful, exposing the standard Add, Delete, and Count methods, and SPListItem was really enticing with its CopyFrom and CopyTo methods. Of course this was nearly a complete waste of time as few of these methods and properties really helped out at all. CopyFrom and CopyTo failed at runtime, Delete works as advertised but SPListItemCollection does not overload the Delete method or provide a sister method to delete everything in the list, and Add only adds a new list item if you get the very confusing url parameter pointing at the right location (a quick hint: it's expecting the URL of the destination file in the case of a document library...).

When it was all said and done, I'd written my own ClearList helper method, cast my destination list to a SPDocumentLibrary, accessed the Files collection via the RootFolder property of said document library, and passed in the byte array representing the uploaded file.

Here's the code I ended up with to copy the contents of the Documents list in one sub site to the Documents list in another sub site within the same web application:

using (SPSite site = new SPSite ("http://dev-moss-mh:101/"))
{
using (SPWeb sourceWeb = site.AllWebs ["Source_Site"])
{
using (SPWeb destinationWeb = site.AllWebs ["Destination_Site"])
{


SPList sourceDocuments = sourceWeb.Lists ["Documents"];
SPList destinationDocuments = destinationWeb.Lists ["Documents"];


if (sourceDocuments.ItemCount > 0)
{
ClearList (destinationDocuments);


foreach (SPListItem currentSourceDocument in sourceDocuments.Items)
{
Console.WriteLine ("Adding: {0}", currentSourceDocument.Name);


byte [] fileBytes = currentSourceDocument.File.OpenBinary ();

const bool OverwriteDestinationFile = true;
string relativeDestinationUrl =
destinationDocuments.RootFolder.Url +
"/" +
currentSourceDocument.File.Name;

SPFile destinationFile =
((SPDocumentLibrary) destinationDocuments).RootFolder.Files.Add (
relativeDestinationUrl,
fileBytes,
OverwriteDestinationFile);
}
} } } }


As you can tell by the variable name, the Add method requires a relative URL pointing within the context of the destination site. This seems odd to me since Add ( ) is called on the destination list itself--why it can't figure out the destination URL is beyond me.

My ClearList implementation is also mildly interesting: deleting items within a foreach loop is obviously a no-no since the foreach syntax in C# is interacting with an IEnumerator object so my first attempt was to iterate over the list using a for loop and deleting list items from the zero index through to the final item in the list. This worked but only sporadically, occasionally leaving items behind. Calling ClearList a second time did the trick with a small list but that's just weird programming.

The solution I arrived at is detailed below and essentially comes down to the fact that the SharePoint list API must maintain a real-time (or part-time) connection with the database; in other words, deleting an item at index zero could mean SharePoint re-fetches the list content so by the time my for loop moves on to the next index, the list has effectively shuffled itself down so index zero is still populated. As you can see, I'm now simply iterating over the list and always deleting the item at index zero. I could have possibly used a while loop and the listToClear.Items.Count property directly but it's difficult to know whether SharePoint can be trusted in a case like this. I'll leave that up to you to try out...

private static void ClearList (SPList listToClear)
{
int initialItemCount = listToClear.Items.Count;

for (int counter = 0; counter < initialItemCount; counter++)
{
// Always delete the list item at index 0
SPListItem listItemToClear = listToClear.Items [0];
listItemToClear.Delete ();
}
listToClear.Update ();
}
 
 
 

Thursday 13 December 2007

LastModifiedIndicator Kind of Works

SharePoint includes a useful out-of-the box control that can be used to display the date and time a page was last modified. It's not without its hiccups, as I'll describe in a moment, but it's quick and simple to use.

The control in question can be dropped into a page layout or master page using the following syntax:

&lt;PublishingWebControls:LastModifiedIndicator runat="server" />

When the page is served, my local dev server displayed the text below (I'm guessing this display format is relevant to the regional configuration of the server):

12/14/2007 2:24 AM

The LastModifiedIndicator class is derived from WebControl and exposes no additional properties or methods to configure the format decoratively. This would obviously be quite handy because the next best "quality" alternative is probably to implement your own LastModified webpart. The format displayed above will be unsuitable in many cases; although I haven't played with the control's output at any length, a quick and easy hack might be to access the last modified string on the client side and use a bit of JavaScript to reformat its contents using the JavaScript date functions. Definitely not pretty but a little less heavy-handed than building your control or getting in there with some server side code.

You may have noticed a time of 2:24AM in the sample output I listed above; no, I'm not a late night hacker! I actually modified the test page I was working on at 11:24AM on 13 December 2007 but my dev server was telling me I modified it at the unwholesome hour of 2:24, one day in the future. I wish I could do that... or SharePoint would allow me to do that but no luck there so far. Anyway, I haven't tested this solution yet but the legendary Tania down the road from Tourism indicated she got the control to behave by "changing the regional settings of the site and all subsites to Australia and then changing to GMT." I'm not sure exactly what that means but I'm assuming she fiddled with the site settings.