Always Get Better

Never stop looking for ways to improve

May 1st, 2008

This isn’t new, but handy to have.  In order to tell the compiler which data type we’re using, we use suffix notation as follows:

  • Unsigned integer, U: e.g. 34506U
  • Long integer (signed), L: e.g. 5297532L
  • Unsigned long integer, UL: e.g. 30958UL
  • Float, F: e.g. 13.6F
  • Double, D: e.g. 14.3D
  • Decimal, M: e.g. 19.95M
April 29th, 2008

One very common requirement is for the number of days elapsed since a particular Date and Time. In C# this can be accomplished through the use of the TimeSpan class.

The easiest way to create a TimeSpan is like this:

TimeSpan tsMySpan = DateTime.Now.Subtract( dtCompareTime );

// The number of days elapsed can be accessed like this:
// tsMySpan.Days

April 5th, 2008

The project I am currently working on has several dozen different types of Form classes, each of which is accessible from a common menu strip. Rather than repeatedly instantiating each of the forms from the menu item handlers, I wanted to funnel the request to a single function.

The problem is: How do you instantiate a form when the type is unknown?

The code snippet is:

CreateForm( typeof(CustomFormType) );

Form CreateForm( Type formType )
{
return (Form)Activator.CreateInstance(formType);
}

April 4th, 2008

When working with a Windows GUI, it may seem unclear whether to use Form.Close() or Form.Dispose() to get rid of a dialog at runtime.

Form.Close() removes the dialog from sight and calls the Closing() and Closed() methods.  You can still access the form and bring it back later on.

Form.Dispose() destroys the dialog and frees its resources back to the operating system.   It does not call the form’s Closing() and Closed() methods. Once disposed, you may not recall a form.

Which to use? If you have no logic in the form’s close methods and don’t intend to re-use the form, go with Dispose().  Otherwise, go with Close().  Some programmers aren’t sure which to use, and they use both – Close() then Dispose()!

February 8th, 2008

Here’s a geeky party trick:

Abstract classes in Java cannot be instantiated. Here we’re going to consider ways in which a button can be programmed within a JPanel. ActionListener is used to react to the button event, but ActionLister is an interface (pure abstract class) so in order to use it you have to derive a class from it and implement actionPerformed.

For example, you can’t do this:

ActionListener buttonListener = new ActionListener();

You have to do this:

public class ButtonListener
{
public void actionPerformed( ActionEvent event )
{
}
}

ButtonListener buttonListener = new ButtonListener();
ourJButton.addActionListener( buttonListener );

However, this code is valid:

ourJButon.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent event )
{
}
} );

What’s going on? It would appear that we are instantiating a new ActionListener and giving it an actionPerformed() method. We’ve succeeded in giving our button a listener without first creating a class to handle the event.

Anonymous Inner Classes

Of course we aren’t really instantiating ActionListener – it was a trick. What this does is create an anonymous inner class only in this part of our code. Check it out – when you compile your code, javac will create extra class files for our ActionListener.

For one-off buttons, using anonymous inner classes is an excellent way of reducing code bloat and improving the readability of your programs. There are drawbacks of course, which I’ll go into detail in another article, another day.

January 24th, 2008

I had been running a short-lived site dedicated to the ins and outs of website scripting languages when I realized something about myself: I don’t know nearly as much as I once thought I did.

That revelation had several effects on me.  First, I began considering much more carefully everything I wrote.  Second, I learned a lot from being corrected by strangers with more experience than me.  Third, I became much sharper – the sudden scrutinization made me become a better programmer.

Finally, I came to understand no matter how good you (think) you are, you can always get better.

The purpose of this blog is to share the process of discovery that makes everyone better at what they do.  As an IT Professional, there really is no such thing as being at the top of your game because we all know the game constantly changes.  The push is to stay current, but you always have to stay knowledgable with everything that has already come to pass.  This could be why so many computer programmers constantly have scowls on their faces, even when collecting their $100k paycheques.

I invite you, dear reader, to suggest web and programming-related topics you may be interested in.  Correct me if something I write seems misinformed.  If we discuss and learn together, we will always get better at what we do.

January 19th, 2008

Because the program code I’ll be posting on this blog is hard to read when in the form of long colourless unformatted lines, I’ve looked around at various plugins to help make my work more readable.

WordPress suggests using a port of the syntaxhighlighter Google Code proejct by Alex Gorbatchev (http://faq.wordpress.com/2007/09/03/how-do-i-post-source-code/) but I found this plug-in to be too robust.

For my purposes, I plan to demonstrate HTML, C#, C++ and some Java, so my goal was to have a very scaled-back method of posting code.

A Google search brought me to Sean Deasy’s blog (http://www.seandeasy.com/code-syntax-highlighting-in-wordpress/) which pushed me in the right direction.

The WP-dp.SyntaxHighlighter:source code syntax highlighting plugin supports a good breadth of languages and has a minimalistic look – perfect for my needs. It can be found at:

http://blog.rubypdf.com/2006/09/28/wp-dpsyntaxhighlightersource-code-syntax-highlighting-plugin/

I recommend this plugin to anyone who, like me, needs a decent syntax highlighter without a lot of the bells and whistles found in other packages.

[Edit: The plugin file for this tool has 2 lines of whitespace at the end, after the "?>". If you install this tool, you will want to remove these extra characters (you can do this from right inside WordPress' editor page after uploading the file to your server).]

[Edit2-Feb8, 2008: I had problems getting the originally suggested Highlighter working the way I wanted. I eventually settled on this one from OpenSourceBrain: http://erik.range-it.de/wordpress/plugins/syntaxhighlighter/ ... it's the same idea but implemented much more cleanly]