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.

LongadderQuad

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 »

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 »

I hacked together an Eclipse @Autowired plugin for a 24 hour innovation day challenge at work. I had not written an Eclipse plugin before and it turned out to be a more complex task than I imagined.

What I like about IDEs is that I can navigate quickly between references, declarations, definitions, superclasses, and subclasses. Pretty much all code relationships that are known at compile time can be navigated. Dependency injection, which I use all the time when I program something in Spring, establishes relationships at runtime and IDEs (or at least Eclipse, which is what I mainly use) don’t know anything about them. There seem to be Eclipse plugins that visualise these relationships but what I want is to right-click on a field and ask Eclipse to take me to the definitions of the bean that’s autowired into it (yes, we mainly use the @Autowired annotation at work). What I don’t want is to open a separate view or even a graph showing bean relationships. I want to see them in the code just like normal code relationships.

Read the rest of this entry »