I have a Windows service with a fluent interface like this:
aRequest = Repository.getRequest()
.createProcess()
.validate();
Sometimes getRequest()
could return a null
value and this would cause an error in createProcess()
. I could banally split getRequest()
from createProcess()
, but if I wouldn't do that what way should I follow, what way is better:
Check if request (
this
) is null and in the case return null:if(this is null) return null
I could do this check in every method next to
getRequest()
. At the endaRequest
will benull
.Throw an exception if
createProcess()
method receive anull
value:if(this is null) throw new NullRequestException();
PRO of the second way: Only second method need a check, independently of the number of method in the chain.
CON of the first way: Every method in the chain needs a check
Now the question: Is second way a bad use of exception concept, since could be normal the absence of request sometimes?
null
is considered harmful and there are better alternatives. If anOptional
type isn't your cup of tea, then throw an exception from thegetRequest
method. – Doval Apr 30 '14 at 15:09