SlowerBuffer

Programming and Technology Related Stuff

What if It’s Null?

You find odd bits in any code base, especially one of significant age, with a few dozen different developers, that has gone through massive refactoring and redesign.

In fact, you might come across something like this:

1
2
3
4
5
6
7
8
    ...
    TimeInterval timeout = this.parameters.getSecondsFromParameter("timeout");
    if (timeout != null) {
        setTimeout(timeout);
    }

    setTimeout(timeout);
}

Which might make you wonder how it works when the timeout is null, since it’s setting it either way. Diving into the setter yields:

1
2
3
4
5
public void setTimeout(TimeInterval timeout) {
    if (timeout != null) {
        this.timeout = timeout;
    }
}

Ah, well that explains it. No bug, but some code smell.

Of course, even in newer applications you can find weirdness, like this:

1
2
3
@staticmethod
def starts_with(value, prefix):
    return value.starts_with(prefix)

Wait…wut?

Comments