Click here to Skip to main content
15,919,121 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: code comments Pin
Sander Rossel26-Jul-16 21:24
professionalSander Rossel26-Jul-16 21:24 
GeneralRe: code comments Pin
ZurdoDev27-Jul-16 1:53
professionalZurdoDev27-Jul-16 1:53 
GeneralRe: code comments Pin
Sander Rossel27-Jul-16 4:05
professionalSander Rossel27-Jul-16 4:05 
GeneralRe: code comments Pin
Slacker00727-Jul-16 5:33
professionalSlacker00727-Jul-16 5:33 
GeneralRe: code comments Pin
KC@CahabaGBA28-Jul-16 3:25
KC@CahabaGBA28-Jul-16 3:25 
GeneralRe: code comments Pin
Slacker00728-Jul-16 3:48
professionalSlacker00728-Jul-16 3:48 
GeneralRe: code comments Pin
CDP180226-Jul-16 21:26
CDP180226-Jul-16 21:26 
GeneralRe: code comments Pin
BillWoodruff26-Jul-16 22:02
professionalBillWoodruff26-Jul-16 22:02 
Well, gosh ... if I only had a Baht (about 2.86 US cents) for every time I've written: if(whatever != null) ...

However, is it, perhaps, useful to distinguish two scenarios:

1. checking for null when there's no need to throw an error if the whatever is null, and, the code context makes it absolutely clear what's going on.

2. checking for null when you absolutely should throw an error on null

I recognize there are "purists" who would like to see you throw errors in all cases.

And, how about this: disclaimer: this is an experiment I did a while ago; I am not indirectly expressing the opinion that anyone should use something like this:
C#
public static class ErrorExtensions
{
     // non-generic version of 'IsNonNull omitted here

     // note: uses optional compiler directive 'ErrorsToConsole

     public static bool IsNonNull<T>(this T refobj, bool throwerror = false, string message = null)
    where T : class
    {
        if (refobj == null)
        {
            string mess = string.Format("instance of {0} is null: {1}",typeof(T).Name, message);
    
            if (throwerror)
            {
                throw new NullReferenceException(mess);
            }
            else
            {
#if ErrorsToConsole
                Console.WriteLine(mess);
#endif
            }
    
            return false;
        }
    
        return true;
    }
}

«There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008


modified 27-Jul-16 4:13am.

GeneralRe: code comments Pin
Slacker00726-Jul-16 23:56
professionalSlacker00726-Jul-16 23:56 
GeneralRe: code comments Pin
Michael Martin27-Jul-16 0:03
professionalMichael Martin27-Jul-16 0:03 
GeneralRe: code comments Pin
BillWoodruff27-Jul-16 3:38
professionalBillWoodruff27-Jul-16 3:38 
GeneralRe: code comments PinPopular
F-ES Sitecore26-Jul-16 22:58
professionalF-ES Sitecore26-Jul-16 22:58 
GeneralRe: code comments Pin
charlieg26-Jul-16 23:54
charlieg26-Jul-16 23:54 
GeneralRe: code comments Pin
Dan Neely27-Jul-16 2:51
Dan Neely27-Jul-16 2:51 
GeneralRe: code comments Pin
Shameel27-Jul-16 0:40
professionalShameel27-Jul-16 0:40 
GeneralRe: code comments Pin
CDP180227-Jul-16 2:49
CDP180227-Jul-16 2:49 
GeneralRe: code comments Pin
#realJSOP27-Jul-16 1:31
professional#realJSOP27-Jul-16 1:31 
GeneralRe: code comments Pin
CDP180227-Jul-16 2:58
CDP180227-Jul-16 2:58 
GeneralRe: code comments Pin
Richard Deeming27-Jul-16 3:09
mveRichard Deeming27-Jul-16 3:09 
GeneralRe: code comments Pin
Nish Nishant27-Jul-16 4:49
sitebuilderNish Nishant27-Jul-16 4:49 
GeneralRe: code comments Pin
TheGreatAndPowerfulOz27-Jul-16 8:10
TheGreatAndPowerfulOz27-Jul-16 8:10 
GeneralRe: code comments Pin
Nish Nishant27-Jul-16 8:18
sitebuilderNish Nishant27-Jul-16 8:18 
GeneralRe: code comments Pin
kalberts28-Jul-16 2:12
kalberts28-Jul-16 2:12 
GeneralRe: code comments Pin
kalberts28-Jul-16 2:59
kalberts28-Jul-16 2:59 
GeneralRe: code comments Pin
Clifford Nelson27-Jul-16 7:39
Clifford Nelson27-Jul-16 7:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.