Avoiding That Null Reference!
- by TheJuice
A coworker had this conversation with another of our developers. Names have been changed to protect the guilty.
Clueless: hey!
Clueless: I am using the ?? operator for null check below
Nice Guy: hey
Clueless:
FundLoanRequestBoatCollateral boatCollateral = request.BoatCollateral ?? null;
Nice Guy: that's not exactly how it works
Clueless: I want to achive:
FundLoanRequestBoatCollateral boatCollateral = request.BoatCollateral != null ? request.BoatCollateral : null;
Clueless: isnt that equal to:
FundLoanRequestBoatCollateral boatCollateral = request.BoatCollateral ?? null;
Nice Guy: that is functionally equivalent to
FundLoanRequestBoatCollateral boatCollateral = request.BoatCollateral
Nice Guy: you're checking if it's null and if so setting it to null
Clueless: yeah
Clueless: if its null I want to set it to null
Nice Guy: if it's null then you're already going to set it to null, no special logic needed
Clueless: I wanted to avoid a null reference if BoatCollateral is null
The sad part of all of this is that "Clueless" has been with our company for years and has a Master's in Computer Science.