Using Exceptions to Escape from Third-Party Libraries

While this point is not strictly speaking related to zen-level (as it is not about preventing programming errors) it remains a useful tool in the software designer’s toolbox: Unchecked typed exceptions found in Java and many other languages can be used as a very powerful break statement. Common wisdom says that using exceptions for control […]

Enforcing State Transitions

To access some service we are using a third-party library with an interface that looks like this: [java]public class Session { public void connect(); public Answer ask(Question question); public void disconnect(); }[/java] The contract specified in the documentation specifies that: The first method invoked on the object may only be connect. The connect method may […]

Maps with Variable Value Types

About Maps and Generics Since Java introduced Generics, Maps became a bit safer to use. Before generics, this code would pass compilation without issues: [java]Map fruitColours = new HashMap(); fruitColours.put(Fruit.banana, “blue”); /* In a method somewhere else */ if (fruitColour.get(“banana”) == { throw new IllegalStateException(“Alien blue bananas not supported”); }[/java]