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;
}
}