Learning a new programming language historically starts off with the classic “Hello World” example. I've probably written a variation on that a bazillion times. From there you take more steps into advanced parts of the language. If you are smart, you save your example programs to refer back to them later. But there's a better way. And that better way is to write test cases that exercise the capabilities of the language you are trying to learn. This also works for new libraries within a language you are trying to learn.
The advantages of writing test cases are plentiful. It gets you writing
actual code which helps you remember, they can be usually be run easily,
they serve as an extra source of documentation about what you are trying
to do (e.g. “How do I use gsub
again? Oh yeah, it's in that string
test I wrote?”) and finally writing tests is fun.
An Example
After ignoring it for way too long, I recently started trying out the Yahoo! User Interface Library. It's a JavaScript library with scads of useful utilities for buidling web applications. So, I pulled up the documentation and started writing a tests based on what's documented. Since it's JavaScript, I used JsUnit since I'm used to JUnit on the Java side of things. And voila we have our first test:
<html>
<head>
<title>Test Yahoo</title>
<script type="text/javascript"
src="jsUnitCore.js"></script>
<script type="text/javascript"
src="yahoo.js"></script>
<script type="text/javascript">
<!--
function testIsArray() {
assertTrue(YAHOO.lang.isArray([1,2]));
}
//-->
</script>
</head>
<body>
A test document.
</body>
</html>
(I know it's very simple but I just wanted to get a feel for the first method in the global library and type it so my brain remembered it.)
So, now I drop the test file into my test runner and here we have the results:
Can't you just feel your brain expanding with the possibilities?