Safe Programming

by Shabbir on August 22, 2008

in C/C++

Once doing Code Review I found a code snippet like this and so thought would share my experience here.

CDialog *SomeDialog = new CMyDialog(<<Some Param>>);
SomeDialog->SomeFunction(this);

In the above code we always assume that SomeDialog is always allocated with the CMyDialog class pointer but that may not be the case always and so its always better to have the code as follows.

CDialog *SomeDialog = new CMyDialog(<<Some Param>>);
if(SomeDialog != NULL)
{
    SomeDialog->SomeFunction(this);
}

and it would always make your programming effort safer and subject to less crashes.

Sharing is Caring...

Other Related Posts:

  1. How some of the programming languages are named
  2. Elance Profile Widget
  3. Follow me On Google Buzz
  4. Aweber Form Builder
  5. Read From File Using StreamReader Class

{ 5 comments… read them below or add one }

Paanchajanyadharaaya August 31, 2009 at 4:56 am

Thank you for your help!

Reply

Teeth Whitening September 4, 2009 at 12:17 am

Hi! I`m totally disagree with you point.

Reply

Teeth Whitening September 27, 2009 at 4:37 am

Hi! I`m totally disagree with you point.

Reply

techgeek4you January 31, 2010 at 4:24 pm

this is also known as defensive programming…

Reply

Shabbir Bhimani February 1, 2010 at 3:23 am

I do not agree on this because its safe and not defensive. I am making it safe from case but defensive may not be the right word.

Reply

Leave a Comment

Spam protection by WP Captcha-Free

Previous post:

Next post:

    About the Author

  • author photo

    My Name is Shabbir Bhimani and I am developer by profession in the field of applications, web and database. Currently doing full time online marketing and like to share my experiences on how you can make money online @ CodeItWell.com. Read more ...


    See how you can get in touch with me.