Assertions are checks which may get switched off. They're rarely used. Why?
result != null
as such checks are very fast and there's hardly anything to save.So, what's left? Expensive checks for conditions really expected to be true. A good example would be the invariants of a data structure like RB-tree. Actually, in ConcurrentHashMap
of JDK8, there are a few such meaningful asserts for the TreeNodes
.
Sometimes, the check is not really expensive, but at the same time, you're pretty sure, it'll pass. In my code, there's e.g.,
assert Sets.newHashSet(userIds).size() == userIds.size();
where I'm pretty sure that the list I just created has unique elements, but I wanted to document and double check it.