Objective C style nil in java?
- by Usman Ismail
Objective C has a concept of a nil object which would accept any method call with any parameters without complaining and silently return another nil.
I have used something similar in Java using easymock and mockito for testing. Is there something similar for main line code? Is it even a good idea to have something like this?
One use-case where I am considering using it is with optional metrics. We have codahale metrics counters on which we call mark() every time an event is generated. This is surrounded by an If/then to check for metrics enabled flag. I could just use a nil object as counter and silently accept the mark call if metrics are not enabled.