Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
F

FatBuddha

@FatBuddha
About
Posts
3
Topics
0
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • Avoid return statement in the middle - horror or not?
    F FatBuddha

    I can assure you that it isn't! This technique is usually called a do-once block, and commented as such to make it really obvious. Look again at just the 'loop' bit and see if you think that this block of code will still run more than once:

    do{//once

    }while(0);

    What you basically end up with, is a block of code that you can jump to the end of early with a 'break', without having to resort to a 'goto'. That block of code isn't a loop, as it will never run more than once. Get it?

    None

    The Weird and The Wonderful question

  • Avoid return statement in the middle - horror or not?
    F FatBuddha

    Doh - I was looking at the second code listing. Assuming that there is lots of other stuff going on between the flag tests, and there is a need for clean-up code (thus don't want to use return), I'd use the same system with a flag:

    //Figure out what to do...
    bool doSomething = false;
    do{
    if(!flagA)break;
    if(!flagB)break;
    if(!flagC)break;
    if(!PromptUser())break;
    doSomething = true;
    }while(0);

    //...then do it
    if(doSomething)
    DoSomething();
    else
    DoOtherThing();

    None

    The Weird and The Wonderful question

  • Avoid return statement in the middle - horror or not?
    F FatBuddha

    Assuming that there is more processing going on than this example indicates, I would use a do-while-false loop:

    do{
    if(!flagA)break;
    if(!flagB)break;
    if(!flagC)break;
    if(!PromptUser())break;
    }while(0);

    DoOtherThing();

    This is the sort of thing that I routinely do with COM programming where I need to check the success of pretty much every single method call. It means that I don't have a crazy amount of indenting, and it's much cleaner for me or somebody else to understand and modify later. I can highly recommend it.

    None

    The Weird and The Wonderful question
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups