'idiomatic' porting of c and or c++ code that does NULL checking

monarch_dodra via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Aug 23 08:00:16 PDT 2014


On Saturday, 23 August 2014 at 10:33:02 UTC, nikki wrote:
> A good to know! thanks.
> I'd still be interrested to see the idiomatic D version of that 
> function, what would that depend on ?

Honestly, I wouldn't change it much. If it didn't throw 
exceptions before, then it probably would have trouble handling 
them now.

What I *would* do is use multiple returns though. This style of 
if/else invariably leads to the dreaded "diagonal line of death" 
(http://geekandpoke.typepad.com/geekandpoke/2009/12/geek-for-dummies-chapter-2.html)

Once you've done that, any resource you'd have otherwise needed 
to Un-conditionally release, I'd do with a scope.

bool init()
{
     //Initialize SDL
     if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
     {
         printf( "SDL could not initialize! SDL_Error: %s\n",
SDL_GetError() );
         return false;
     }

     //Create window
     gWindow = SDL_CreateWindow( "SDL Tutorial",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH,
SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
     if( gWindow == NULL )
     {
         printf( "Window could not be created! SDL_Error:
%s\n", SDL_GetError() );
         return false;
     }

     //Get window surface
     gScreenSurface = SDL_GetWindowSurface( gWindow );

     return true;
}

Now, once you've adopted this kind of style, migrating to 
exceptions (should you so wish to do so) should much easier.


More information about the Digitalmars-d-learn mailing list