Programming Tutorial: Comments

Comments are only effective if they threaten abuse.

/**
 * Note: The following array MUST be sorted in order for BinarySearch to work. I have taken the liberty of
 * creating it sorted to avoid this operation. If you really feel the need to break the manual sorting,
 * uncomment the array.sort below this, and then go out and stand in front of traffic. thank you.
 */

string[] allowed_items  = {"activate", "restore", "save", "update-relay", "verify-mount", "verify-restore"}; 

//Array.Sort(allowed_items);

if (Array.BinarySearch(allowed_items, list) >= 0) {
	/* -.v.- */
}

Discovered by Noah Massey.

Programming Tutorial: Covering All Your Bases

Make sure you cover all your bases when reseting variables:

if ($flag == "yes") {
	$form->creationDate = null;
	$form->runCount = 0;
	$form->statusCode = 1;
} else {   
	$form->creationDate = null;
	$form->runCount = 0;
	$form->statusCode = 1;
}

Discovered by @zewillow

Programming Tutorial: Unreachable Code

Always run non-existent commands after unreachable code.

if [ `id -u` != 0 ]; then
	sudo $0
	exit $?
fi

if [ `dpkg -l | grep sun-java5 | wc -l` -eq 0 ]; then
	sh /media/cdrom0/application/appupgrade; # causes reboot
else
	sh /media/cdrom0/application/sysupgrade; # causes reboot
fi

sudo updat-java-alternatives -s java-6-sun;

exit 0;

Discovered by Noah Massey.

Konica Minolta Canvas

At the end of the month I will be going to On Demand in Philly. I will be going to support my companies products and even help showcase a few of them. This quickly reminded me of Vision 2009 and the fun myself and some co-workers had waiting around to talk to customers. Instead of playing tick-tac-toe or Solitaire we used a product which will be released soon by KMBS. Simply put, the product is awesome. It has a lot of potential for growth, but at the moment it serves two purposes:

  1. Define Workflows for Printgroove
  2. Draw Process Diagrams

A normal workflow might look like:

sales-to-shipping

You can also create cool diagrams – or in this case a Shipping Label:

shipping-labels1

However, with a little bit of creativity and boredom you can create some pretty crazy things:

invasion1

roar

ninja-n-private-island1

Please note, these drawings are not my sole creation. They are the result of a few people in my department having fun.

Programming Tutorial: Readability

Readability is always more important than simple one-liners:

if ($form->minute == 0) { 
	$minute = "00";
} else if ($form->minute < 10) {
	$minute = "0" . $form->minute;
} else {
	$minute = $form->minute;
}

Discovered by @zewillow.

Programming Tutorial: Variable Initialization

Always make sure your variables are properly initialized:

$list = $_SESSION['list'];

if ($list == null || $list == "") {
	$list = $_SESSION['list'];
}

Discovered by @zewillow

RESTful Web Services in .NET (Part 2)

Now that I’ve finally got a bit of time I figured I should explain how we updated our old RESTful framework to be nice and slick. Instead of one method per class which handles all sub method variations we now have something much prettier and easier to maintain. The UserService found in the previous post can now look like:

public class UserService : RestService
{
	[RestMethod("GET", "^$")]
	public IList RetrieveAll()
	{
	}

	[RestMethod("GET", @"^(?<id>[0-9]+)$")]
	public User Retrieve(int id)
	{
	}

	[RestMethod("HEAD", @"^(?<name>\w+)$")]
	public User Exists(string name)
	{
	}

	[RestMethod("PUT", "^$", typeof(User))]
	public User Create(User user)
	{
	}

	[RestMethod("POST", "^$", typeof(User))]
	public bool Update(User user)
	{
	}

	[RestMethod("DELETE", @"^(?<id>[0-9]*)$")]
	public bool Delete(int id)
	{
	}
}

As you can see there are a lot of differences:

  • Heavy influence from newer APIs
  • Methods can return any Type
  • Methods can take 0 or more arguments of any Type
  • RestMethodAttribute has been added to match URLs against methods

Most of the code above should be self-explanatory, but in case it’s not here’s some help.

The method decorator [RestMethod("GET", "^$")] tells the framework that any HTTP GET requests against the UserService class without any trailing arguments (http://localhost/user.r) should be handled by RetrieveAll().

The method decorator [RestMethod("GET", @"^(?<id>[0-9]+)$”)] tells the framework that any HTTP GET request against the UserService class which ends with a number only (http://localhost/user.r/1, http://localhost/user.r/5000, etc) should be handled by Retrieve(int id). The cool thing here is that the regex group will provide a match to the argument name so our function will be called with whatever number the URL ends with. So calling http://localhost/user.r/25 where the following is the executed method will result in?

[RestMethod("GET", @"^(?<id>[0-9]+)$")]
public bool Retrieve(int id)
{
	return id == 25;
}

TRUE!! Pretty simple, and very cool. Another nice feature is when the HTTP request contains a body like a POST or PUT would. So POSTing to http://localhost/user.r with a body of:

{'user': {
	'first_name': 'Matthew',
	'last_name': 'Metnetsky'
}}

would call the method decorated with [RestMethod("POST", "^$", typeof(User))] and it’s user argument would actually be set assuming the class implemented a JSON serializer. All of the URL/body mappers are definable by implementing Binder and listing them in the web.config against a Content-Type. By default there are Binders for form variables, JSON, XML, raw streams, and a few more.

So why might I be discussing the external API of a closed library? To PUSH innovation in the current public ones. By taking these ideas and creating an “open” version I’m sure I’d be breaking my contract so I’ve got two options:

  1. Expose our public API to entice and PUSH others to innovate
  2. Get a lot of feedback which might enable me to get our library opened

So…. let me know…. ;-)