Always Get Better

Archive for April, 2008

Form Trumps Function

Wednesday, April 30th, 2008

One (sad?) truth I have learned through the past several years is this: Form Trumps Function, every time. You can write the most elegant code in the world, produce an application that meets requirements 100%, but if it isn’t pretty to look at, your time has been wasted.

In fact – you can show a client a beautiful shell of an application that has no functionality at all, and they will love it.  Show them the same application feature-complete but without the design, and they will not want to pay for it.

The bottom line is this – when writing code, be it for web or for the desktop, always have an eye for the ‘look’ of your product.  Even when a separate designer is involved, the programmer can take responsibility for the ‘feel’ of the application – how the buttons move around, tab order, this kind of thing.

I am considering spinning this blog off to a second one devoted to designing applications (both GUI and web).  The design element can’t be ignored, so hopefully getting programmers more involved in the process will improve the situation on all levels.

C#: Finding the Number of Days Between Two DateTime Items

Tuesday, April 29th, 2008

One very common requirement is for the number of days elapsed since a particular Date and Time. In C# this can be accomplished through the use of the TimeSpan class.

The easiest way to create a TimeSpan is like this:

[source:c#]
TimeSpan tsMySpan = DateTime.Now.Subtract( dtCompareTime );

// The number of days elapsed can be accessed like this:
// tsMySpan.Days
[/source]

Handling Relative Paths Programmatically In ASP.NET

Friday, April 25th, 2008

One of the nicest features in ASP.NET is its out-of-box support for relative paths in hyper links and other controls. This is very important for developers whose code resides within the root of their testing environment but within a sub-directory of the production server.

Whereas one would have to carefully program things like image links to point at “/applicationbasepath/images/”, in ASP.NET we can simply use “~/images”. Genious!

When writing custom code, however, the work isn’t done for us automatically. We have to pass our URL to a server-side function in order to convert the “~/” into a base path usable by our visitors.

There are two ways of doing this:

Using Relative Paths From a Web Page or Control

If we are programming a page template or a server control, we can use the ResolveUrl() function provided by our environment context.

[source:c#]
string resolvedUrl = ResolveUrl( “~/index.php” );
[/source]

On AlwaysGetBetter.com, “~/index.php” would resolve to http://www.alwaysgetbetter.com/blog/index.php. On another site, it might resolve to the root such as http://www.abetterblog.com/index.php. Programming this way makes our solution more portable.

Using Relative Paths From Outside a Web Page

Sometimes we need to use relative paths from outside the context of a web page. For example, if we were to make a change in our Global.asx file, the program code we use may not be considered to be within a web page scope.

Fortunately, .NET provides us with the static helper class VirtualPathUtility.

[source:c#]
string redirUrl = VirtualPathUtility.ToAbsolute(“~/redirPage.aspx”);
Response.Redirect( redirUrl );
[/source]

Technorati PostClaim

Thursday, April 24th, 2008

Technorati Profile

Apache: Enabling mod_rewrite on Windows 2003 Server

Monday, April 21st, 2008

Recently I had to set up a WordPress blog on a Windows 2003 server, which involved installing Apache, PHP and MySQL.  Everything went pretty well – except for the typical PHP.ini problems I shall write about later.

Once the database was set up and I had the blog running, I tried to adjust the permalink structure using .htaccess rewrite rules so they would be more human-readable.

Out of the box, mod_rewrite isn’t enabled on Windows Apache.

The steps I took to resolve the problem were:

  1. Uncomment the line containing ‘LoadModule mod_rewrite modules/mod_rewrite.so
  2. In the VirtualHost directive hosting the blog, add ‘AllowOveride All
  3. Restart Apache

It works like a charm, now.  All the tools ship with Apache, it’s just a matter of configuring them as needed.

Editing the HOSTS file in Windows Vista

Friday, April 18th, 2008

Windows Vista keeps the HOSTS file locked down so only users with elevated permission can edit it.  I found the fastest way to add lines to this file in my own system was:

  1. Open the directory containing the file (C:\Windows\System32\drivers\etc\hosts)
  2. Copy the file to my Documents directory by right-clicking Send To…
  3. Editing the file in UltraEdit (or notepad, or whatever)
  4. Moving the file back to the etc directory.

The act of moving it back causes Windows to prompt you for privilege elevation.  It’s possible to raise your text editor’s privileges, but going in through the command line – at least to me – seems like a much longer and more tedious way of doing the same thing.

404 Errors when Accessing ASPX Pages on Windows 2003

Wednesday, April 16th, 2008

By default, windows 2003 Server is locked down and won’t display ASP.NET pages.

To enable them you must set its status to Allowed within the Web Service Extensions of IIS.