Friday, October 09, 2015

Exception handling in Thread and SwingWorker

Exception handling in SwingWorker differs from Thread. When using a Thread if an exception is thrown inside run(), it is propagated up the chain and you can handle it outside if you want. When an exception occurs in SwingWorker.doInBackground(), you have to handle it in the done() method. The following code demonstrates this. If you delete the done() method, it will run but the exception will be lost.
 * Demonstrates difference of SwingWorker and Thread when an exception occurs. In SwingWorker, if you don't catch the exception in done(), it gets lost.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    new Thread(new Runnable() {
        public void run() {
            throw new IllegalArgumentException();

    new SwingWorker() {
        protected Object doInBackground() throws Exception {
            if (true) {
                throw new IllegalArgumentException();
            return null;
        protected void done() {
            try {
            } catch (InterruptedException e) {
                System.out.println("Caught exception: " + e);
            } catch (ExecutionException e) {
                System.out.println("Caught exception: " + e);

For more information see How should I handle exceptions when using SwingWorker?

Tuesday, October 06, 2015

Less Medicine, More Health

Less Medicine, More Health was especially relevant to me because my mother (65 years old) underwent colonoscopy as part of a routine control in which the doctor removed a few polyps and one of them had cancer cells close to the colon wall. Three out of four doctors said that she must have an operation which would remove a quarter of her colon (without saying anything about the risks of the operation). Only one doctor pointed out that may be all the cancer cells were taken out during the polyp removal, the risks and probability of success of the operation (which were horrible numbers). In the end my mother reasoned that the operation was not worth it. This was 6 months ago. Today she is alive and well, tending her garden.

Almost everybody has some sort of abnormality that won't hurt him but becomes a problem due to early diagnosis, i.e. you would be better of if did not perform the checks in the first place. Lesson: Don't do full checkups, only deal with things that you have symptoms of.

It shows the importance of randomization and biases that can be overlooked when performing medical trials, for example a trial based on volunteers can be biased because health conscious individuals are healthier than the average.

Thursday, September 17, 2015

Scrolling vs. Navigating in JTabbedPane

It is possible to programatically move between the tabs of a JTabbedPane:

There are two mechanisms: Navigating and scrolling. Their difference is that navigation changes the selected tab while scrolling shifts the view but does not change the selected tab.

Monday, August 31, 2015

Working with a junior developer

When I (a senior developer) have to supervise someone new (a junior developer) - who can be a new employee or an intern - I use the following steps to get the junior up to speed without taking too much of my time:

Note how it starts with an informal discussion of the problem to be solved. I sit down with the junior, tell him what I know about the problem, draw some sketches on a white board. The critical step is to let him write a problem definition document that repeats what we have talked about. When I review the document, I can verify that the junior has understood the problem well. This pattern repeats with design and test documents. It reduces my burden of supervision (I don't have to write anything) and helps the senior to really understand - not just "hear" - the problem. And last but not least, we get good documentation. Win-win...

Monday, August 24, 2015

Strategy pattern for modularization

When your software exceeds a certain size, it's a good idea to separate it into modules that can be compiled and tested independently (for example, it is easier to isolate memory leak causes). Some modules might also have value as standalone components.

Wednesday, August 12, 2015

The International Bank of Bob

The International Bank of Bob explains how the microlending platform Kiva works by visiting the recipients of such lends around the world.

I find Kiva convenient for two reasons: It makes it very easy to find someone who could really use financial help and it is as transparent as it gets.

Before thinking about the details of microlending and Kiva you have to ask yourself "do I really care about the plight of others so much that I can part with my $25?" You have to give an honest "yes" before coming up with questions about how reliable/efficient Kiva is. I suspect most of such questions stem from not being that committed to helping others but being ashamed to admit it and instead formulating more rational sounding questions. After you gave that first "Yes", read this book. In the words of Bob, "Pretty much the only things we do get to choose are how we treat others and conduct ourselves in the world. Are we honest? Are we caring? That’s who we are."

Kiva team: Friends of Bob Harris

Kiva fellows blog

Tuesday, August 11, 2015

Thinking Fast and Slow

Thinking Fast and Slow shows how hard it is to think and how often people avoid it and makes me wonder if anything I know is truly correct. "Considering how little we know, the confidence we have in our beliefs is preposterous".

It is depressing to realize the difficulty of changing our own faulty ideas, let alone changing others. To help myself, I read books like this. When arguing with others, I never aim for really changing them. I don't bother with trying to be fair and give them the benefit of doubt because they never return the favor. I just shut them up with a few simple tactics so that they don't think that what they present is irrefutable brilliance. Schopenhauer's "Die Kunst, Recht zu behalten" has good techniques.

Thinking as correctly as possible is not some academic detail. I recently had to confront two important, life-changing challenges: One was which house to buy and the other was whether my mother should have a risky operation. With rationality and luck, both turned out very well (We bought the best house possible and my mother rejected the operation which in hindsight was a good decision).

Another application of this book might be using it when raising a child (I have a 3 year old son). The hard way is to use rationality and correctness of argument. The easy way would be to appeal to the inherent weaknesses present in every human being [as politicians and marketers do].

The moral of the story is: be aware of your limitations (this book is for that) and think... a lot!