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 »

The Process Myth

This blog post by Michael Lopp makes me feel much better about process documents because it describes how they are written down culture and values (and how they become mindless jumping through hoops after a few years…).

Maybe a good way of keeping process documents fresh is to go through them at the beginning of each project and decide which bits to keep and which bits to add. This means that the actually mean something to the whole team, not just to the ones who originally wrote them.

The Land that Scrum Forgot

A great piece by Robert C. Martin how software engineers are rewarded for sloppy code in Scrum. He describes how sloppy code initially gives the impression that Scrum gives great and fast results only to bring down the project over time. He suggests a number of ways to measure code quality and to reward developers for them.

I think it’s a great post to start a conversation and to start a thinking process but his belief in the completeness of acceptance tests is a bit hard to swallow for me.

Clean Code

I’ve been meaning to read this book by Robert C. Martin for a few years now and I have finally started. I’m not far into the book yet but it’s starting out fantastic. I’m basically sitting on the train during my morning commute shouting silently “Yes, yes, I’ve experienced all of these problems that you are describing! Now please tell me how to fix them!”

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 »