I just sent this e-mail to a few mailing lists, but I know there are a few coders who read my LJ and may find this information useful. -- I just listened to a neato talk by Scott Collins at Netscape about writing portable code. During the talk, he brought up a technique I'd never seen before. The idea is that you should always declare variables in the smallest scope that could reasonably enclose them. Let's say you have an object that can return a reference to another object. The naive approach (and the one I always have done before) is:
class Object {
  // ...
  Child* getChild();
  // ...
}* object;

// ...

// get a child if we have one
Child* c = object->getChild();
if (c) {
  // did we get one?
}
The technique Scott mentioned was:
if (Child* c = object->getChild()) {
}
It looks kind of strange at first, but this way, c is in scope if and *only* if it is a valid pointer. You can do something similar with dynamic_cast.
Base* b;  // ...

if (Derived* d = dynamic_cast(b)) {
  // d is only valid iff b is really a reference to a Derived object
}
I thought that was really cool. :) Another important concept he touched on was "write what you mean". For example, to test if a string is empty, a lot of people write something like the following:
if (str.length() > 0) {
  // nonempty
}
Let's say your string is implemented with a C-style, null-terminated, character array. Testing for the empty string by checking the value of its length is O(n), and may take a while for really large strings. Another, clearer, way to write the code is the following:
if (!str.empty()) {
  // nonempty
}
The implementation of empty() can be O(1), and the intent of the code is clearer. (specifically, here's an O(1) impl of empty():
  bool empty() {
    return !mStr || !mStr[0];
  }
) You can download the movie in RealPlayer format at ftp://ftp.mozilla.org/pub/mozilla/presentations/cpp-portability.rm. You can view his slides at http://www.mozilla.org/hacking/cpp_portability/.