Fail faster

It's a cliché to say that mistakes are there to learn from. Or: everyone makes mistakes, as long as you learn from them.

I do think that I've learned the most (life lessions, even) not from the things I did adequately, but from the times I tried something more daring and failed. It gave me a clear understanding of why I shouldn't do what I did, because I've lived through it.

So I was thinking, wouldn't it be an interesting principle to try and fail faster?

And couldn't this also be a management principle of sorts? Instead of trying to cover up possible problems from the get go, instead have a go at them from the get go so you fail faster? Failure later in a project has larger consequences and leaves less time to address serious complications.

Regex

In regex, if you do lookaheads with conditions to validate a string, place the condition that is most likely to fail first. This increases the matching speed by knowing earlier when to stop.

Design regular expressions to fail: think about what happens when a match is not found. If you do a lookahead on X with (?=.*X).* then this is fine if X can be matched. But if it cannot be matched, you'll traverse the whole line, then the lookahead fails, then you move up one position, start the lookahead again, fail again, and so on. You want this pattern to be anchored to the beginning of the line so the lookahead fails after having traversed the whole line once.

Using contrast avoids unnecessary backtracking.

You can also make regex fail faster using possessive quantifiers or atomic groups by avoiding unnecessary backtracking. Also see regex catastrophic backtracking.