Always Get Better

Never stop looking for ways to improve

November 12th, 2009

Google has taken another step toward world domination with the launch of an experimental new programming languages aptly named “Go”. Go promises to pick up where C and Python left off, providing programmers with a new garbage-collecting low level languages suited to efficient server programming.

I spent the better part of last night looking for a way to get Go‘s tool chain to run under Windows using Cygwin. Unfortunately the tools can’t be created; even if they could, they would produce binary files which would be unusable within Cygwin/Windows due to their low-level nature.

If you are a Windows programmer hoping to give Go a try, your best bet is to download the andLinux distribution – this is a native Linux distribution that runs similar to a virtual machine under Windows. Once you have set it up, go to the Installing Go page for instructions on getting started with the new language.

November 5th, 2009

If you’re using SQL Server Management Studio Express under Windows Vista and see either of these errors:

CREATE DATABASE permission denied in database 'master'

or

The database [Name] is not accessible. (Microsoft.SqlServer.Express.ObjectExplorer)

Here’s the fix:

  1. Close SQL Server Management Studio Express
  2. Open your start menu and locate that program.
  3. Right-click on the Management Studio and choose ‘Run as Administrator’
  4. Fixed!

I swear the simplest solutions can be the hardest to find – hopefully this saves someone (or my forgetful self!) some aggravation.

October 10th, 2009
Get Yourself Out of Debt
Creative Commons License photo credit: faungg

When creating reports that are calculation-heavy, it’s tempting to create functions like ‘calculatePercent()’ or ‘calculatedMedian()’ so the correct numbers are available on demand.

Sounds good and convenient, but what happens when you have 100 different calculations to make across 50,000 data records? Each report will take 5 million passes to generate. That could take a long time especially if there are multiple reports being generated.

DRY – Don’t Repeat Yourself

Fortunately, the solution is straightforward. Rather than passing through those 50,000 records 100 times (once for each percentage needed), create an array for your values and calculate ALL of them in one shot. Then, just have calculatePercent() and calculateMedian() call from that array. Sounds simple, and it is as the pseudocode below shows:


for each record:
for each value:
valueList[value].append( record[value] )

July 12th, 2009

You can still see ghosts of the traditional “waterfall” method of software development in modern agile practices. The traditional model involved long periods of planning followed by development and extended maintenance periods – ideal for long-lived systems (I’m shuddering and thinking of COBOL apps running on mainframes).

With today’s rapidly-evolving platforms and business’ intolerance for risk, developers are called upon to deliver solutions faster on changing hardware and software. The focus has shifted toward quick development cycles and constant integration.

At the basic level, the process is the same: plan, build, deploy.

A full understanding of the traditional “waterfall” software development lifecycle (SDLC) can help any programming communicate more clearly with project managers or clients who are more inclined to understand projects in these terms.

Phase 1: Planning (Logic)

The planning phase of the SDLC involves communicating with the project’s key stakeholders in order to understand the project’s requirements. What are the goals of the project, and what are the expected costs?

At this stage there is no program code involved, nor is there discussion of any particular programming language or framework. The goal is to understand what the new software will do and why; not how.

The planning phase is also the time to assess other possible solutions that could meet the client’s recommendation. This is where most analysts fail – rather than let their project stop at this point, many organizations will endeavour to push their own solution. Try to ignore the dollar signs – if you can meet your clients needs by integrating an existing solution rather than developing something new, you will make them happier because they save money and end up with a product that is completely within their best interests.

Phase 2: Design

The design phase brings us closer to writing code, but we still haven’t opened our IDE yet. At this stage our job is to create the software on paper based upon the requirements we came up with in the previous step.

Many clients feel like they are in over their head when your design starts taking form, but you can’t let them off the hook. You need to take the time to explain your design and make sure the client is fully aware and in agreement with what you are doing. Teach them how to communicate with you; learn the terms they use so you can speak their language.

Phase 3: Implementation

When we start programming, this is the part we envisioned ourselves doing. In reality, this is the part we do the least (assuming we did our job right in phases 1 and 2). We’re talking about getting down and dirt with raw code.

Phase 4: Maintenance

This is the most expensive part of the project – keeping the software running. If you’re lucky you will be gone after phase 3 – if your successor (the maintenance programmer) is lucky you will have done a thorough job of your documentation in all of the previous phases.

Maintenance deserves special thought because it occurs over time, so it gets absorbed as an ongoing cost to the business. It can be hard to justify spending a lot up-front to develop a new system when the existing one “already works, and costs less”. Always weigh the ongoing costs of developing and supporting features for an aging system versus performance gains and optimizations possible with new software.

Sometimes it makes sense to keep existing software in operation; sometimes businesses hold onto decaying systems far too long. There will always be a point where the newer system costs more to operate than the old would have cost for the same stretch of time; however, the total cost of ownership – satisfaction, new features, bug fixes – needs to be considered, not just the cost of implementing the new system.

May 14th, 2009

One of my absolute favourite statements in C# is the using statement (not to be confused with the using directive, which is what we use to import libraries like System.Web into our projects).

using forces us as programmers to be honest about releasing memory to the CLR. Whenever we use an unmanaged resource like an SQL connection or file IO handler, the garbage collector will eventually eliminate any open streams or connections associated with that resource. However, “eventually” doesn’t cut it when we’re dealing with SQL connections on a production server – we need to make sure the connections are released no later than when we’re done with them.

If you come from the C++ world, you’re probably (hopefully) used to calling delete to deallocate any memory you reserved. You also know that forgetting the delete (or delete [] on arrays) results in a memory leak. You might think of Dispose() as C#’s implementation of the delete statement.


using ( SqlCommand cmd = new SqlCommand( sqlStatement, sqlConnection ) )
{
// Do something
}

using acts as a try…catch…finally block, so if your code fails your object will still be disposed. The using statement keeps everything wrapped into a neat little package so you don’t forget to keep your local variables in scope.

Like I said, this is one of my favourite features in .NET (lock { } is similarly beautiful). You can use the same construct in VB as well.

May 3rd, 2009
Anchor Chain
Creative Commons License photo credit: chefranden

Link rot occurs when a page linked to from within your web site becomes invalid. Visitors to your site who click on that link will receive a 404 “Not Found” message rather than the quality content they were expecting to find. This matters to you as a web site operator because:

  1. The value of your content is diminished because your well-selected links no longer provide value to your readers
  2. Search engines indexing your site treat the broken links as a sign that your page is out-dated and therefore needs to be relegated to the bottom of their indexes

Fortunately, it’s very easy to deal with link rot. Essentially it boils down to arming yourself with a link validation tool – I recommend Xenu as the best free tool for this purpose – and run it periodically (once a month should suffice for smaller sites). Xenu will point out which links on your site are no longer working and on which pages they can be found so you can take action. The only problem Xenu has is that Wikipedia blocks this application so any links you make to Wikipedia will have to be manually checked (but, you really shouldn’t be linking to Wikipedia anyway, it is not good reference material).

When you find a broken link, you have a few options available:

Fix It

Sometimes website owners change their site’s structure without regard for incoming links. When you operate your own site, try to take all possible steps to prevent this from happening. If you are the victim of this kind of foolishness, you may be able to go to the offending site and find your link’s new path.

Change It

If you have referenced a news article or other topical content, try searching for it on the host site as above. This kind of content is the worst for being removed by its owners when it is no longer current, but often the same content can be found on another news organization’s site. If that’s the case, make the change. The original site won’t benefit from your link, but they don’t deserve to anyway.

Remove It

If your content can be re-written so removing the link doesn’t reduce its usefulness, this can be an option.

Remove the Article

If your post is written in response to content that no longer exists, just delete your post since it isn’t useful anymore. Be sure to protect the validity of any incoming links by checking your site for any internal references to the post. In order to maintain incoming links from other web sites, you may consider writing a brief explanation as to why the content is no longer available and poitning toward some of your newer work.

Credit where credit is due: I thought to write this post because of a prompting from Problogger who featured an article dealing with link checking on blogs this weekend. I found a good number of broken links on my various sites and ended up re-discovering and updating content I hadn’t thought about for some time.

March 3rd, 2009

Suppose you have a button in your Flex program. When the user clicks on your button, they are prompted to choose where they would like to download a file. But… the file never comes:

protected function buttonClick():void
{
var mpRequest:URLRequest = new URLRequest(“soundbyte.mp3″);
var localRef:FileReference = new FileReference();

try
{
// Prompt and downlod file
localRef.download(mpRequest);
}
catch (error:Error)
{
trace(“Unable to download file.”);
}
}

The reason the file never downloads is because as soon as the function completes, it goes out of scope. As far as I know, this has to do with the event model using weak referencing by default when triggered by clicking on the button.

The simplest solution is to simply move the variables outside the button:

protected var mpRequest:URLRequest;
protected var localRef:FileReference;

protected function buttonClick():void
{
mpRequest = new URLRequest(“soundbyte.mp3″);
localRef = new FileReference();

try
{
// Prompt and downlod file
localRef.download(mpRequest);
}
catch (error:Error)
{
trace(“Unable to download file.”);
}
}