DecentXML 1.2, my own XML 1.1-compliant parser, is now available.
Text Editor Component and JADS
27. August, 2008While working on DecentXML (1.2 due this weekend), I’ve had those other two things that were bugging me. One is that there is no high-quality, open-source framework with algorithms and data structures. I’m not talking about java.lang.Collections, I’m talking about red-black trees, interval trees, gap buffers, things like that. Powerful data structures you need to build complex software.
Welcome the “Java Algorithm and Data Structure” project – jads. I haven’t started opened a project page on SourceForge or Google Code, yet, but I’ll probably do that this weekend.
Based on that, I’m working on a versatile text editor component for Java software. The final editor will work with user interfaces implemented in Swing, SWT and Qt. It’s an extensible framework where you can easily replace parts with your own code to get the special features you need. I currently have a demo running which can display text, which allows scrolling and where you can do some basic editing. Nothing fancy but it’s coming alone nicely.
If you want to hear more about these projects, post a comment or drop me a mail.
Four harmful Java idioms, and how NOT to fix them
4. August, 2008In his article “Four harmful Java idioms, and how to fix them“, John O’Hanley writes about how to make Java more maintainable. He picks four common patterns and gives tips how to fix them … or not. Follow me.
Names
The first idea is to prefix names with a letter giving a hint what they mean: Is “start” a method? A field? A parameter? The goal is to make the code more readable to humans.
Unfortunately, this doesn’t work. The human brain doesn’t read letters, it reads words. So “fStart” (meaning a field with the name “start”) is rejected by the brain because it’s not a word. This triggers the conscious analysis which John tries to avoid! Which is why modern IDEs use color to tell you what something is: The brain can decode color and words in independent parts – unconsciously.
Packaging Convention
Next, he moves on how to split code into packages. Currently, we use a “package by layer” scheme, meaning all DB code goes into one package and the model code into another and UI layer in a third, etc. He proposes to use a “by-feature” packaging with the litmus test “you should be able to delete a feature by deleting a single directory, without leaving behind any cruft”.
Uhm. When have you ever written any code where you could remove a feature just by deleting a class? This sounds nice and simple but it’s fails Einstein’s litmus test: “Make it as simple as possible but not more simple”. Even if you have a plug-in based software like Eclipse, this doesn’t work because there are still references outside (otherwise, your plug-in wouldn’t be able to do anything).
Also, to keep a feature as isolated from everything else as possible (which is a good thing), you need to copy a lot of code into the feature which would otherwise reside elsewhere, neatly packed up in its own package. Really just a limitation of Java where you can’t tell the compiler to generate boiler plate code for you. Still, you need to cut code in such a way that it reduces dependencies, not increases them. Therefore, a general rule won’t cut (or maybe it will cut: you).
Immutables
John quotes: “‘Classes should be immutable unless there is a very good reason for making them mutable,’ says Bloch.”. And later: “From a practical perspective, many widely-used frameworks require application programmers to use JavaBeans (or something similar) to model database records. This is deeply unfortunate, because it doesn’t allow programmers to take advantage of the many positive qualities of immutable objects.”
From a practical perspective, immutable objects are dead weight. Applications are all about changing something. I read data from the database, I modify it, I write it back. I rarely read, display and forget about something. Yes, immutables have advantages because they can be shared between threads but that’s their only advantage.
Just think about this: You must modify data from the database. So you read the data into an immutable. How to modify it, now? Obviously, you need a method to change it. If you prefer setters, the “setter” must return a copy. So you need to copy the object for every single change. If you want to get a feeling for that, try to do math with BigDecimal. Okay, after the copy you can write the copy back to the database. Question: How do you notify everyone else who might have a (now stale) copy of the old immutable? There are no listeners; immutables can’t have listeners. Duh. Driving this to the extreme, lists wouldn’t offer methods to add or remove items; or rather they would return new copies of themselves after every add/remove operation.
Sorry, no sale. I can’t add money to my cash register. It’s immutable.
And a colleague just introduced me to another great concept: Constructors which require values for all fields. The class in question has 95 fields. This idea has the following flaws: a) No matter how big your screen, you can’t fit the call onto it. b) After argument #10, you lost track and you can’t see anymore which value goes into which argument. Now imagine you have to remove a field. How do you find the right one in this mega-call?
No, nothing beats the no-arg constructor plus a list of setters, all costs considered.
Private members
John proposes to move private members to the end of the class. Here, I agree. I’d even put them close to the getter and setter so that a lot of stuff that belongs together is together.
In todays IDEs with their superb code navigation (I can’t really believe there was a time before the F3 key), this doesn’t matter much, though.
Conclusion: Think about it, but don’t bother.
Update to DecentXML
30. July, 2008I’ve updated my XML parser. The tests now cover 97.7% of the code (well, actually 100% of the code which can be executed; there are a couple of exceptions which will never be thrown but I still have to handle them) and there are classes to read XML from InputStream and Reader sources (including encoding detection).
The XMLInputStreamReader class can be used standalone, if you ever want to read an XML file with the correct encoding.
You can download the sources and report issues in the new Google Code project I’ve created.
A Decent XML Parser
29. July, 2008Since there isn’t one, I’ve started writing one myself. Main features:
- Allows 100% round-tripping, even for weird whitespace between attributes in elements
- Suitable for building editors and filters which want to preserve the original file layout
- Error messages have line and column information
- Easy to reuse
- XML 1.0 compatible
You can download the latest sources here as a Maven 2 project.
DSLs: Introducing Slang
28. July, 2008Did you ever ask for a more compact way to express something in your favorite programming language? Say hello to DSL (Domain Specific Language). A DSL is a slang, a compact way to say what you want. When two astronauts talk, they use slang. They need to get information across and presto. “Over” instead of “I’ll now clear the frequency so you can start talking.” And when these guys do it, there’s no reason for us not to.
Here is an artical on Java World which gives some nice examples how to create a slang in Java and in Groovy. Pizza-lovers of the world, eat your heart out.
Toying With Swing
21. July, 2008I’ve been toying with Java Swing (the UI which comes with Java in case you’re wondering) a bit lately to determine which UI to use for my ePen project. I’ll post a longer article about my findings in the next few days but for now, just a few links I’ve collected:
From LegHumped:
- JTextField with background image (as in the location field in your browser)
- Word-wrap for JLabels
- Close a JFrame with Escape
- Utility methods for tree nodes
Then, there is the JFC Swing FAQ and of course the Java Swing Trail by Sun.
I’ve been looking for a good source on editor components. Swing Hacks looked promising but it seems to only scratch the surface like the rest.
Java Tricks: Fastest Way to Collecting Objects in a String
11. July, 2008The fastest way to collect a list of objects in a String in Java:
StringBuilder buffer = new StringBuilder ();
String delim = "";
for (Object o: list)
{
buffer.append (delim);
delim = ", "; // Avoid if(); assignment is very fast!
buffer.append (o);
}
buffer.toString ();
MQSeries 2045: MQRC_OPTION_NOT_VALID_FOR_TYPE
8. July, 2008While doing some work with MQSeries, I got an error “MQJE001: Completion Code 2, Reason 2045” in MQQueueManager.accessQueue() which translates to “MQRC_OPTION_NOT_VALID_FOR_TYPE”. Hm. Hey, IBM, how about adding real error messages to your products instead of having people look up odd codes in tables?
Anyway, the error means that I’m trying to open a queue for output which doesn’t support this. For example, remote queues can be opened with the option MQC.MQOO_OUTPUT but not with MQOO_BROWSE or MQOO_INPUT_AS_Q_DEF. Other queues don’t allow to read from them, i.e. you have to get rid of MQC.MQOO_INPUT_AS_Q_DEF in the openOptions.
See this page for all possible combinations: MQOPEN – Open object
Posted by digulla