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.)
My thoughts as an enterprise Java developer.
Tuesday, April 19, 2005
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.
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.
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?
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.)
A few of the problems with this code:
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:
- 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).
- while(r != null): Knowing nothing about JDBC it should be obvious that r could never change from not null to null.
- while(r.next()) is the standard way to use JDBC.
- 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.)
- The result should always be closed in a finally block.
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);
Subscribe to:
Comments (Atom)