Mateusz Loskot :: hacking on, working out, living up

Initialise in condition, not assign

30 Sep 2011 | mloskot

I’ve read a lot of code polluted with assignments in condition:

T* p;
if (p = source())
{
    ...
}
else
{
    ...
}

Presumably, it is preferred by someone who is really after shortcuts in code. If you must cut short, reconsider it and at least write this:

if (T* p = source())

It is a perfect shortcut through several features at once: initialisation, condition and scope encapsulation. The scope of p is narrowed to the if-else condition only:

if (T* p = source())
{
    ...
}
else
{
    ...
}

And, it feels and works like using statement in C#. Perhaps that will convince C/C++ legacy crowd ;-)

Thanks to Jonathan Wakely from ACCU for inspiring discussion(s).

Fork me on GitHub