65.9K
CodeProject is changing. Read more.
Home

"if" with no code block parenthesis

starIconstarIconstarIconstarIconstarIcon

5.00/5 (5 votes)

Feb 10, 2011

CPOL
viewsIcon

23752

Avoid using "if" with no code block parenthesis

Though it is supported, You should not use "if" without parenthesis for code block.

You should use:

MIDL
if(condition)
{
// Your code 
}

and you should not use:

MIDL
if(condition)
// Your code

One of my colleagues encountered the problem due to the unparenthesized "if".

The story goes like this:

His code was like this:

MIDL
if(condition)
   System.out.println("This is log message to be printed on Console");

for( int i = 0; i < 10; i ++ )
{
    //Some important Code 
}

For final release, he globally replaced the System.out.println with //System.out.println so that all SOP statements would be commented.
But due the use of unparenthesized "if", the for loop went inside the if which was not desirable.

and the product started crashing.....

So the situation was ....Product was running properly if we keep all SOP.

After scanning the code throughly, we found out the culprit .....if with no code block parenthesis :omg:

"if" with no code block parenthesis - CodeProject