One can do this:
case WM_COMMAND:
if (WORD wNotifyCode = HIWORD(wparam))
{
...
}
And one can do this:
case WM_COMMAND:
{
WORD wNotifyCode = HIWORD(wparam);
if (wNotifyCode > 1) {
...
}
}
But one cannot do:
case WM_COMMAND:
if ((WORD wNotifyCode = HIWORD(wparam)) > 1)
{
...
}
Using a for statement here I think is misleading:
case WM_COMMAND:
for (WORD wNotifyCode = HIWORD(wparam); wNotifyCode > 1; wNotifyCode = 0)
{
...
}
Because it looks a lot like a loop is happening – and the poor schmuck who comes after me has to decipher this garbage.
But is there no syntactic construct which combines the elegance of an if-statement that includes a local variable declaration with the ability to test its value for something other than zero?
Sometimes readability and maintainability is more important then a line of code saved.
IF you need the local variable at all then by all means introduce it explicitly in this case and maybe introduce an additional scope if you want it limited – but you should also consider if maybe you can just live with using the HIWORD macro in a couple places – this way you don’t need any tricks at all.