The Mighty “Success Exception”

For all those Java programmers who feel lonely in the land of .NET – here’s a class just for you!

using System;

namespace Biz.Your.Company.Goes.Here.Util
{
	public sealed class SuccessException : Exception
	{
		public SuccessException() : base() 
		{
			System.Environment.Exit(1);
		}

		public SuccessException(string msg) : base(msg)
		{
			System.Environment.Exit(1);
		}

		public SuccessException(string msg, Exception exception) : base(msg, exception)
		{
			System.Environment.Exit(1);
		}
	}
}

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.

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