Delete On Exit

A couple of days ago, I was working on some code. I had created a few temporary files on disk and needed to delete them. I was going to work out a scheme for recording the file names and hooking into an exit routine to delete them. I went to look for the delete file command in Java and found something brilliant: File.deleteOnExit.

I was thrilled and stunned to be confronted with the answer to my problem. All I had to do was call this function and Java would take care of the rest. It doesn’t get any easier than that. This is just one reason that I don’t agree with Jeff Atwood on his assertion that you must be a good typist to write code. If I had blindly started writing this utility, I would have added a few dozen lines of unneeded code. However, that’s not really the point here, just a corollary point.

My real point is that including deleteOnExit was brilliant. It’s just one example of why I love Java for writing an application. I’ve written loads of C++, and a little C# (but, not enough to know all of the hidden functionality). What amazes me is that I’ve been able to make incredible progress on my application than I would have considered possible in C++ in more than twice the time. Part of it is due to Java’s integrated UI, graphics, and table functionality. In C++, there’s an object oriented language, some utility classes, and the Microsoft windowing and graphics stuff that’s really old and poorly designed. In C#, at least there’s some integration of these things as an environment. But, that’s still not the point.

Getting back to the point – including useful functionality as part of the package saves people time. It’s part of picking the right tool for the job. It’s why you should write a script for repetetive programming tasks. It’s why you should refactor your code to create as many utility functions for other programmers to use. It’s why the Extreme Programming mantra you’re not going to need it is only partially on the mark. It’s why you should always think about how to make your code reusable. It’s why you should try to write as little code as you can to do the job correctly, creating and using the tools available to make coding easier.

The next time you’re writing some code, think about whether there’s a way that you can make it easier to use. Including deleteOnExit is just one example of saving thousands of users time because one programmer thought to add it to the toolkit.