Skip to main content

Popular posts from this blog

Java Blooper #2: Must be a Better Way...

It's Monday, which means it's time for another blooper... What's wrong with this code? boolean hasThing( List things, Thing thing ) { for ( int i=0; i < things.size(); i++ ) { if ( thing.equals( things.get( i ) ) ) { return true; } } return false; } Update: Minor edit to add missing parenthesis from if statement that got "lost in translation" en-route to the blog :)

Configuring Mac OS X Terminal

I recently installed Leopard (Mac OSX 10.5) on a new mac. There are a few factory settings I usually change on a new installation, although by far fewer than I do typically with Windows. One of them is the default keyboard configuration for Ctrl+Left Arrow, Ctrl+Right Arrow, Page Up, Page Down, Home, and End in Terminal. The default settings drive me a bit potty since I'm used to using Linux and emacs every day at work.Changing them is easy, fortunately. Just visit Keyboard under Settings in Terminal->Preferences, and modify the following keys, so that their action matches the value shown. You can edit the keystroke for an item by double clicking on it, selecting "send string to shell", and typing the indicated keys.KeyActionKeystrokescontrol cursor left\033bESC bcontrol cursor right\033fESC fend\005CTRL+ehome\001CTRL+apage down\026CTRL+vpage up\033vESC v

Java Blooper #2 Solution: Must be a Better Way

Sorry for the delay in this week's blooper solution. On Monday we saw this code: boolean hasThing( List things, Thing thing ) { for ( int i=0; i < things.size(); i++ ) { if ( thing.equals( things.get( i ) ) ) { return true; } } return false; } Can we simplify this? Sure. It pays to know the class libraries well, and the collections framework in Java in particular: boolean hasThing( Collection things, Thing thing ) { return things.contains( thing ); } As well as being more compact, this idiom is safer if the collection is a synchronized wrapper (as pointed out by reader alex in the comments). We were also able to specify the things parameter as a Collection rather than a List, following item 34 in EJ ("Refer to objects by their interfaces"), since we no longer need the ability to iterate items in the collection by index, which may well have been inefficient depending on whatever implementation of list was passed to us.Blog readers jeff and Mike Ka…