Saturday, December 21, 2013

Back to native

Usually the end of the year is time to take a step back, reflect on the past and plan for the future. For me it is also a time for a change. After 9 years working in Java (SE) I will switch back to C++. So I'll use the opportunity to share some thoughts I have accumulated in recent years.
Generally I avoid being a language zealot (like some of my colleagues ;). For me it is more important to create simple and beautiful solutions leveraging whatever technology is available.

Some say Java tends to be too ceremonial and I would agree on that.
Nowadays the industry has accepted that checked exceptions are not so useful after all.
Proper resource management with all this exception handling feels very clumsy and is definitely inferior compared to the good old RIIA mechanism available in C++.
Some design mistakes from the yearly day of Java are still present for the sake of compatibility. These are things like decorator pattern overuse in stream handling and all these unnecessary methods in Object most of which you rarely use but constantly pay the price for.

It was interesting for me to see how C++ has advanced for the last decade. I see there are many new and useful features C++11. For example lambdas are now part of the standard while in Java they have been delayed for years and are still not present.
The preprocessor and the necessity to use it has always been for me one of the major issues in C++. So I was hoping that this was addressed somehow in the recent evolution of the language. Alas! All these #include and #define directives and the involved text substitution without any awareness of the language feel like a stone age hack. And it is still there.
Even before I started working in Java I thought it would be great if C++ could be compiled to some standard intermediate format. This could improve both compile-time and runtime performance. When you compile to machine instructions ahead-of-time (AOT) you rarely know the exact CPU where the code will run, so you cannot take advantage of new CPU features. I was glad to find recently that there is already a considerable effort in that direction - namely LLVM project even though it is not C/C++ specific.

Wednesday, May 22, 2013

Java Serialization Performance

The Java program I work on in my day job maintains its data model in a tree-like structure of Java beans. The data volume is not big but still it is significant - about 500K property values in about 30K bean objects.
To ensure our program can restart from point of failure we often have to save this data model to disk. We use custom XML serialization. As this takes quite some time (several seconds) I started researching different alternatives.

It turned out that JAXB is not appropriate for the job as it does not cope well with cyclic dependencies and cross-references. It also requires adding a lot of annotations and our data model employs about 50 classes.

Next I tried standard Java object serialization but it turned out to preform even worse - it saved the data model in 15s in a 10MB file.

A quick search about Java serialization showed an open source library Kryo. I was able to test it very easily as it requires no annotations and almost no code changes. Kryo turned out to be lightning fast! It saved the same data model in just 400ms in a 5MB file. At first I did not believe all our data was saved, so I loaded it back and compared it to the original data but there were no differences.
One of the main reasons for this performance is that instead of reflection Kryo uses dynamic byte code generation via ReflectASM.
I also found another project run by Nate - Spine to be very interesting. It is related to game development. Obviously a very skilled developer.
Kudos Nate, great job!

Wednesday, May 15, 2013

Batch convert MOV to MP4 in Ubuntu

for i in *.MOV; do avconv -i $i -b:v 3000k ${i%.MOV}.MP4; done

Video: mjpeg -> libx264
Audio: pcm_s16be -> libvo_aacenc
Compression: 10 -> 1

These are video clips from my Panasonic photo camera

Wednesday, May 8, 2013

Saturday, February 23, 2013

Function or method

Lately I have been reading about Python. Here is an example that shows some interesting properties of functions and methods.
Given this code in methods.py

def f(a):
    print a
 
class C:
    def __init__(self, x):
        self.x = x
 
    def __str__(self):
        return "C(%d)" % self.x


Let's try some statements in the Python console

>>> from methods import *
>>> f("Hello")
Hello
>>> c = C(5)
>>> print c
C(5)
>>> c.f = f
>>> c.f("bye")
bye
>>> C.g = f
>>> c.g()
C(5)
>>> g = c.g
>>> g()
C(5)



Friday, October 26, 2012

Beware of Premature Generalization

When people start writing some new code they often feel so enthusiastic that they try to make it reusable right from the beginning. Or some do it out of some pretended sophistication. The end result is usually layers of unnecessary abstractions that are actually not used anywhere else. In the end such code takes more effort to write and more effort to maintain afterwards.
The reality is that you cannot make something reusable if it is used in a single place/scenario. To make it reusable you do need at least two real use cases, so you can abstract the common traits. The more use cases you have, the better you can refine the abstraction.
You may think you know what new use cases will come, but in reality these either don't come at all or are quite different from what you have expected.
So don't do this, unless you can tell the future.

I thought premature generalization would be a good term for such situations. Then after searching the net it turned out this term is well established already - Premature Generalization Is Evil. It is even listed as #1 in Seven deadly sins of programming.

Thursday, August 30, 2012

Remove class inheritance from Java

Introduction

We often hear the advice that we should prefer composition over inheritance. Then naturally come the questions: Are there valid cases at all to use inheritance? Is it possible that inheritance, one of the fundamental OOP principles, is actually an anti-pattern and unnecessary language feature?
By inheritance here I mean only class inheritance and not interface extension.

Goodbye Class Inheritance

The most common reason for class inheritance found in practice is code reuse. When people realize they need some common code in two or more classes, they pull it up in a common base class (usually an abstract one). But what would you do if you need to reuse code from several other classes? There is no multiple inheritance in Java. The right way to reuse some common code is via composition, i.e. extract the common code in a separate class and use it via a reference.

The next reason for inheritance is method overriding, i.e. template method pattern. When people realize they need some variability in behavior, they define a method for it (often abstract) and implement it in different ways in different subclasses. This is also wrong for several reasons. You cannot change the variable method implementation dynamically without changing the whole object. Also if you have several axes of variability, i.e. several abstract methods, how many subclasses do you need to cover all possible combinations? Again the right way to do that is via composition. Each independently variable part is extracted in a separate interface. Different implementations of these interfaces are injected from outisde, i.e. strategy pattern.

Can you suggest cases where it is still best to use class inheritance? Post a comment.

Generally, inheritance violates other OOP principles like encapsulation as it creates tight coupling between involved classes.

The conclusion is that the availability of this feature (implementation inheritance) leads people into wrong designs. So the Java language can be both improved and simplified if classes are not allowed to extend other classes. Still an interface can extend other interfaces and a class can implement a number of interfaces.
Removing this feature can simplify the language significantly. Additional language features can also be removed as they are no longer necessary.
  • abstract keyword  is no longer necessary and can be removed.  Abstract coding can get you only abstract money. :)
  • protected keyword  is no longer necessary and can be removed
  • final keyword on classes and methods  is no longer necessary
  • (Probably this list can be extended further. Drop your suggestions in the comments.)
So this is a nice example how taking away can improve the result, i.e. less is more principle. Or as Antoine de Saint-Exupery has put it

"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away."

Welcome Auto-binding

Still when your class implements an interface you often do not write the code from scratch but want to reuse some existing implementation. As we saw already the proper way to do that is via composition. Something like this

class A implements I
{
    private I anI;
    ...
    public void foo() { anI.foo(); }
    public void moo() { anI.moo(); }
    ...
}

Unfortunately we have no way to tell that anI provides the implementation of interface I, so we have to implement all the methods of I in A just by delegating to anI. Boring boilerplate! This is where the second proposal for the language improvement kicks in. You can call it auto-wiring or auto-binding but it does just that, specify that an interface is implemented by a specific class member. Something like this:

class A implements I via anI
{
    private I anI;
    ...
}

The compiler can automatically generate the necessary delegation methods or an optimized implementation could even skip this overhead.

If a class needs to customize the interface implementation it can still implement some of the interface methods, thus overriding these methods from the external implementation.

Instead of a field also a method could be specified in case the interface implementation should be calculated dynamically. Something like this:

class A implements I via getI
{
    private I getI() { ... }
}

I find these two proposals for Java language improvement nicely complement each other and naturally lead developers in the right direction.
Until these improvements are implemented in Java (version 15 probably :) you can try to avoid class inheritance in your code.