Tuesday, September 30, 2008

The curse of the swallowed exception

Ever since I've been working in (and maintaining) Java code, I have found myself regularly trying to track down an issue where either there is no error message or the error message is mysterious and unhelpful like "read error at byte 0."

I think one of the most common and most expensive (in terms of maintenance) programming errors I see is the handling of exceptions.

I am sure everybody who is passionate about programming has their own principles, but this is my blog, so here are mine. You can give me yours in the comments.

David's Principles of Exception Handling and Reporting
  • Silence is deadly - This is the overriding principle. The stack trace is a beautiful thing to someone trying to fix an issue. An error message without a stack trace, or worse yet, silence, is the cloaked harbinger of hours, days, even weeks of hair-pulling debugging. Please, please, do not just quietly catch an exception and do nothing or just print out the message and not the stack trace.
  • When in doubt, throw it - If you call a method that throws an checked exception, generally you should rethrow the exception. The only time you shouldn't is if your method is responsible for communicating with the user.
  • Have one exception class per module - Here a module is a conceptual grouping of classes that together provide a service. I generally follow the principle that a module should have one and only one exception class, and all exceptions thrown by that module should be of that class. It's not helpful to creatively invent new exceptions for each condition. But, as usual, there are exceptions, particularly when you want to communicate a very particular situation. Bot those exceptions are rare, IMHO.
  • Don't break the chain - Because of the two principles above, you need to wrap exceptions you re-throw in the exception class for your module. Please don't just throw a new exception without wrapping the old one - vital information is lost that way, and you are likely cursing somebody (maybe even yourself) to hours or even days of head-scratching. Java has had exception chaining for years - learn it and use it.
  • The buck stops here - If you have nobody to re-throw to (generally because your method was invoked by a user action), sorry, but it is your responsibility to report the error the user. Who the user is and how you report it depends on your application. If you're a server application, you need a way to send the error message to the client. If you're a user application, you need to report the error through the UI. In either case, you need translate geek-speak into user-speak. Thus the next principle...
  • Be a butler - When you report an error to a user, don't be a gruff soup nazi. Be helpful. Describe the error, provide a likely cause, and offer possible actions the user can take. So instead of "I/O error: unable to read next 10 bytes from stream" say something like "We encountered an error while trying to talk to the server. It is possible the network connection was lost or the server was stopped. Please check to see if the network is working and the server is running and try again."
  • Log it - When you report a nice helpful error message to the user, log the full stack trace to the error log and not just the message. Log anything else you think is useful, the more information the better. This essential for the poor sod who has to try to track down the cause of the error. If you don't have an error log, get one.
  • Now what - You've just reported an error, what are you going to do next? The answer, of course, is "it depends." It's a discussion left for another day, but my general principle is, if data or long-term state is involved, it's time to fail quickly to avoid data corruption. If data is not involved - it's about a user session and user interaction, you generally report the error and move on - the user is responsible for any corrective action.

Monday, September 29, 2008

Geva Perry: Thinking Out Cloud: Ellison's Anti-Cloud Computing Rant

As I and others have already pointed out several times, Oracle is the company of status quo. Things are very good for Oracle the way they are exactly now -- any change is unwelcome. They make money by having high-paid aggressive sales people shove big stacks of software down customers throat, forcing them to buy this software with up-front, fully paid-up prepetual licenses. They then continue to make profit from these customers by selling them annual maintenance fees, most of which customers don't need or use ... So take Larry at his word when he says Oracle is not going to change anything but marketing.

http://gevaperry.typepad.com/main/2008/09/larry-ellisons-anti-cloud-computing-rant.html

Wednesday, September 24, 2008

Tom's Essay - Suzanne Vega

A great wandering, fun, delicious blog by Suzanne Vega about Tom's Song and Tom's Album and how she became declared "The Mother of the MP3"

http://measureformeasure.blogs.nytimes.com/2008/09/23/toms-essay/index.html?ref=opinion

Starting MySQL in NetBeans as root/administrator

As you may or may not know, NetBeans 6.1 and 6.5 provide the ability to let you start and stop your MySQL server directly from NetBeans. To be honest I wasn't sure how valuable this feature would be when I implemented it, but in my own work I've found it to be very nice. It's a small thing but it helps me maintain my "flow" when working with the database.

One challenge is that often MySQL can only be run with administrative/root privileges.

On Linux and Solaris you can use 'gksu' to start a program as root, and a dialog will appear asking you for the root password. So if you right-click on the MySQL node and choose Properties and then select the Admin tab, you can make gksu the command name and the actual command to start and stop MySQL is part of the command arguments. Here is a screen shot to give you the idea:

linux-properties.jpg

Now, gksu doesn't exist on Mac. The command to bring up a dialog for the administrator password is somewhat complex. So I've written a little script called "macsu" that looks like this:
#!/bin/sh/usr/bin/osascript -e "do shell script \"$@\" with administrator privileges"
Then in your Properties window you can do the following:

mac-properties.jpg

Et, voila, you get a prompt when you run the start command:



Note that my 'macsu' command doesn't handle "--" options well, so if you want to do "--console" you might have to wrap your mysql start command in your own script file, and then invoke that file from macsu.

To be honest I have no idea what you can do on Windows. Does anyone have any tips?

Why is the default answer always a web app?

On one of our NetBeans mailing lists, a new programmer asked a question, how should they build their application that needs to run on ten client machines talking to a MySQL database.

The quick answer was "build a web app."

I just don't understand this. The web app architecture is - c'mon, admit it - ugly for applications that need to have dynamic behavior (e.g. almost every app out there). It was never built for that. HTTP/HTML is a static, document-oriented protocol and markup. HTML came from SGML, which is all about documents and publishing, not dynamic database-driven OLTP applications.

But the browser is ubiquitous. It's the absolute best way to distribute a service out to millions of people. Its the visual channel into the Internet. So we've worked with it. Thousands of developers have grown up building apps using HTML/CSS/JavaScript, session state, multiple tiers and so on.

But it wasn't always that way. When I was fresh out of school, the browser didn't exist, and we built simple client/server applications. The client talked directly to the database using a database-oriented protocol.

There were issues, of course, the primary one being how to manage versioning and upgrades across thousands of machines, each with their own operating system and versions, some of them sporadically connected (e.g. on laptops). One of the key advantages of the web is that you install it in one place, and everybody automatically gets the upgrade. And it runs on everybody's machine. It's so cool.

But today there is Java (write once, run anywhere) and Java Web Start (auto-upgrade to all your client machines). They are mature and battle tested. If your application is only going to run on the local network, there is absolutely no need to create a multi-tiered HTML/CSS/JavaScript monster. You can use traditional client/server, with rich clients written in Java and deployed by Java Web Start talking directly to your database using JPA/Hibernate/JDBC. You can use the visual design tools in NetBeans or Eclipse to visually build your application. No hacking <table><tr><td> garbage.

Having a middle tier also allows you to disintermediate between the clients and the database, which can give you a chance to improve performance and scale through things like caching, pooling, clustering, replication and so on. But none of that is needed when you have a small number of users, so why create complexity when it's not required?

Now, if you're building an application to be accessible over the Internet, or if you need to scale, it's a different story. You need a middle tier, and one that speaks HTTP if you're going over the Internet.

But even then you can use Java and Java Web Start, and your client application can talk to the middle/web tier using a service API. There is no reason why the server should be responsible for composing the UI for your application. It can be a server, not a UIer.

If you have Java antibodies, you can use Flex or Silverlight. Or take a look at Cappucino.

There are reasons why these RIA architectures are popular - they are meant for dynamic applications and dynamic behavior, unlike HTML, which really was never intended to be anything more than a way to display static documents. With these RIA environments, finally we're getting back to building apps with tools and an underlying architecture that match the dynamic application paradigm.

Now, folks like Google want you to keep building browser-based HTML applications, using JavaScript for dynamic behavior to muck with the DOM (note their new browser with superior JavaScript performance). Why? So they can scan your text-oriented application and inject targeted ads. Not possible with RIAs where the application is compiled into byte codes.

But just because it works for Google doesn't mean it works for you or your users, or that it's the best architecture. It's just the architecture we've had to work with for the past fifteen years.

Now, there are times when a web app makes sense, absolutely. The answer, as always, is "it depends." But when someone asks you how to build a database-oriented app please don't just default to "web app". There are other, potentially superior, options out there...

Tuesday, September 23, 2008

Drag and drop service consumption in NetBeans PHP

Very nice screenshot showing how easy it is to generate PHP code to consume various web services, such as Amazon S3, Facebook, Delicious, Google Maps, etc. Includes a link to a tutorial on how to use this feature for Yahoo News Service.

http://blogs.sun.com/netbeansphp/entry/software_as_a_service_in

Hey, Mr. Senator, can I have some of that?

I hear tell that the gummint is handing out cash to anyone in trouble.

Well, hey, Mr. Senator. My bank won't give me no loan, and it's costin' me more and more for food and clothing. Baby needs a new pair of shoes. Can you give me some of that there 700 BILLION dollars? Where do I sign up?

Looking to talk to a few good PHP developers

Well, our team is hitting code freeze today, and so it's time to look ahead and think about database tooling for NetBeans.next.

Last year I interviewed 10 different Java developers about how they use databases, and it was very enlightening. But I didn't talk to very many PHP developers.

I'd like to change that, and YOU can help. If you develop with PHP and would like to help the NetBeans db team improve your productivity when it comes to working with databases, then let me know, and let's talk. You can reach me at david dot vancouvering at sun dot com.

Monday, September 22, 2008

Shock politics

When I read the headline today that Bush is putting heavy pressure on Congress to accept the bailout plan, where we throw at a minimum 700 billion dollars (or more) at Wall Street with no strings attached, saying it's a crisis and we must act quickly, and when there are serious concerns with it, I smelled hidden agenda.  You can just feel it in your bones.

Last week my wife told me that she had read an article where it explained that a big agenda of the current administration before it leaves is to empty out the government coffers as much as possible.  I walked away scratched my head wondering why on earth anyone would want to do that.

Then today I read this article by Naomi Klein and a light went on in my head.  If we're drained of cash, then it becomes much easier to push through "cash-saving" agendas such as privatizing social security, lower corporate taxes and cut spending on the poor.  It severely hobbles any new administration to do anything progressive.

Eight years ago I would never have believed such a cockamamy "conspiracy theory."  But these days, after watching what has happened with the Iraq war, with the EPA, and with the politicization of the Justice Department, well, I'm not so sure.  I have the ugly feeling this may actually be a strategy.

If it's true, and they succeed, heaven help us.  If such backroom strategists are really thinking this way, I don't think they really have any idea what the true consequences of such a strategy might be.  Talk about short-sighted loot-and-burn out-and-out greed.  It makes one shudder.

No, not *that* Palin

Get Fuzzy is a great comic, but I think I haven't laughed at any of them more than these two. Priceless.

Consensus does not mean we all agree

I have had a number of colleagues come to me over the years since I've worked in open source expressing frustration and almost hopelessness as they found themselves trying to obtain consensus for their open source contribution.

What I tell them is that just because the community is consensus-based does not mean everyone has to agree with you. 

Here's one definition of consensus I found on the web: General agreement, characterized by the absence of sustained opposition to substantial issues by any important part of the concerned interests.  My dictionary on my Mac makes it even simpler: general agreement.
 
The key word here is general agreement.  I also like the phrase "absence of sustained opposition in substantial issues."

Many people get frustrated working in open source because it feels like any attempt to make a substantial change gets bogged down in numbing committee-like discussions.

Now, I won't deny it, there is overhead in working with open source.  You do need to get feedback, and make a best effort to address comments or concerns.

But that doesn't mean you iterate and iterate and iterate in some Sisyphean nightmare trying to get everyone to accept your change, or that you hunker down and make a change you disagree with. 

If you're a committer, you take the feedback, incorporate what makes sense to you, and if there is absence of sustained opposition, you check it in.  If you are not a committer, then all you need to do is find one committer who supports your change and they can check it in for you.

In the Apache model, sustained opposition is in the form of a veto.  A veto is taken very seriously - if you veto, you had better have a very good reason, and you had better offer an alternative. 

In the OpenSolaris ARC model, reviewers provide comments in the form of TCAs (Technical Change Advised) and TCRs (Technical Change Required).  It's only the TCRs you have to worry about.  Your change won't be accepted withough addressing the TCRs, and generally there are few if any TCRs.

I like the TCR/TCA model because it (a) explicity identifies which issues are causing you to veto a change and (b) requires you to be very clear about what needs to change, rather than just saying "-1".

I learned the principle of "consensus does not mean we all agree" the hard way.  When I was new on the Apache Derby project (my first open source project) I spent three months trying to get agreement on a change that would allow sharing of code between the network-based JDBC driver and the embedded JDBC driver.  It was insane.  I would try to please one person or group, and then another set of people would complain, back and forth.  Finally I got a private email from one of the veterans on the project who said "you're never going to make everyone happy, David.  Nobody's threatening to veto.  Just check it in."

Oh.

So now I offer the same advice to people when they complain to me that they feel if they have to bend over backwards for person X or person Y or they say something like "OK, fine, I'll do it your way, but I don't like it."  Relax.  Consensus does not mean we all agree.  Just check it in.

Thursday, September 18, 2008

Barack Obama's Economic Plan

Wow. Direct, to the point, and ( (although I'm not sure about the tax rebate) it actually makes sense. Read more here.
  • Revamp tax system to work for the average American, not the rich
  • Regulate Wall Street
  • Energy independence in 10 years
  • Crack down on lobbyists
  • Responsible end to war in Iraq
It won't be easy. But we're Americans. We've met tough challenges before and we can again.

Nicholas Kristof - Need a Job? $17,000 an Hour. No Success Required.

Pointed, painful, outrageous, hilarious.
John McCain seems to think that the problem is that C.E.O.’s are greedy. Well, of course, they are. We’re all greedy. The real failure is one of corporate governance, which provides only the flimsiest oversight to curb the greed of executives like Mr. Fuld....
John Kenneth Galbraith, the great economist, once explained: “The salary of the chief executive of a large corporation is not a market award for achievement. It is frequently in the nature of a warm personal gesture by the individual to himself.”

http://www.nytimes.com/2008/09/18/opinion/18kristof.html?ref=opinion

Friday, September 12, 2008

Paul Krugman - Blizzard of Lies

It is stunning to see advertisements approved by John McCain stating flat, complete, and utter lies.
What it says, I’d argue, is that the Obama campaign is wrong to suggest that a McCain-Palin administration would just be a continuation of Bush-Cheney. If the way John McCain and Sarah Palin are campaigning is any indication, it would be much, much worse.

http://www.nytimes.com/2008/09/12/opinion/12krugman.html?ref=opinion

Wednesday, September 10, 2008

Ludovic Champenois's Blog: GlassFish V3 Prelude Update Tool

Ludo gives a nice demo of the Glassfish V3 update tool in action. An app server that is incrementally upgradable like Ubuntu. Pretty nice!

http://weblogs.java.net/blog/ludo/archive/2008/09/glassfish_v3_pr.html

Word wrapping with a JLabel

I am sure a lot of you already know about this, but it took me a while to find this in Google-land, so I thought I'd share it here.

If you want to create a JLabel that does word wrap dynamically, so that if you resize the window the label "does the right thing", then wrap the label text with <HTML> and </HTML>

Memcached User-Defined Functions for Java DB : Knut Anders Hatlen's Weblog

Wow, now this is cool. Some sample code that shows you how to turn Java DB into a memcached engine. Take a look at this snippet:

ij> create trigger insert_trigger after insert on my_table
referencing new as n
for each row
call memcached_set(n.id, n.x);
0 rows inserted/updated/deleted

http://blogs.sun.com/kah/entry/memcached_udf_for_java_db

Drew Westen: What Obama Needs to Do in the Final Sixty Day

Long but great essay with a lot of zing about the problems with standard Democratic strategy and some excellent suggestions (IMHO) or Mr. Obama. I don't know about you, but I am horrified with the decisions McCain is making and am impressed by the ruthlessness of the Republican political machine. I would desperately like to see Obama and his campaign take McCain/Palin by the jugular and stop namby-pambying.
This isn't just about politics. This is about pulling out all the stops to make sure we don't have four more years of horrible Republican policy - I mean, look around at the mess in this house (country)! It's deplorable, and it's been done on the backs of the middle class and poor.

http://www.huffingtonpost.com/drew-westen/what-obama-needs-to-do-in_b_125051.html

Tuesday, September 09, 2008

Bob Herbert - Liberals, Hold Your Heads Up

Great editorial by Bob Herbert reminding all of us the amazing accomplishments of liberals in the US. I highly respect and agree with conservative's championing of traditional values such as honesty, sacrifice, hard work and family.


But let's remember the love, hard work, sacrifice (including jail time, verbal and physical abuse, even lives lost) given by liberals fighting for the repressed and ignored - women, blacks, the elderly, gays, our health, the environment.


Without these honorable fights against a highly resistant conservative status quo, where would we (and our brothers and sisters and children and parents) be today? Hear hear, Bob.

http://www.nytimes.com/2008/09/09/opinion/09herbert.html?_r=1&em&oref=slogin

Monday, September 08, 2008

Skype ignores PayPal siphoning hijack scheme | The Register

Ouch. This is nasty. I use both Skype and Paypal, and now I wonder if I should move on to something else. I stopped using EBay years ago after a number of nasty experiences.

http://www.theregister.co.uk/2008/09/02/mysterious_skype_hijackings/

Sun and NetApp gain external storage market share | The Register

You know, we make excellent products, and it's nice to see people taking notice...

http://www.theregister.co.uk/2008/09/05/sun_and_netapp_gain_market_share_gartner_and_idc_say/

Patrick Keegan's Blog: A Personal Data Storage Application With Embedded Java DB

Patrick has done a very useful blog showing how you can use JPA with embedded Java DB. Thanks, Patrick!

http://weblogs.java.net/blog/pkeegan/archive/2008/08/a_personal_data_1.html

Friday, September 05, 2008

Economic View - Is History Siding With Obama’s Economic Plan? - NYTimes.com

This article review a book that reveals statistical facts that reflect my experience at any rate. The Republicans talk about being for the common man and woman, and perhaps many of them are, but the ones in power have shown by their actions that they are very much for the rich and powerful.
The two Great Partisan Divides combine to suggest that, if history is a guide, an Obama victory in November would lead to faster economic growth with less inequality, while a McCain victory would lead to slower economic growth with more inequality. Which part of the Obama menu don’t you like?

http://www.nytimes.com/2008/08/31/business/31view.html?em

U.S. Rescue Seen at Hand for 2 Mortgage Giants - NYTimes.com

Nice to know that wild speculation is being bailed out on the backs of taxpayers.

I know, I know, better than not doing anything, but it sure reminds me of sending drunken Uncle Bob some cash so he doesn't end up in jail.

Promises of reform to follow, I suspect, followed by a short memory, debates, and a watered down bill in Congress.

http://www.nytimes.com/2008/09/06/business/06fannie.html?hp


And here, this is what really gets my goat:


But privately, senior officials have been critical of top executives at the companies, particularly Freddie Mac. They have raised concerns about major risks to taxpayers of a bailout of companies whose executives have received huge compensation packages. Mr. Syron, for instance, collected more than $38 million in compensation since he joined the company in 2003.

Must be nice, to have the taxpayers foot $38 million in personal income. Consequences, what consequences?

What's even worse is that the reason investors didn't help out these two companies was because everyone knew that the US government would bail them out sooner or later. Fait accomplis...

Sigh...

Major Republican Hypocrisy - The Daily Show | Comedy Central

A friend showed me this video. Hilarious and also very seriously revealing. The two quotes from Karl Rove are particularly damning. Warning - a little strong language in some of the commentary.

http://www.thedailyshow.com/video/index.jhtml?videoId=184086&title=sarah-palin-gender-card

TechCrunch: Cappuccino Brings Cocoa-Like Programming To The Web

OK, this can change everything. If you can use a full-fledged programming language like Objective-C to build web apps, and you don't have to know HTML and CSS (and who would want to if you had a choice), well, now that's interesting.


Cool thing is if you learn this, then you can write stuff for Cocoa as well. As a programmer, anything that lets you do two in one blow is very attractive. I have avoided writing web apps for years because of the hideous, painfully low-level programming environment. This could switch me over.

Also, think about it - the full application is running in Javascript on the IDE. This means the server isn't busy building HTML/CSS, but it simply in the job of delivering Javascript and providing back-end services for rich web clients. Cool.

http://www.techcrunch.com/2008/09/04/cappuccino-brings-cocoa-like-programming-to-the-web/

Thursday, September 04, 2008

Brian Aker - Deleting Your Work, Starting over is good for the soul...

It felt good to delete yesterday's tree and start over again. More often than not this is a best practice :)


A nice short blog about something I have done a number of times. It's good to know I'm not the only one, and yes, now that I think of it, it is a good thing.

http://krow.livejournal.com/608110.html

NetBeans 6.1 has a new patch release available

Hi, all.  I thought I'd share this message from our sustaining team:

Hi All,


I would like to inform you that a new patch for NetBeans IDE 6.1 is now available on the Update Center. The patch includes fixes in modules for BPEL, C/C++, Composite Application, Database, Editing Files, GUI Builder, GlassFish, IDE Platform, Java, Java EE, Java Persistence, NetBeans Plugin Development, Platform, RESTful Web Services, SOA, TAX Library, WSDL, Web Services, XML Productivity Tools, XML Schema Support, XML Tools Java Ext, XML and Schema and XSL Support. 

The patch is available in English, Japanese, Simplified Chinese and Brazilian Portuguese languages.


For more information, see http://wiki.netbeans.org/wiki/view/NetBeans6.1PatchesInfo.


The update is easy to install when the NetBeans IDE is running. Just click on the globe in the status bar or go to Tools->Plugins->Updates tab, then follow the installation instructions.


Regards,
-Rudolf Balada

Wednesday, September 03, 2008

Government arrests protesters - in USA, not China

Pretty ugly.  I just find this astounding, in the country where the principle of free speech was established.