2008-03-24

Premature optimization is not THAT root of evil

Sometimes I can see really ugly code from my colleagues. Something like this:

ArrayList checkList = new ArrayList();
for (Item item : allItems) {
    if (item.isModified) {
        checkList.add(item);
    }
}
for (Item item : itemsList) {
    if (checkList.contains(item)) {
        doSomething(item);
    }
}


Yes, there are situations, where you need hacks like this. But why, why are they using ArrayList to perform contains checks? HashSet is created for this job, why not to use it? For 10 items you will not see speed problems. For 100 items speed will lose few millis. For 10000 item you will count seconds!
No! I will not do it like you want! Premature optimization is root of evil!

Do you see premature optimization here? For me it is just making use of proper tool, tool that created for this task. Don't you think so?

2008-03-23

Less than Hello World

Once I started to learn Assembler. I had a book of one russian hacker. First chapter of this book started like this:

Every book starts with "Hello, World" program. But reader can see programs, that can do more, than just pring "Hello, World". So he might think, that there is programs, that can do less, than "Hello, World".

For now, Assembler is dead, and we are using Java, as perfect technology.
So, less than "Hello, World!":

package helloworld;

public class LessThanHello {

    public static void main(String[] args) {
    }

}