Click here to Skip to main content
Click here to Skip to main content

"if" with no code block parenthesis

By , 12 Feb 2011
 
Though it is supported, You should not use "if" without parenthesis for code block.
 
You should use:
if(condition)
{
// Your code 
}
 
and you should not use:
 
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:
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:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

PrafullaVedante
Software Developer
India India
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralReason for my vote of 5 You learn something new every day. ...memberZamirF15 Feb '11 - 9:17 
GeneralIndivara is completely correct - the issue is not so much th...memberPete Goodsall14 Feb '11 - 2:37 
GeneralReason for my vote of 5 AGREED!! I don't even like the if's...memberJoe119386911 Feb '11 - 5:41 
GeneralHave to agree with Indivara on this.memberAlbert Holguin10 Feb '11 - 12:43 
GeneralGood.membershakil030400310 Feb '11 - 3:34 
GeneralI see the danger here, but the bigger issue seems to be glob...subeditorIndivara9 Feb '11 - 22:40 
QuestionIS THERE ANY RULE ?membersudheer muhammed10 Feb '11 - 0:40 
AnswerRe: IS THERE ANY RULE ?memberAbdur Rehman Raza Khan - dontumindit13 Feb '11 - 11:44 
No there is no rule, but the above situation is common once we try to do some change in hurry, so its good to use if with brackets from start to avoid such time consuming effort at later stages

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 13 Feb 2011
Article Copyright 2011 by PrafullaVedante
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid