3/23/08

Ambush Interviews

OK you're going in for an interview. You show up, and find yourself facing a bunch of smarmy self-impressed dipshits asking you a bunch of weird questions about Java, stuff you've never really thought much about. First thing to remember is that they've had number of heated debates about this kind of stuff, so to them it's all really, really important. And you damned well better think it's important too, or else.

For example, one of the questions I've been smacked with has to do with the great checked v. unchecked exception debate. Like you knew there was one, right? It's been going on pretty much since Java was first released, and shows no sign of abating.

More than likely, this will be in a shop that's using Spring bundled with Hibernate. You'll probably also find that these folks really like to talk about dependency injection and inversion of control, but that's a whole nuther issue.

The key is that the Spring framework (and to a lesser extent Hibernate) generally catches checked exceptions and rethrows 'em as unchecked. Folks who think this is the ultimate coolness would be considered in the Eckel camp, which generally thinks checked exceptions suck.

There's another camp, sometimes associated with Joshua Bloch or Rod Johnson (as in the guy who invented Spring), which gets a lot less worked up about the matter, and seems to understand that things are as they are, and we all should be more focussed on writing clean and functional code.

Long and short? Know about this great debate, and be prepared to discuss it. Read through this IBM article and this one as well. Have a little fun and make up a few questions to toss back. Hell, they'll talk about it the whole interview, if you let 'em.

If you really want to floozle 'em, segue into an API design discussion with questions based on this article. They'll think you're a veritable genius.

If nothing else, you'll have showed 'em you can be every bit as dogmatic as they are. Not that it actually lends itself to writing good code, mind you.

When all is said and done, you may actually get an offer. Then, the big question becomes do you really want to work with people like this?

1/5/08

New Improved!!

Odi fetches me up about exception handling.

Instead of this:

finally
{
try
{
if (fos != null)
{
fos.close();
}

if (osw != null)
{
osw.close();
}

if (theFile != null)
{
theFile.close();
}
}
catch (IOException ignored)
{
log4jLogger.error(ignored);
}
}
Do this:
finally
{
close(fos);
close(osw);
close(theFile);
}

private void close(Closeable closeMe)
{
if (closeMe != null)
{
try
{
closeMe.close();
}
catch(IOException ignore)
{
log4jLogger.error(ignore);
}
}
}
That way, if an exception is thrown early on, your code will continue trying to close the other resources.

1/4/08

Goodbye ASCII; Hello Unicode - Part Two

Want to write Unicode to a file? Say no more:

public void write(String stuff, String toFilename) 
throws IOException
{
FileOutputStream fos = null;
OutputStreamWriter osw = null;
BufferedWriter theFile = null;

try
{
fos = new FileOutputStream(toFilename);
osw = new OutputStreamWriter(fos, "utf-8");
theFile = new BufferedWriter(osw);
theFile.write(stuff);
theFile.flush();
}
finally
{
try
{
if (fos != null)
{
fos.close();
}

if (osw != null)
{
osw.close();
}

if (theFile != null)
{
theFile.close();
}
}
catch (IOException ignored)
{
log4jLogger.error(ignored);
}
}
}

Goodbye ASCII; Hello Unicode

Sometime, when you actually have to write internationalized software, you may run into difficulties with those damned ResourceBundles, and their bastard progeny, the PropertyResourceBundle.

According to the documentation, "(c)onstructing a PropertyResourceBundle instance from an InputStream requires that the input stream be encoded in ISO-8859-1."

If you end up with non-ISO-8859-1 characters in your bundle (e.g., SimplifiedChinese and Japanese), all is not lost. The little trick I stole from this guy could save you an awful lot of grief:

public String getProperty(String key)
{
ResourceBundle bundle = getResourceBundle();
String value = bundle.get(key);
byte[] bytes = value.getBytes("ISO-8859-1");
return new String(bytes, "utf-8");
}

Comparing Two Xml Files


package com.blogspot.abbablows.tests;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import junit.framework.TestCase;

/**
* JUnit test shows one method of comparing two xml files
*/

public class Example extends TestCase
{
protected void compareFiles(File actual,
File expected,
String elementName)
throws ParserConfigurationException, SAXException,
IOException
{
FileInputStream fileOne = null;
FileInputStream fileTwo = null;

try
{
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();

DocumentBuilder builder = factory.newDocumentBuilder();

fileOne = new FileInputStream(actual);
Document firstDocument = builder.parse(fileOne);
firstDocument.getDocumentElement().normalize();
NodeList firstRoot =
firstDocument.getElementsByTagName(elementName);

fileTwo = new FileInputStream(expected);
Document secondDocument = builder.parse(fileTwo);
secondDocument.getDocumentElement().normalize();
NodeList secondRoot =
secondDocument.getElementsByTagName(elementName);
walkNodes(firstRoot, secondRoot);
}
finally
{
if (fileOne != null)
{
fileOne.close();
}

if (fileTwo != null)
{
fileTwo.close();
}
}
}

private void walkNodes(NodeList firstRoot,
NodeList secondRoot)
{
assertEquals(firstRoot.getLength(), secondRoot.getLength());

for (int i = 0; i < firstRoot.getLength(); i++)
{
Node itemOne = firstRoot.item(i);
String nameOne = itemOne.getNodeName();
String valueOne = itemOne.getNodeValue();

Node itemTwo = secondRoot.item(i);
String nameTwo = itemTwo.getNodeName();
String valueTwo = itemTwo.getNodeValue();

if (itemOne.hasChildNodes())
{
walkNodes(itemOne.getChildNodes(),
itemTwo.getChildNodes());
}

if (itemOne.hasAttributes())
{
doAttributes(itemOne, itemTwo);
}

assertEquals(nameOne, nameTwo);

if (valueOne != null && valueTwo != null)
{
assertEquals(valueOne.trim(), valueTwo.trim());
}
}
}

private void doAttributes(Node firstRoot, Node secondRoot)
{
NamedNodeMap mapOne = firstRoot.getAttributes();
NamedNodeMap mapTwo = secondRoot.getAttributes();

assertEquals(mapOne.getLength(), mapTwo.getLength());

for (int i = 0; i < mapOne.getLength(); i++)
{
Node itemOne = mapOne.item(i);
String nameOne = itemOne.getNodeName();
String valueOne = itemOne.getNodeValue();

Node itemTwo = mapTwo.item(i);
String nameTwo = itemTwo.getNodeName();
String valueTwo = itemTwo.getNodeValue();

assertEquals(nameOne, nameTwo);

if (valueOne != null && valueTwo != null)
{
assertEquals(valueOne.trim(), valueTwo.trim());
}
}
}
}

It Begins