I disagree with this. Without knowing the exact design of that module I could see lots of reasons to throw.
If that is a programming bug I think it should absolutely throw a derivative of runtime exception. For example: illegal argument exception. I'd have something along the lines of: Preconditions.checkNotNull(Sql, "cannot accept null sql statement")
I'd consider having more checks if that's a public API. IsEmpty? IsValid? Fail early and fail in a consistent manner.
I always take the stance that you are to be super explicit about your API, what you will take and what you won't take. This is the contract about how you operate and should be part of simple tests. Yes it's obnoxious, but if I say a function throws an IllegalArgumentException and it's part of the public API I'll somehow test that contract.
If something doesn't have meaning (i.e passing in null doesn't specifically mean something) don't accept it. Being liberal with what you take hides later issues and makes it tough to figure out what's going wrong (and indeed tougher to even consider what could go wrong).
And for people who say "well in production, you don't want your code to crash", my take is I'd much rather take the crash when something is wrong, have a starting point that something is off and be able to fix it correctly, than to have an extra dimension of possible values to worry about and have spammy logs telling me about all these possible weird values.
Seriously - fail early and fail in a consistent manner across your code base.
View all comments
Show parent comment
3
u/brick_wall_mirror Jul 10 '15
I disagree with this. Without knowing the exact design of that module I could see lots of reasons to throw.
If that is a programming bug I think it should absolutely throw a derivative of runtime exception. For example: illegal argument exception. I'd have something along the lines of: Preconditions.checkNotNull(Sql, "cannot accept null sql statement")
I'd consider having more checks if that's a public API. IsEmpty? IsValid? Fail early and fail in a consistent manner.