Sunday, August 29, 2010

Test Drive Hello World

Earlier this week during a discussion on Test Driven Development TDD I commented to an associate that "once the TDD mindset takes a hold of you, you will find yourself writing the test before you write a single line of code even for something as trivial as hello world." My friend paused and asked how and I responded, well I would write a test to look at Standard Output and assert that it contained 'hello world'. The test will fail, then I would write to the Standard Output and watch the test failed. We moved on to other topics but in the back of my head a voice said 'why not try it out'

Here is chronology of that trial:
I first wrote the following unit test to capture Standard Output

[TestMethod]
public void TestSayHello()
{
string logFile = "test_log.txt";
string result;
using (StreamWriter writer = new StreamWriter(logFile))
{
Console.SetOut(writer);
SayHello();
}

using (StreamReader reader = new StreamReader(logFile))
{
result = reader.ReadLine();
}

Assert.AreEqual("Hello World", result);

}

Next I wrot the following method to get my unit test to pass

private void SayHello()
{
Console.WriteLine("Hello World");
}

I next DRYed the unit test by introducing the following helper method

private string CaptureStandardOutput(Action perfromAction)
{
string logFile = "test_log.txt";
string result;
using (StreamWriter writer = new StreamWriter(logFile))
{
Console.SetOut(writer);
perfromAction();
}

using (StreamReader reader = new StreamReader(logFile))
{
result = reader.ReadToEnd();
}
return result;
}

Which in turn cleaned up the unit test as follows

[TestMethod]
public void TestSayHello()
{
string result = CaptureStandardOutput(SayHello);
Assert.AreEqual("Hello World\r\n", result);
}

Sunday, August 8, 2010

Land The Tech Job You Love by Andy Lester

Land The Tech Job You Love by Andy Lester is a must read for everyone. Even though the title suggest that it is targeted for the techies, this book contains valuable insight and advice for the non-technical among us as well.

Andy walks the user from preliminary steps such as determining what you want in your next job to the offer letter and beyond. Early on in the book Andy gets the reader to reflect on vital questions such as what do I want in the a job and what are the factors which are key
motivators in this stage of my life.

Next, he provides excellent advice on the content and structure of your resume. He covers the all important question of what format should the resume follow and gives key pointers on what should and should not be included in this all important document.

The importance of personal network in finding a job is covered next and he provides interesting statistics regarding the effectiveness of various mediums such as job boards, company websites, head hunters and referrals. Next, he covers applying for the jobs and how you should tailor
your resume and your cover letter for that particular job.

Important advice for the preparation and execution of the job interview is covered next. He devotes an entire chapter on handling tough interview questions and another on which questions to avoid during the interview. Next, he gives advice on how to handle things after the
interview such as thank you notes, followup inquiries, references, and the all important job offer.

The final chapter gives great advice on keeping yourself marketable. This has prompted me to resume blogging. He echos many of the sentiments found in some of the other books in the pragmatic bookshelf series such as, improve yourself, improve your network, and improve your brand.

Wednesday, September 30, 2009

Notepad++ support for Scala

Scala comes with a syntax definition file for Notepad++ which can be found at scala_root\misc\scala-tool-support\notepad-plus

Since I use Notepad++ (portable edition) I copied the userDefinedLang.xml file to \PortableApps\Notepad++Portable\App\Notepad++ directory and restarted Notepad++

Thursday, September 17, 2009

CAPTCHA

For some time I have been filling out CAPTCHA images on various sites. At an abstract level I knew the image challenge was to prevent non-human users from gaining access to the site. However, I did not know that the term applied is called CAPTCHA until this morning when I came across this announcement that Google has acquired reCAPTCHA. What is interesting still is that each time a human answers a reCAPTCHA powered challenge successfully it enables the company to digitize part of a scanned text. For example, reCAPTCHA may scan an old edition of the New York Times and the OCR will likely, recognize and, convert to text a large percentage of the document. Those pieces which are not recognized are then turned to graphics and presented to human biengs for translation. So how does the computer know that the human has answered the question correctly? Well the challenge is composed of two parts known and unknown. If the human answers the known element correctly it is assumed that the unknown is correct as well. The assumed correct answer to the unkown is then validated against a larger sample to ensure its accuracy.

Thursday, September 10, 2009

Scripting languages

I came across this article which does a pretty job at comparing Perl Python Ruby and Groovy.

Monday, September 7, 2009

Syntax formatting and highlighting in Blogger

It took two hours of hacking and trying various suggestions from blogs but I finally got code snippets to display properly. Here are the steps

1. Edit the html template for blogger by referencing SyntaxHighlighter code from googlecode by placing the following just below the opening head tag.

<!-- Add-in CSS for syntax highlighting -->
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shCore.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushCpp.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushCSharp.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushCss.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushDelphi.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushJava.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushJScript.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushPhp.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushPython.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushRuby.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushSql.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushVb.js' type='text/javascript'></script>
<script src='http://syntaxhighlighter.googlecode.com/svn/trunk/Scripts/shBrushXml.js' type='text/javascript'></script>

2. Copy the contents of the style sheet from SyntaxHighlighter and paste just above the closing tag i.e.
]]></b:skin>

3. Place the following function in the head as well.

<script language='javascript'>
window.onload = function() {
dp.SyntaxHighlighter.BloggerMode();
dp.SyntaxHighlighter.HighlightAll(&#39;code&#39;)
}
</script>

Once this initial setup is complete you can place code snippets in the blog by simply placing the following:

<pre name="code class="xml">

</pre>

You can use this to format the html for the snippets.

NullPointerException thrown when comparing strings

When comparing strings in Java a run-time NullPointerException will be thrown if either string is null.
This can be avoided by performing a comparison between a constant and variable string which could potentially be null.

That is, variable.equals(CONSTANT) will throw a run-time exception if the variable is null. Therefore, a better comparison would be CONSTANT.equals(variable)

The following will throw an exception

public class StringCompare {
final static String ELEMENT = "constant";
public static void main( String[] args ) {
String element = null;
element = getElementValue();
if (element.equals(ELEMENT)) {
System.out.println("element " + element + " matches ELEMENT " + ELEMENT);
}
}

private static String getElementValue() {
return null;
}
}

Whereas this will not throw an exception

public class StringCompare {
final static String ELEMENT = "constant";
public static void main( String[] args ) {
String element = null;
element = getElementValue();
if (ELEMENT.equals(element)) {
System.out.println("element " + element + " matches ELEMENT " + ELEMENT);
}
}

private static String getElementValue() {
return null;
}
}