My thoughts as an enterprise Java developer.

Monday, January 15, 2007

Tip: Need to communicate between anonymous inner classes?

Here is a quick tip:
Do you have two anonymous inner classes that need to communicate? I had two anonymous inner classes that implemented Runnable and needed to communicate with a boolean value. Therefore I decided to make a boolean variable in the declaring method but in order for the inner class to access the variable it needs to be declared final. But that would prevent me from changing the variable! I needed a mutable Boolean. Instead of creating a MutableBoolean class I declared the variable as "final boolean[] done = new boolean[]{false};" so that one thread could call "done[0] = true;" and the other thread could check "if(!done[0])". If there is a better pattern for this please tell me.

Background of why I needed this:
In an applet, I was saving data back to the server. In order to keep the UI responsive I of course do that in a separate Thread from the Event Distpatch Thread. When I save the data I first send then data and then read the response so the user only really needs to wait for the data to be sent. Therefore I have a timer that tells the user that they don't need to continue waiting after they have waited 30 seconds. But I don't need to pop that if the response is already read so I needed a way for the reader thread to tell the timer message Runnable that it was already done. I actually found a better solution: The reader thread can call stop on the Timer so that it is never run.

Tuesday, November 21, 2006

Google Desktop had to be removed

Since my last post on Google Desktop I had 3 problems with it:
  1. Paused indexing prevents rename of directories
  2. Google Desktop appears to hang computer then using Remote Desktop (Terminal services)
  3. IT required that I remove it because of security issues.
:-(

Thursday, November 02, 2006

Search and replace across files

Some of my co-workers had their cvs login name changed so we had the problem where every directory checked out from cvs had a CVS subdirectory with a Root file in it that had the old username. The easy way was to delete all checkouts and checkout again but if that would cause the loss of data we needed a different solution.

I handled my machine by doing a Windows search for all Root files, adding them to a new project in Source Insight, then doing a search and replace but others don't have nice editors like that. My machine had over 10,000 Root files so manual processes were out of the question.

For those without a nice editor that can do a replace across files I came up with the following solution:
  1. Install UnxUtils and add the main directory to the path.
  2. Run:

    find . -name Root -print | sed "s/.*/copy & &.new \&\& sed \"s\/jstauffer\/jstauffe\/\" &.new > & \&\& del &.new/" > Root.bat

    Windows has find so I had to make sure that it used the correct find. Basically it finds all Root files, creates a command to copy that file, use sed to do the replace back to the original file, and delete the copy of the file.
  3. Run Root.bat
I know the same could have been achieved in Perl or even Java but is there anything else that would be as quick and easy (granted it isn't easy to understand)?

Tuesday, October 17, 2006

Dragging in UI and finishing with a quick movement

When the user drags an item to a location, slows movement, and then makes a quick movement right before finishing the drag, the user probably wanted the ending location to be the mouse location right before the quick movement. The user should be prompted for which target they want if the quick movement changed the target. With normal mice there probably isn't often a quick movement but I use a pen (an old version of the Intuos 6x8) for ergonomic reasons and I often have a quick movement as I let go of the button on the pen.

Friday, October 06, 2006

Google Desktop Rocks!

I was developing a new feature and needed to add a new column to the database. I didn't remember the exact syntax so I went to look for a sample on my computer. A few weeks ago I had installed Google Desktop (because I had done some searching using Windows search that took an hour!) so I thought I would give that a try. I knew the command started with alter table so I first typed that into the search. In less than a second it gave 6 results but the first result wasn't useful. So I added quotes around the search words and an instant later it showed 6 results and the first result was exactly what I wanted! I was quite surprised by how fast and easy that was! I even had to show it to my co-worker LaMoine.
Just for comparison we went through the steps of doing it the old way. I knew there was probably an example in a specific directory (5 levels deep) so I clicked on the button for Windows Explorer in the task bar and did 5 clicks to get to that directory. I then did a search for the same term (without the quotes) and it gave the same file as the first result.
It took over 25 seconds compared to under 10 seconds for Google Desktop! That savings in time would be significant in keeping me "in the zone" except that I was so surprised by how good it was that I had to show LaMoine and write this blog entry. Anyway the next time I have to search for something on my computer (when not in an editor) I will be using Google Desktop! (I have no ties to Google -- I am just a satisfied customer).

Friday, September 08, 2006

Why I like my job

Here are a few reasons (off of the top of my head) why I like my job:
  • Telecommuting one day per week (I wish I could do that more.)
  • Being a "guru" on a product.
  • Good team that is smart and willing to do any task to help.
  • Good compensation
  • 5 weeks of vacation
  • Minimal office politics
  • The company is profitable and growing quickly
  • The company is the biggest company in our area (or so they tell us :-) ).
  • We use a language that I like (Java if you couldn't guess :-) )
  • We use 3rd party libraries where appropriate.
  • Few meetings and very few useless meetings
  • We attempt to use best practices and improve things as much as possible while still balancing business needs (like creating new features for users).
For reference my title is Sr. Software Engineer and the products on which I work are web-based using J2EE.

My employer: "SPS Commerce is the leader in hosted Electronic Data Interchange (EDI) software. We offer a rapid, turn-key service to connect suppliers with their retail and distributor customers using tested and proven EDI connections and integration services."

I like my job so well that pretty much the only reason that I would be willing to switch would be if I could telecommute more and still have most of the things above.

What do you like about your job and what would it take for you to move to another job? The harder it be for another company to lure you the better you probably like your job!

Wednesday, August 30, 2006

How do you make money with open-source software?

You don't, you save money!

(Some companies do make money but many more companies could save money). More companies are concerned with using software than selling software. If users of software fund the open-source development of software then it will make economic sense for the software to be developed open-source.
If 50% of companies that buy commerical database systems instead used an open-source database and spent 25% of the savings promoting and advancing that open-source database then it would all work out well.
The one difficulty is getting the software customers organized to do this. It really helps if someone can jump start the project so that other companies can see the value.

Would you rather spend $100 for for Windows XP or get Ubuntu and spend $25 supporting it?

Tuesday, July 25, 2006

Definitions: Always and Never

Definitions:

Always – At least 10% of the time.

Never – Less than 90% of the time.

Friday, April 07, 2006

Iterating

I often see code like:
Iterator i = list.iterator();
while(i.hasNext()) {
...
}
but I write that (when Java 1.5 isn't available) as:
for(Iterator i = list.iterator();i.hasNext();) {
...
}
because

  • It shorter
  • It keeps i in a smaller scope
  • It reduces the chance of confusion. (Is i used outside the while? Where is i declared?)

I think code should be as simple to understand as possible so that I only have to make complex code to do complex things. What do you think? Which is better?

Thursday, March 09, 2006

Sorting algorithms


http://www.cs.rit.edu/~atk/Java/Sorting/sorting.html
shows four sorting algorithms and animations for each. It shows Bubble, Quick, Odd-Even Transposition, and Shear sorts. The last two are multi-processor sorts so I have 2 questions:

  1. Could Bubble and Quick sort be changed to multi-processor sorts? It seems that Quick Sort could be.

  2. How would they compare in time to the other multi-processor sorts? It would be especially interesting how Quick Sort would compare.

Wednesday, March 01, 2006

Don't return in a finally clause

The code below actually prints "yikes!"
If you return in a finally block then any Throwables that aren't caught (in a catch block that is part of the same try statement as the finally block) will be completely lost. The really bad part about this is that it looks so innocent. I can think of no reason to return in a finally clause. If nothing else, catch Throwable and return in that so it is obvious what is happening. Note this also applies to other things that transfer control (continue, throw, etc).


public class Test {

public static void main(String[] args) {
try {
doSomething();
System.out.println("yikes! don't program like this!");
} catch (RuntimeException e) {
System.out.println("got it.");
}
}

public static void doSomething() {
try {
//Normally you would have code that doesn't explicitly appear
//to throw exceptions so it would be harder to see the problem.
throw new RuntimeException();
} finally {
return;
}
}
}

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.