Programming in Windows et al.

Moving to Jekyll, and Failed Migration to Azure

October 6, 2012

tldr;

This is just an FYI that I am currently migrating from Wordpress to Jekyll. Getting to a point where I am happy may take a week or two.

Back story

Recently, I have been getting really slow speeds on Dreamhost, and have quite honestly had enough of it. Also, the hosting has been provided to me free of charge for years, and I thought it was about time to stop mooching.

The initial plan was to move over to Windows Azure Web Sites on a Pay-As-You-Go plan. I had some success migrating it to Azure, but I was having issues getting my domain working the way that I had it. Azure absolutely refused to recognise my www subdomain (it would 404 while the TLD worked fine).

I then discovered that my Wordpress installation had been infected by malware, and given this is the second time in many months that this has occurred (while running the absolute latest version), my frustration lead me to looking at alternative options.

Jekyll

I had been toying with the idea of running a Jekyll blog for a while, and honestly I don’t think I need any more complexity than that. I could use Azure for it and pay whatever miniscule bandwidth costs that come with it, but then I discovered that you can run a Jekyll blog straight from Github Pages for free. Sign me up!

I’m also using Jekyll-Bootstrap, which makes the process of theming my site easier, considering I have the visual prowess of a frog.

In Conclusion

Wordpress is completely gone, but I haven’t quite yet mastered my Jekyll installation, so please bare with me while I work out the kinks and actually decide what I want the blog to look like post-wp.

Discuss...

String.Trim vs Regex.Replace

October 3, 2012

DISCLAIMER

It has come to my attention that the programmer in this article has read this and was taken aback by my comments. This is not an attack on the programmer in question, although I can see how that impression was given.

The point I was trying to make is that programmers are being taught the wrong things. They finish their degrees and have a breadth of knowledge that some of which I don’t have, but due to the education system miss out on the staples of being a real working programmer.

“University students only learn how to build a space ship”


I encountered this line in some code while I was doing a review:

userName = Regex.Replace(userName, "[ ]", "");

It struck me as odd that the programmer would use this in place of String.Trim() to “remove whitespace from either side of a string”:

userName = userName.Trim(); //this is what I would have done

The programmer’s response was that he didn’t know about String.Trim() and just used what he knew.

My head is full of F#$%

I would argue that String.Trim is much clearer to someone reading your code (especially if you didn’t know regex), but in his case it wouldn’t be because he had no idea what Trim was. The other side effect is that the regex code in this instance would remove all the whitespace, not just leading or trailing.

What baffles me the most is that a programmer can be oblivious of string manipulation methods that are present in practically every language. To cite a few:

Java
String trim() - Returns a copy of the string, with leading and trailing whitespace omitted.
C#
Trim() - Removes all leading and trailing white-space characters from the current String object.
Python
str.strip([chars])
Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped

I’m sure that I could go on. Surely, there would be a similar method in every language that I could think of. I know that I’m picking on Trim here but the idea is that those key string manipulation methods are staples:

  • ToLower
  • ToUpper
  • Format (printf)
  • SubString
  • EndsWith
  • StartsWith
  • Etc...

Discuss...

Sublime Text - A Text Editor For Programmers - Part 1 - Package Control

July 20, 2012

Come With Me If You Want To Live

The text editor is a programmer's most valuable tool. Without it, you can't write any code at all. It is your life-blood. You need it to survive. It will save you, just like Arnie did. There are plenty of text editors out there that solve one problem or another. There is also a couple of editors that are popular for certain platforms:

vim

There are others (of course) like Emacs, Programmer's Notepad, or even plain old Notepad.exe.

How Did I Get Here?

I have tried a few editors in the last few years.

  • I learned vim which was a good experience, but I felt it didn't fit in with the Windows ethos. Being able to edit completely without the help of a mouse was interesting, but I just couldn't break completely free of it.
  • Notepad++ is a solid editor which served me well for several years, but it just isn't nice to look at. It is powerful, but does not have a bustling community making extensions for it. It can also be a bit sluggish to use.

The Saviour

Sublime Text is amazing! You only have to watch the demonstrations on their homepage to see why. Unlike all the above editors, it is not free, but after the 30 day trial I was sold.

Sublime Text 2

TextMate Compatibility

One of the major drawcards for me (despite not having used TextMate) is that ST2 is 100% compatible with TextMate Bundles. This gives you access to a massive range of add-ons from the super popular Mac editor. Ones that I use are the Pascal and CacheManifest syntax bundles. I also have native packages to create Github Gists, print to HTML and search Stack Overflow to name a few.

Package Control

Packages can be installed using Sublime Package Control. It is a package itself which grants package management right inside Sublime Text. Once installed - you can install, upgrade and uninstall packages using the Command Palette (Ctrl + Shift + P on Windows).

Installing

To install Package Control we are going to run Python code from the console. Copy the following code and paste it into the Sublime Text console, accessed using Ctrl + ` and run it by pressing Enter.

import urllib2,os; pf='Package Control.sublime-package'; ipp=sublime.installed_packages_path(); os.makedirs(ipp) if not os.path.exists(ipp) else None; urllib2.install_opener(urllib2.build_opener(urllib2.ProxyHandler())); open(os.path.join(ipp,pf),'wb').write(urllib2.urlopen('http://sublime.wbond.net/'+pf.replace(' ','%20')).read()); print 'Please restart Sublime Text to finish installation'

There are manual instructions if the console install does not work for you.

Once installed, check out the Usage page to get started. Try typing your favourite language into the Package Control: Install Package search query.

Stay tuned for more Sublime Text tips :)

Discuss...

Update All Hg Repositories Using Powershell

July 5, 2012

Developer Morning Ritual

As part of my morning ritual I need to update all of my source control repositories so that I have the latest code to start the day. For the record, I'm using Mercurial (hg).

For the longest time I was going through each repository using TortoiseHg Workbench and doing a manual hg pull using the GUI. The amount of repositories has gotten to a ridiculous number so I decided I would automate the process.

Yay, Powershell!

Creating Jobs

Given that I am working on Windows I decided to use Powershell, which I feel is very powerful and exactly what we (Windows developers) needed in a command line.

Teh Codez

Cls
Write-Host "Searching for repositories to update..."
Write-Host

$dirs = Get-ChildItem | where {$_.PsIsContainer} | where { Get-ChildItem $_ -filter ".hg" } 

foreach ($dir in $dirs)
{
	Start-Job -Name HgUpdate$dir -ArgumentList @($dir.FullName) -ScriptBlock {
		pushd $args[0]
		hg pull --update
	} | Out-Null
	"Created job for repository: " + $dir
}

Cls
Write-Host "Waiting for jobs to complete..."
Wait-Job HgUpdate* | Out-Null

Cls
Receive-Job HgUpdate*
Write-Host
Write-Host "All jobs completed."
Write-Host "Press any key to continue..."
$host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | Out-Null

There's not a lot going on here:

  1. Get all the directories in the same directory as the script that contain Hg repositories
  2. Start a job to update each repository with a unique name (HgUpdate plus the name of the directory)
  3. Wait for all the jobs to complete
  4. Print the result of all the jobs to the screen
  5. Wait for user input

The cool thing happening in this script is that all the repositories will be pulled asyncronously using the Start-Job command. This way we don't have to wait for one to finish before the next one can start. The result is a morning filled with the latest codez for no effort :)

What About Git?

Using minimal changes you should be able to get this script working with Git. I haven't provided a Git version because I don't need it, and you smart developers should be able to make the changes yourself. Changing the -filter to look for a Git repository and the hg pull --update line to get the latest Git changes should do it. If you do it, let me know!

Discuss...