My thoughts as an enterprise Java developer.

Monday, November 07, 2005

Strong vs. weak typing

I like strong typing but don't like weak typing because I would rather that the compiler find my bugs. A point in the middle is type inference.

http://beust.com/weblog/archives/000333.html


A co-worker pointed out that some languages allow a variable to be declared as strong or weak and that sounds like it would be the best of both worlds.

Monday, October 31, 2005

Synergy: Share keyboard and mouse between computers

I just learned about Synergy which is a software KVM (without the "V") and it looks pretty slick.

http://synergy2.sourceforge.net/about.html

Thursday, October 27, 2005

Good programmer vs. knowing a programming language

From http://weblogs.java.net/blog/flozano/archive/2005/10/what_makes_prog.html
It's just like giving a good programmer a course of drawing software. He may learn all about using the software, but he won't be able to create nice, pleasant figures. You need a different skill set to create graphics than to create software.

Wednesday, October 05, 2005

Declaring variables

A pet peeve of mine is when a variable is declared way before it is needed. It is much easier to understand a piece of code if a variable is declared just before it is needed. That immediately tells me that it isn't used anywhere before that or in any wider scope.

I also find it annoying when a variable is declared with a null value and them immediately assigned a value.

String value = null;
value = "hi";

should be:
String value = "hi";

Not that one reason to do that the first way is if the second way would create a line that is too long.

Thursday, August 18, 2005

IncompatibleClassChangeError

I just delivered a patch to SQA and got a IncompatibleClassChangeError. The javadoc description doesn't help much.

Thrown when an incompatible class change has occurred to some class definition. The definition of some class, on which the currently executing method depends, has since changed.


I looked at the line of code that had the error and went on a hunch. In my case a class used a variable in a super-class and I had changed that variable from static to instance. Therefore the class file in the patch was trying to access it as an instance variable but the old super-class still had it as a static variable. I changed the patch class to not use that variable and sent it back to SQA. Hopefully that fixes it.

Friday, August 12, 2005

Finding the file for a class

From http://java.sys-con.com/read/117751_2.htm

Classpath Problems
Another class of problems are classpath issues. There are times when you are not sure if there is another version of a class in the classpath that is getting picked up before yours, usually a result of a bad environment setup. To eliminate this possibility, a simple check is to add a print statement to see if your new code gets picked up. If it isn't getting picked up, you need to locate the other class that is getting picked up. One neat API in Java that allows you to locate where a class is being picked up is:

Class.getProtectionDomain().getCodeSource().getLocation()

In most cases, depending on the class loader being used, you'll get the location of the class that is being executed and you can correct your environment setup.

Tuesday, July 26, 2005

"Java" article?

Does this article/press release make any sense for a "Java" magazine?
http://java.sys-con.com/read/112734.htm

Friday, May 20, 2005

GUI designers take note: Selecting by first letter should show as many as possible that start with that letter

When I am in a list and I press a letter to jump to the first entry that starts with that letter why does it leave that entry on the bottom of the visible entries? It should make the entry the top visable entry so that many entries that start with that letter can be seen.

In more concrete terms if I am selecting a state from a drop-down list and press "w" it should make "Washington" visible at the top instead of the bottom so that I can see Wisconsin without scrolling.

This is just my little pet peeve of the day. :-)

Sometimes old solutions work best

I have a 4.8 MB file where I needed to do a replace that would happen about 850,000 times. I tried it in a few different editors:

  • Notepad: It looked like it was taking 3 seconds per replace so I killed that. I don't have the time to wait a month!
  • Source Insight (My preferred code editor): It partially suceeded in a few minutes but it ended up corrupting the output since it "ran out of undo space". Time to ask my co-workers...
  • jEdit: After running for at least 15 minutes I killed it.
  • TextPad: After running for at least 10 minutes I killed it.
  • sed: 10 seconds!


BTW my machine is a hyperthreaded P4 3.06 GHz with 1 GB of RAM with a serial HD.

Tuesday, April 19, 2005

Case of static method names

I like to make my static method names start with an upper case letter. I know that isn't standard and that only class names are supposed to start with an upper case letter but I find that it is helpful because it quickly shows which methods are static. Also, I have never confused a static method with a class because the usage is different. Does this sound beneficial?

(I don't know from where I got this idea but at one time I assumed it was a standard.)

Friday, April 08, 2005

Developer Documentation

Have you ever gotten a product and you could obviously tell that the person who wrote the manual didn't speak English natively?
From my little knowledge of languages it seems that it works better for people to translate into their native language.

I think the same principles applies to developers translating from code to documentation. I would prefer to explain how a product works to a user and have the user write the documentation because they will cover things that seem obvious to me but not the average user.
Someone not intimately familiar with the product will probably write much better documentation.

Finding duplicate code

I noticed some duplicate code in a program that I help maintain and thought "wouldn't it be nice to have a tool that found duplicate lines of code?"
After a quick chat with a co-worker we decided that we could make something that read in every source line, trimmed it, created a hash and added that hash to a List for that file. Also add that hash to a Map with a List of all of the files and lines where that hash occurs. When all files are read go through all lines with the same hash (from the Map) and look at the next lines to find if those hashes match. It wasn't a perfect design but it would probably work well enough.
Before I went any further I decided to check Google and low and behold I found PMD http://pmd.sourceforge.net/cpd.html. It is on its 3rd algorithm and "now it can process the JDK 1.4 java.* packages in about 4 seconds" and "works with Java, C, C++, and PHP code." I guess there isn't much point in writing my own version. :-) Remember to always do a quick search before coding something new.

Wednesday, April 06, 2005

Anonymous class constructor

Today I was trying to modify an anonymous class to add a parameter to the constructor. After talking to 3 other co-workers we couldn't decide if it was possible because a constructor has to be named to match the name of the class and an anonymous class does not have a name. I found the answer at http://www.developer.com/java/other/article.php/3300881 under the section titled "Anonymous classes versus local classes." I am not including the answer here so that you can think about it for a while if you desire.

Tuesday, April 05, 2005

Extra info in stack traces

I often find that when I see a stack trace I want to know the value of some variables in that stack. I created a request for enhancement (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4857236).
Does that seem useful to you?

What was null?

When I get a NullPointerException I usually have to look at the source code to determine what might have been null. Therefore I created a feature request (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4834738) to request that NullPointerExceptions include the name of the variable when possible. Do you think that would be useful?

Friday, April 01, 2005

del.icio.us Bookmarks

I have added most of my computer and programming related bookmarks to http://del.icio.us/Stauffer

Wednesday, March 30, 2005

How not to use JDBC

This bad example came from production code. :-) (DatabaseManager is a wrapper for JDBC and behaves similarly.)

String select = "select ...";
Result r = null;
List numberList = new ArrayList();
try {
r = DatabaseManager.ExecuteQuery(select);
while (r != null) {
r.next();
numberList.add(r.getString("col"));
}
}
catch (Exception e) {
Result.Close(r);//Closes the result while handling null and any IOException.
}

A few of the problems with this code:
  1. DatabaseManager.ExecuteQuery can never return null (but I can let that slide some since a programmer would have to look at the code to know that -- or look at the other 64 examples in code to see that no other code checks for a null return value).
  2. while(r != null): Knowing nothing about JDBC it should be obvious that r could never change from not null to null.
  3. while(r.next()) is the standard way to use JDBC.
  4. This code uses an Exception to get out of the while loop! (Note that I have done something like that once but there was at least a somewhat good reason and it was documented.)
  5. The result should always be closed in a finally block.
So here is the improved code:

String select = "select ...";
Result r = null;
List trackingNumberList = new ArrayList();
try {
r = DatabaseManager.ExecuteQuery(select);
while (r.next()) {
trackingNumberList.add(r.getString("col"));
}
} catch (Exception e) {
e.printStackTrace();
//Logging would be nice but a stack trace to standard error is better than nothing.
} finally {
Result.Close(r);
}

How may classes does it take to screw in a light bulb?

Is this factory overkill? First the factory class just caches an instance of the finder so the finder could just as easily have just static methods (there are no member variables in DocumentFinder). Second there seems to be no reason to declare finder as an IFinder and check instanceof since it must always be a DocumentFinder and even if it wasn't it would throw a NullPointerException. This looks like another case of mis-applied patterns. (BTW I think patterns are good when correctly applied.)

//FinderFactory just instantiates the class with Class.forName and stores
// the instance in a Map (it will return that instance on subsequent calls).

IFinder finder = FinderFactory.getFinder("com.app.finders.DocumentFinder");

DocumentFinder documentFinder = null;
if (finder instanceof DocumentFinder) {
documentFinder = (DocumentFinder)finder;
}
return documentFinder.findDocuments(documentSourceId);