Saturday, May 5, 2012

Exception handling in Java threads

There is a lot of literature on exception handling in java and we, developers, are often confused on how to deal with exceptions in a thread. There are several best practices when it comes down to exceptions, before going deeply in to the matter of this article let's take a quick look at them:

  1. Checked exceptions: Use checked exceptions only when the client code can perform some recovery action based on the information he received.
  2. Preserve encapsulation: It doesn't make any sense for a client code to receive an IOException if he wasn't directly using a resource, propagating such an exception can be misleading.
  3. Provide relevant information for the client: If you happen to create a custom Exception you should start by asking yourself if this is already implemented or how you could provide an extra piece of information. A simple error code (you may want to use an Enum) describing the nature of the exception is priceless for tracking problems quickly to their root and for metrics as well (check out this talk about the importance of metrics).
  4. Always clean up resources: Deleting temporal files, closing connections and streams and so on. Most likely you are going to perform these tasks in a finally block.
  5. Do not swallow exceptions: A checked exception is telling you that you have to take some action, do not continue the flow of an application as if nothing happened.
  6. Do not catch top level exceptions: Throwable, Exception, Error or RuntimeException. This can lead to some serious misbehaving you are not being aware of what it's really happening, especially if you happen to catch an error coming from the JVM as it could be an OutOfMemoryError.
  7. Never expose top level exceptions: throw new Throwable();, public abstract void doSomething() throws Exception;. This lines are absolutely against the principles described above they will introduce potential bugs in your application which won't be easy to track.
  8. Never use exceptions for control flow: An exception is meant to break the flow of an application, trying to do dirty tricks absolutely invalidates the nature of the exception itself.

Thursday, March 29, 2012

The strategy pattern

In the last article of this series I wrote about the template method pattern and how it is different from the strategy pattern although they try to solve a very similar problem in design. Nevertheless I didn't quite explain the latest in deep, this time though I'm going to solve it with a real example: a currency converter in java.


As we saw in the last article, the strategy pattern has the specific functionality of delegating the responsibility of choosing the implementation of the algorithm to the client. This notion introduces the dependency inversion principle into our code, this principle states:
  1. High-level modules should not depend on low-level modules. Both should depend on abstractions.
  2. Abstractions should not depend upon details. Details should depend upon abstractions.

Sunday, March 11, 2012

Python Magic, Part 1

Hi! I'm Albert, the new contributor of Scodden, I'm posting articles about curiosities (or magic) python does in certain situations that I've found while working with it.

Import and reload magic

I'm starting with a simple 3 lines piece of code in python which can bring some confusion about its output. Let's start creating a new file called
import candy
print "candy"
candy = reload(candy)

Tuesday, March 6, 2012

Best practices: Design patterns

On Friday Alexis wrote an article about best practices on integrating maven in your current project. I would also like to take the time to talk about practices; programming practice. I'm introducing a series of articles about Design Patterns.


When it comes to object oriented programming the core of your application (your most abstract layer) is always about design patterns. I'm making a big step here, I know most developers that normally use design patterns don't think about them as being the core of their applications, but some handy tools to solve certain parts of it. However design patterns in the core of your application will help you be compliant with the SOLID principle. Don't get the wrong idea: you must make concessions as well.

Friday, March 2, 2012

How to integrate Maven in your current project

I am not going to discuss which are the best build tools available out there, this article could be entitled as well How to integrate Gradle on your current project since both tools have overlapping features. If you have already decided to switch to an automated build tool, this article and the following ones are going to help you with this tedious task.

The methodology

First let's talk about the methodology: What do we need to know before drafting our integration road map? To move the mind of old school developers into such a big change and to make them feel comfortable with Maven conventions is not an easy task. I'm starting with some really basic advices and we'll go deep into the subject on future articles:

Thursday, March 1, 2012

SQL like min and max queries for MongoDB

Today I was trying to retrieve the last insertion dates from a set of documents in a Mongo database and I struggled with frustration when I realized that, despite the efforts of the people working on this great product for making their query system very rich and documented, there wasn't a real equivalent for min and max functionality as you'll find on a traditional SQL database.