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 nullI could do this check in every method next to
getRequest(). At the endaRequestwill benull.Throw an exception if
createProcess()method receive anullvalue: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?
nullis considered harmful and there are better alternatives. If anOptionaltype isn't your cup of tea, then throw an exception from thegetRequestmethod.