Being a C# developer by trade, I naturally spend a lot of my working life programming computers. Whilst I by no-means profess to be a “guru” of any kind, I do notice that there is one aspect of software development that is often neglected from discussion, and most programmers will hopefully agree with me as I explain.

Programming is often described as an “art” – and whilst I agree with that statement, it is probably for different reasons than most programmers would assume. I believe one of the most important (and inherently artistic) elements of programming, to be that of simple document formatting.

With the majority of application development shifting to the ‘net, maintenance-programming is arguably becoming more important. As such, I myself would much rather attempt to maintain something complicated that was formatted and factored sensibly and aesthetically, than something programmatically simple, but with poor formatting, naming and structure.

Take the following made-up Page_Load example:

protected void Page_Load(object sender, EventArgs e)
{
    CreateDefaultWidgetProperties();
    if (WidgetProperties == null)
    {
          divMain.Visible = true;
     }
     
     HideWidgetTable(false, "customers", null);
     //The keyword Search Keyword = tbSearch.Text.Trim();
    lblMessage.Text = string.Empty;
    lblMessage.Visible = false;
    int PersonID = Int32.MinValue;
 
    if (!Page.IsPostBack)
    {
        if (Session[SessionKeys.ManageWidget.ContinueAction] != null 
                              && Person.Details.IsSomething == false)
        {
            string action = (string)Session[SessionKeys.ManageWidget.ContinueAction];
            ContinueAction(action, true);
        }
       
        SetupColumnHeaders();
       
        switch (ManageWidget.SearchType)
        {
            case ManageWidget.SearchTypes.DateSearch:
                LoadFromDateSearch();
                break;
            case ManageWidget.SearchTypes.RefSearch:
                LoadFromRefSearch();
               break;
            case ManageWidget.SearchTypes.StdSearch:
                LoadFromStandardSearch();
                break;
            default:
                SetupPage(WidgetProperties);
                BindRepeater(WidgetCollection);
                break;
             }
        }
       
        if (divOtherUsers.Visible == false || ddlPeople.SelectedValue == Person.ID.ToString())
            personId = Person.ID;
        else
            personId = int.Parse(ddlPeople.SelectedValue);
       
        if (personId > 0) Session.Add("CurrentPerson", personId);
}

As a rule of thumb, especially when dealing with program entry-points (such as .Net page-loads) I strongly believe that they should contain as little “code” as possible (if you have over a page in a method, you’re probably doing something wrong) – but more-so, they should contain an “code-index”.

When writing a page of code, consider you are writing it as you would an instruction manual, wherein having everything listed in one big chunk could potentially be hard to follow. Using the example of a VCR, if you only wanted to know how to fix a blinking VCR clock, you would go straight to the section in the instructions “How to set your VCR’s time”.

It should be the same with code. If I have to fix a bug in my imaginary “Widget Search”, I’ll know straight-off that it’s very likely to be in the ProcessWidgetSearch() method, meaning my maintainence will probably be faster, and my head will hurt less.

So, for example:

protected void Page_Load(object sender, EventArgs e)
{
    CreateDefaultWidgetProperties();
    divMain.Visible = WidgetProperties == null;
    HideWidgetTable(false, "customers", null);
    ProcessKeywordSearch();
 
    if (!Page.IsPostBack)
    {
        if (this.ContinueAction != null && !Person.Details.IsSomething)
        {
            ContinueAction(this.ContinueAction, true);
        }
        SetupColumnHeaders();
        ProcessWidgetSearchType();
    }
 
    SetCurrentPerson();
}

Another positive that should naturally fall-out of such an approach will be that of code re-use. If I wish to make a call to process my widget search-types again, I have already wrapped this logic in a method, meaning I’m less likley to copy / paste the same block of code (which would add to further maintainence).

The ‘art’ of this process is choosing which code-elemets are abstractly linked – however if you take the time to write code as if you were formatting it for someone else to read, not a computer, you will notice this will point your abstraction in the right direction, and everyone will say:

“What a nice programmer”

kick it on DotNetKicks.com