Objective C style nil in java?

Posted by Usman Ismail on Programmers See other posts from Programmers or by Usman Ismail
Published on 2012-11-14T07:52:45Z Indexed on 2012/11/14 11:18 UTC
Read the original article Hit count: 590

Filed under:
|

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.

© Programmers or respective owner

Related posts about java

Related posts about objective-c