This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I've been reading Effective Java book. It's a very good book, indeed. Is it actually necessary to check every parameter for a null and then throw a NullPointerException if the parameter is null? It seems that a method will be cluttered with a lot of if conditions if we need to do so.
Please share your opinion. Thanks
SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
Yup, I agree with that, and that's what I normally do. But according to the book, it's actually a good practice to check for a null instead of letting the parameter throws a NullPointerException as a result of referencing a null. That way, the debugging will be easier.
I think what Bloch means is that you need to avoid the case where you don't throw an NPE; if your method passes the null to something else which passes the null to something else which passes the null to something else which puts it in some record whence it is retrieved and throws an Exception . . .
You will have no end of difficulty working out what happened.