Learning functional Javascript through Koans
Filed under: HOWTO, Javascript, Software Craftmanship
Given how Javascript interpreters are improving in leaps and bounds; and that it seems to be the only language that will be supported by all web devices; its time to for me to revive my Javascript skilz. Fortunately the tooling has improved greatly; Eclipse 3.6 for Web Developers and JsTestDriver bring a refactoring and a unit test runner to Javascript development.
Interestingly, Javascript seems to have more functional than object orientated characteristics; so it seems reasonable to learn it wearing my functional hat.
I’ve been enjoying learning Ruby syntax via RubyKoans – little tests that teach you syntax and convention as you make them pass
I though I’d create a similar set of Functional Javascript Koans to help refresh my Javascript skills.
Its a bit rough; so please fork and contibute back improvements.
http://github.com/mrdavidlaing/functional-koans
CI for Flex 4; running FlexUnit4 unit tests and FlexMonkey4 acceptance tests with Maven and FlexMojos
The FlexMojos project is a great way to bring your Flex application under a grown up continuous build system like Maven.
Getting FlexMojos 3.6.1 working with Flex 4, running Flash Builder 4’s version of unit tests and FlexMonkey4’s version of acceptance/UI tests is pretty tricky.
Hopefully this sample project – http://github.com/mrdavidlaing/flexmojos-sample along with this screencast will save someone else the pain I went through to get all these playing together.
Howto add new component to FlexITP from David Laing on Vimeo.
Patches welcome – just clone the git repo make your change, and request a pull.
HOWTO Reset MySQL 5.0 root password in Ubuntu 8.04 LTS
Turns out there are lots of complicated ways, but in Ubuntu you can just reconfigure the package:
dpkg-reconfigure mysql-server-5.0
Hopefully that will save someone some hair pulling
Utils anti-pattern & AutoMapper
These are more for my reference purposes – hopefully you’ll find them useful:
If you’re about to name a class **Util; think harder – there is a better name that discribes what that class is for:
Chriss Missal has some advice for you
Faced with the prospect of heaps of left hand side => right hand side code in your DTO of anti-corruption layer? Consider Jimmy Bogard’s Automapper
Try/Catch for SQL!?
Thanks to Nick Sertis for this trick – who knew TSQL could do try/catch statements!
Very useful when you need to write data manipulation scripts for production databases.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | BEGIN TRY
BEGIN TRAN
--Some SQL
COMMIT TRAN
END TRY
-- Catch the errors on the inserts
BEGIN CATCH
ROLLBACK TRAN
SELECT ERROR_MESSAGE()
END CATCH |
Functions with side effects are just rude!
Today I fell into a trap when using a function that had a side effect – it unexpectedly changed an input parameter; causing a later statement to fail. Debugging took an age!
For example, consider the following function:
1 | string StringReplace(string haystack, string needle) |
If this function is side-effect free, we can use it without fear like this:
1 2 3 4 5 6 | string menagerie = "cat,dog,bee,llama"; string catFreeMenagerie = StringReplace(menagerie, "cat"); string beeFreeMengerie = StringReplace(menagerie, "eric"); Assert.AreEqual(",dog,fish,llama", catFreeMenagerie); Assert.AreEqual("cat,dog,,llama", beeFreeMengerie); |
However, if StringReplace() had the side effect of also changing the passed in haystack, then the second Assert would fail, because the first StringReplace has the unexpected side effect of changing one of its arguments.
Evans in the DDD book has quite a bit to say about this; arguing that having side effect free functions goes a long way towards making a supple design
Side effect free functions also make testing & refactoring easier (less state to worry about etc)
Remember, a function that changes its parameters is rude, and should not be trusted!
PS: Eric the half a bee lyrics
Selenium gotcha – selenium.GetHtmlSource() returns processed HTML
Whilst writing some Selenium based acceptance tests today; I bumped into a hair pulling gotcha. Hopefully this post will prevent you from the same pain.
The test was to check whether some tracking tag javascript was being inserted into the page correctly or not.
I assumed that I could get the page source as it was being delivered to the browser by calling selenium.GetHtmlSource(); and then check that for the javascript string I was expected.
Unfortunately, GetHtmlSource is just a proxy for the browsers DOM.InnerHTML method; and that returns the Html after it has been preprocessed by the browser.
Turns out that preprocessing does a couple of funky things, including
- Changing line-endings (Firefox)
- Changing capitalization (IE6)
- Seemingly random removal / insertion of ” & ‘ (IE6)
So, when I was expecting a string like this:
1 2 3 4 5 6 | <script language="javascript" type="text/javascript"> <!-- var amPid = '206''; var amPPid = '4803'; if (document.location.protocol=='https:') ...[snip]... |
IE6 was presenting me with:
1 2 3 4 5 6 | <SCRIPT language=javascript type=text/javascript> <!-- var amPid = '206''; var amPPid = '4803'; if (document.location.protocol=='https:') ...[snip]... |
A possible solution is to ignore case, whitespace and quotes when doing the comparison, with a helper method like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 | /// <summary> /// Use this to compare strings to those returned from selenium.GetHtmlSource for an Internet Explore instance /// (IE6 seems to change case and inclusion of quotes, especially for Javascript.?) /// </summary> /// <param name="expected"></param> /// <param name="actual"></param> private static void AssertStringContainsIgnoreCaseWhiteSpaceAndQuotes(string expected, string actual) { string expectedClean = Regex.Replace(expected, @"\s", "").ToLower().Replace("\"","").Replace("'",""); string actualClean = Regex.Replace(actual, @"\s", "").ToLower().Replace("\"", "").Replace("'", ""); StringAssert.Contains(expectedClean,actualClean, string.Format("Expected string \n\n{0} \n\nis not contained within \n\n{1}", expected, actual)); } |
It was the line endings that really floored me; because they were automatically normalized/corrected by my test runner when displaying the error. Aaargh!
Apache2 on Ubuntu 8.04LTS; restrict access to PAM authenticated users
I have a couple of static pages that I want to restrict access to.
I don’t want to manage another set of usernames & passwds, so I’d like apache2 to authenticate off the standard users on my system, via PAM.
To get this to work, you need to install and configure mod_auth_pam and mod_auth_shadow
aptitude install libapache2-mod-auth-pam libapache2-mod-auth-shadow
Ensure the www-data user is part of the shadow group, so apache2 can read the passwords
usermod -G shadow www-data
And set up the relevent virtual host:
AuthPAM_Enabled On AuthShadow on AuthPAM_FallThrough Off AuthBasicAuthoritative Off AuthType Basic AuthName "Restricted to group: sysadmins" AuthUserFile /dev/null Require group sysadmins
Restart apache, and you’re done!
Self Cert SSL certificate for Apache2 on Ubuntu 8.04LTS
Generate a self cert certificate:
https://help.ubuntu.com/8.04/serverguide/C/certificates-and-security.html
Create a new virtual host (you can only have one SSL virtual host / IP)
sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/ssl
Edit ssl sothat it looks like this:
NameVirtualHost *:443
ServerName webangle-www1.everyangle.co.uk
ServerAdmin webmaster@localhost
DocumentRoot /var/www/
SSLEngine on
SSLOptions +StrictRequire
SSLCertificateFile /etc/ssl/certs/server.crt
SSLCertificateKeyFile /etc/ssl/private/server.key
Finally, if you want to force redirect of all traffic to a certain folder via SSL (e.g, /phpmyadmin), add the following to /etc/apache2/sites-available/default
#Redirect traffic to /phpmyadmin through https
RewriteEngine on
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^/phpmyadmin(.*)$ https://%{SERVER_NAME}/phpmyadmin$1 [L,R]
Enable it:
sudo a2ensite ssl sudo /etc/init.d/apache2 reload
Automount remote filesystem over SSH
Previously I posted on how I backup my server’s data to rsync.net’s remote storage.
A convienient way to access that remote storage is to configure rsync over sshfs:
sudo aptitude install sshfs mkdir /mnt/sshfs mkdir /mnt/sshfs/rsync.net sshfs **username**@ch-s011.rsync.net: /mnt/rsync.net
First, unmount
fusermount -u /mnt/rsync.net
Then, install autofs, and edit the config file
sudo aptitude install autofs sudo vi /etc/auto.master
Add the following line
/mnt/sshfs /etc/auto.sshfs --timeout=30,--ghost
Then,
sudo vi /etc/auto.sshfs
Add
rsync.net -fstype=fuse,rw,nodev,nonempty,noatime,allow_other,max_read=65536 :sshfs\#**username**@ch-s011.rsync.net\:
And finally restart autofs
sudo /etc/init.d/autofs restart
Now, when you cd /mnt/sshfs/rsync.net, after a short delay you will automatically be connected to the remote filesystem over SSH. After 30 seconds of inactivity, the connection will be closed.


