Bent Andre Solheim Blog

Thursday, November 23, 2006

Note to self: How to import a certificate in Java

Every time I need to import a certificate in Java to get access to some https resource I have to google for the command. Now I don't have to do that any more:

%JAVA_HOME%\bin\keytool -keystore %JAVA_HOME%/jre/lib/security/cacerts -import -file c:/server.cert

Enter keystore password: changeit

Thursday, November 16, 2006

The Apple iPhone Brand Mistake

There have been rumors about Apple working on a cellphone for years. To my knowledge Apple has never confirmed these rumors, but recently Hon Hai Precision Industry in Taiwan has confirmed that they have a contract with Apple to produce 12 million phones with integrated mp3 player. I have read several places that people consider this a confirmation from Apple that they are producing the iPhone.

I could not disagree more, and I really hope that I am right. Ever since cellphones came with calendars, cameras, mp3 players, email, Java and what not, people are using their "phones" for more and more tasks unrelated to making phone calls. In my opinion, a regular cell phone to day is not so much a telephone as it is a multimedia device or a personal planner. There are really no reasons for this ubiquitous gadget to be called a phone or anything similar.

My point is as follows; The iPod is a known brand loved through out the world. The devices Nokia, Sony Ericsson, Motorola and company call phones are in my opinion closer to the iPod in functionality than to a telephone. It would be a huge mistake by Apple to brand their communication capable device anything other than an iPod. They are in the position to rename, or rebrand, a device owned by most people beyond the age of 12 in a large portion of the world. Imagine the power; nobody calls their phones phones anymore. They are all pods, or something similar. And Apple will pretty much own this new name. Calling their new device iPhone, Apple will lose this opportunity.

I realize I am not very good at coming up with good product names. My suggestions below are pretty pathetic. But I know a bad name when I hear one. At least I think I do... ;)

* iPod Talk
* iPod Connected
* iPod Communicator

Saturday, November 11, 2006

Embedding a Wiki

On one of the projects I'm currently involved with, we are building a web based Java application with a fairly complex problem domain. In addition to general user documentation, the customer also want the ability maintain their own documentation about the domain, and link to documents they already have in their document archives. To stay within budget, we could not spend much time implementing the feature.

It immediately struck me that embedding a Java based wiki would solve the problem well. Wikis are tailored for this particular need, and I knew that there are several existing free Java based wikis available. The required features of the wiki we needed was that it supported attachments, stored data on the file system and was easy to set up. As I have used XWiki in the past, I reviewed that implementation first. Although XWiki is highly configurable and flexible, it is too complex for the simple requirements we have. I then looked at JspWiki. JspWiki is not as feature rich as XWiki, and is simpler to set up, but I had trouble getting file attachments work out of the box, and the security set up was just too much.

I was about to Google for another wiki implementation when I was made aware of Wiki Matrix. Wiki Matrix is a web site that lets you compare wikis and help you find a wiki based on your requirements. Using the wizard, I found that VQWiki suited our needs best. Extremely easy to set up. I was up and running in a seconds. It is also feature rich; printer friendly versions of pages, free text search, history ... I can warm heartedly recommend VQWiki if you need to embed an easy to set up, feature rich wiki.

Also, Wiki Matrix is really useful!

Friday, November 10, 2006

Guard Clauses and Composed Methods

As I have told you before, I have worked with several legacy systems so far in my career. Most of these systems have had code that was hard to read and understand. When working with the code to increase readability, I find myself doing the same two refactorings over and over again. In fact, I find myself doing these refactorings pretty much all the time when writing new code aswell.

The first one is Replace Nested Conditionals with Guard Clauses. Nested Conditionals, also called the the Arrow Anti-pattern, is when a method has conditional logic that makes it unclear what the normal path of execution is. Let me give you an example;

Result res;
if (user.privileges.get("admin") != null) {
if (connectionPool != null) {
if (connectionPool.hasAvailableConnections()) {
if (webService != null) {
res = doSomethingWithDbAndWs(
connectionPool.connection(), webService);
} else {
res = null;
}
} else {
res = null;
}
} else {
res = null;
}
} else {
res = null;
}
return res;

Although the essence of this method is not complex, the nested conditionals make it very hard to get a feeling of the logic. Refactoring to use guard clauses makes it much easier to read;

if (user.privileges.get("admin") == null) return null;
if (connectionPool == null) return null;
if (!connectionPool.hasAvailableConnections()) return null;
if (webService == null) return null;

return doSomethingWithDbAndWs(connectionPool.connection(), webService);

Returning from the method as quickly as you possibly can will often improve readability and reduce its cyclomatic complexity. Cyclomatic complexity is a measure of how many distinct paths there are through code, and studies show a correlation between a program's cyclomatic complexity and its error frequency. You should use Guard Clauses whenever you can.

The second refactoring I tend to come back to is Compose Method. Compose Method is much harder to explain than Guard Clauses, because there are no defined steps to perform to complete the refactoring. In his book "Refactoring to Patterns", Joshua Kerievsky explains it as a "[transformation of] the logic into a small number of intention-revealing steps at the same level of detail". This will result in programs with many small methods, each a few lines long. Let me give you an example (I stole this from Refactoring to Patterns);

public void add(Object element) {
if (!readOnly) {
int newSize = size + 1;
if (newSize > elements.length) {
Object[] newElements = new Object[elements.length + 10];
for (int i=0; i<size; i++) {
newElements[i] = elements[i];
}
elements = newElements;
}
elements[size++] = element;
}
}

I cannot rapidly see what this code does. I have to read through it thoroughly to understand the logic. Performing the Extract Method refactoring a couple of times makes for a whole different experience:

public void add(Object element) {
if (readOnly)
return;
if (atCapacity()) {
grow();
}
addElement(element);
}

There are several reasons why the refactored version is earsier to read. It is much shorter, and it is now composed of method calls to well named methods that describe what operations are performed. In addition we converted the nested conditionals to guard clauses.

If you consistently apply Composed Method, you will find that most of your code comments will be superfluous; your methods will be self documenting. In the long run, self documenting composed methods are easy to maintain.

I find Guard Clauses and Composed Methods to be two extremely important techniques for writing maintainable code. If you do not have any experience with either, I would recommend you take steps to learn them both. It will pay off, guaranteed!

Saturday, November 04, 2006

New Monitor

The last year my desktop has seen a lot of monitor setup changes. Early this year I had only my laptop. Getting an additional 17" was truely amazing. Especially when programming, screen real estate is very important. More information available at the same time, without having to scroll and switch views, is a major boost to productivity. The months following, in addition to my laptop, I had a 19", 20", and now, finally I have a 24".

The 24" Dell monitor arrived yesterday. This panel is pretty close to what I call an optimal monitor size. Any bigger than this, and you have to tilt your head to see everything that is displayed. You don't have to do this with 24", but it is still huge! I love this monitor! With its 1920x1200 resolution, you can fit lots windows on it, and the twenty four inches make the physical sizes of the windows viewable aswell. My laptop resultion is also 1920x1200, but with only 17" everything is tiny.

I was adviced by my colleagues that I should buy two of these monitors aslong as I was buying... I'm quite happy with only one, but is two 24" panels the optimal setup? I can imagine it is...