Monday, July 25, 2005

If statements inside out

NOTE TO SELF: I am doing if statements in reverse. Instead of

bool error = someFunc();
if(!error) {
// do the next thing
} else {
// do error corrective actions

I should be doing

bool error = someFunc();
if(error) {
// try to fix or bail

// go on

This makes much more sense to me, and I'm not sure why I was doing it the other way before. I guess it is the easier way to think about it initially. The original way I was handling errors meant that a) I was moving the error handling away from the place where I actually caught the error, and b) was nesting things. a lot. The second form means that the normal flow of execution stays at the top level. This means that the eye can follow it much easier. Also, the error blocks are indented, and noticably different. You can't stick with the "single return point" mantra, but I've never done that anyway (it has never made sense to me), so nothing changes for me.