Archive

Posts Tagged ‘Programming’

Code Review Does Not Have to Suck

February 24th, 2011 No comments

I have been using this saying for the last 6 years, “Code Review Does Not Have to Suck”.  I just saw today that SmartBear, who makes the wonderful CodeCollaborator peer code review tool, which I also use, released a quick 3-minute video explaining this.

Peer code review is a critical milestone step in any good development process; especially with distributed teams.  CodeCollaborator, Crucible, and Kiln are all great tools for facilitating peer code review without having to go through all the headache of gathering files, scheduling meetings, pissing off your co-workers, etc.

Check out the video.  It’s worth the 3-minutes.

How to Design a Good API and Why It Matters

February 4th, 2011 No comments

I am starting to design a new API for an application that will need to integrate with other applications.

Before doing so, I always make it a point to re-watch and re-read this presentation given by Joshua Bloch from Google.  Joshua does a fantastic job going over the DOs and DON’Ts of good API design.

 

Joshua Bloch Presentation on API Design

 

You can also download the slides here.

Windows Bulk File Move Script

October 13th, 2009 1 comment

We have some applications that leave files sitting in a folder location out on some file server.  Eventually the disk space gets used up, or Windows starts choking because there are too many files in a directory, etc.  To delete the ‘old’ files, a support technician will try to connect to the file share, sort the files by date, and delete the files within a date range, e.g., older than a year.

What can happen, especially on a Window server, is that the operating system will attempt to load all the file information into memory, so that you can see it, and then tell it to sort all those files by date, which in turn, increases the time and memory needed by the OS to accomplish this.  If the file size/quantity is too great, the problem that caused you to want to clean up the disk space will reappear and choke Windows once more.

After going through this more than twice, I decided to write a JavaScript and execute it using the Windows Scripting Host (WSH).  The script performs very well and was able to iterate through a directory of 700,000+ files in a matter of an hour.  This was including the log statements from being enabled.  Disabling the ‘Echo’ statements may increase performance.

Here is the script:

var fso = new ActiveXObject('Scripting.FileSystemObject');
var argArray = new Array(WScript.Arguments.Named("src"), WScript.Arguments.Named("s"), ".");
var inSrcDirName = coalesceArray(argArray);

argArray = new Array(WScript.Arguments.Named("dest"), WScript.Arguments.Named("d"));
var archiveDir = coalesceArray(argArray);

argArray = new Array(WScript.Arguments.Named("year"), WScript.Arguments.Named("y"));
var beforeYear = coalesceArray(argArray);

if (archiveDir != null) {
	if (!fso.FolderExists(archiveDir)) {
		fso.CreateFolder(archiveDir);
	} else {
		fso.CreateFolder(archiveDir + "1");
	}
}

var gfldr = fso.GetFolder(inSrcDirName);

ArchiveFiles(gfldr);

function ArchiveFiles(fldr) {
	var i, bias;

	WScript.Echo("Loading all files...");
	for (files = new Enumerator(fldr.files); !files.atEnd(); files.moveNext()) {
		f = files.item();
		dt = new Date(f.DateCreated);
		bias = dt.getTimezoneOffset();

		dt.setMinutes(dt.getMinutes() - bias);

		fileYear = dt.getFullYear();
		//WScript.Echo(f.Name + ":" + fileYear);
		if (beforeYear > fileYear) {
			WScript.Echo("Moving \"" + f.Name + "\" with year " + fileYear + " to " + archiveDir);
			f.Move(archiveDir+"\\" + f.Name)
		} else {
			WScript.Echo("Skipping \"" + f.Name + "\" with year " + fileYear);
		}
	}
	WScript.Echo("Done loading files.");

}

function coalesceArray(arr) {
	var i;
	for (i = 0; i < arr.length; i++) {
		if (arr[i] != null) {
			return arr[i];
		}
	}
	return null;
}

Executing the script is performed by passing a source directory, destination directory (to move the files to), and a year to mark the year that all files must be equal to or younger than to not be moved as parameters.

Example:

cscript movefiles.js /s:c:\myapp\data /d:c:\backup\archive_2006 /y:2007

This will move all files from the c:\myapp\data folder that were created before January 1, 2007 to the c:\backup\archive_2006 folder.  The script can be easily modified to use the ‘modified’ or ‘accessed’ date instead.

Hope this helps someone else as much as it helped me.