I have some methods that return a value (or object) if all went as planned, otherwise return null (something went wrong).
For example, DataTable dt = DoSomething();
If something blew up in DoSomething()
the return value would be null
and dt
would be set to null
.
There are other cases where I'm testing for a value and then setting a variable to the value if the test value is not null. This doesn't feel right. I'm calling the same method twice.
For example, if (String.IsNullOrEmpty(getAddresss())) {Do Stuff;} If I declare a variable before the test, the code could blow up when it gets set. If I test, then set it seems like I'm duplicating work.
What is the preferred way to test for and handle null values?