Elegant ways to handle if(if else) else

Posted by Benjol on Programmers See other posts from Programmers or by Benjol
Published on 2011-11-30T10:21:32Z Indexed on 2011/11/30 10:31 UTC
Read the original article Hit count: 271

Filed under:

This is a minor niggle, but every time I have to code something like this, the repetition bothers me, but I'm not sure that any of the solutions aren't worse.

if(FileExists(file))
{
    contents = OpenFile(file); // <-- prevents inclusion in if
    if(SomeTest(contents))
    {
        DoSomething(contents);
    }
    else
    {
        DefaultAction();
    }
}
else
{
    DefaultAction();
}
  • Is there a name for this kind of logic?
  • Am I a tad too OCD?

I'm open to evil code suggestions, if only for curiosity's sake...

© Programmers or respective owner

Related posts about coding-style