Archives for category: programming

What began as a small peek at JDK source code quickly showed me a whole new side of Java.

java.util.concurrency.atomic.LongAdder is a new class that will be introduced with Java 8. It provides an atomic way to add up a large number of values. According to the documentation, LongAdder is faster than AtomicLong. I was curious how that could be and had a look at the implementation.


I found that yes, LongAdder is faster (see graph on the right). And I also learned a bit about how atomics are implemented in Java. It turned out to be more complex and more interesting than I expected.

Read the rest of this entry »

This year I submitted a piece to the JS1K competition: “You Are Here“. JS1K is a competition for writing JavaScript demos that are no larger than 1024 bytes. This blog post describes some of the tricks I learned along the way:

  • Writing small code
  • Compressing data
  • Using Closure Compiler efficiently to minify the code
  • Using JSCrush to compress the code

Read the rest of this entry »

This is part 3 of a series on patterns for writing safe constructors (and destructors) in C# and Java. Start reading at part 1: Exceptions in Constructors.

As seen in part 2, using the Open / Close pattern decouples object creation and resource allocation safely. However, for this solution to work we need to rely on the user of the object to stick to the correct sequence of actions, or we need to explicitly enforce the correct sequence with invariants.

Static factory methods reduce the potential problems we introduced in part 2. This is achieved by combining object creation and calling the Open method. Many of the problems listed at the end of part 2 simply disappear.

Read the rest of this entry »

This is part 2 of a series on patterns for writing safe constructors (and destructors) in C# and Java.

As seen in Part 1, we can have a problem if a constructor throws an exception after allocating a resource. There is no trivial way of releasing the resource after the constructor bails out. Part 2 discusses the most common pattern to avoid this situation: moving the allocation of resources into an Open() method.

Read the rest of this entry »

What happens if a constructor throws an exception? Is a new object still created? Do we even need to care?

Yes, a new object is created. It won’t be initialized properly and we usually won’t have a reference to it. And yes, we do need to care. That half initialized object may block important resources.

The examples in this series are in C#, but everything can be applied to Java and other similar object oriented languages.

Read the rest of this entry »

Space Invaders screenshot

In this blog post, I deconstruct a little Space Invaders inspired game I wrote to get to know the physics engine box2dweb. The code builds on the wrapper classes from the excellent Making Games With Box2dWeb article. I strongly recommend reading this article first to understand the basic concepts that I will be using. The topics discussed in this blog post will be

Play the Space Invader game.
Get the source code.
Read the rest of this entry »

The using statement is short and concise but does get in the way writing clean code. Do not use it.

I found that my colleagues and I were using the C# using statement as a replacement for try/catch blocks because we assumed it automatically and magically handled exceptions safely. Of course whenever you assume magic in programming, you don’t know what you’re doing and you inadvertently do it wrong.

In actuality, the using statement does no such thing as handling exceptions. It can even hide exceptions from you!

Read the rest of this entry »