Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Bracketing loops and if statements (Java)

  • 08-12-2009 12:06am
    #1
    Registered Users, Registered Users 2 Posts: 428 ✭✭


    Hi, I was just wondering if the only reason for bracketing statements and loops was for clarity, i bracket them but noticed in some sample code that alot of people dont.

    eg, I would would write an if statement like this:

    if(a+b = c){
    System.out.println(c + "is the sum of " + a + " and " + b);
    }

    But other people write like this:

    if(a+b = c)
    System.out.println(c + "is the sum of " + a + " and " + b);


    Just wondering if there is a real reason for using one over the other or if its only personal preference, thanks in advance


Comments

  • Registered Users, Registered Users 2 Posts: 3,766 ✭✭✭Reku


    If the command to be followed if the condition applies is only one line you don't need brackets, more than one line you do.

    e.g.
    if x is true
    do this;
    else
    do this;


    if y is true
    {
    do this;
    and this;
    }


    TBH I still use brackets even for one line commands as I find it easier to read.


  • Registered Users, Registered Users 2 Posts: 9,211 ✭✭✭Royale with Cheese


    Yeah I was wondering this myself as recently I was using some code somebody else had written without brackets. I agree with farohar, I'd always use them for better clarity.


  • Registered Users, Registered Users 2 Posts: 21,263 ✭✭✭✭Eoin


    The only time I might not bracket my code is if I know I would never have to add any debugging code later, and then I would put it on one line rather than over two lines.
    if(a+b = c) System.out.println(c + "is the sum of " + a + " and " + b);
    


  • Registered Users, Registered Users 2 Posts: 428 ✭✭Joneser


    Thanks for clearing that up, i thought it was just for clarity myself but wanted to make sure, cheers :)


  • Closed Accounts Posts: 5,082 ✭✭✭Pygmalion


    They ARE needed if there's more than one line to be executed in the if statement.

    And the way I see it, if you're going to need them sometimes why not put them in all the time instead of being inconsistent about it?
    Any time it's ever mentioned everyone seems to agree it's a lot easier to read with the brackets in.


  • Advertisement
  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/


    eoin wrote: »
    The only time I might not bracket my code is if I know I would never have to add any debugging code later, and then I would put it on one line rather than over two lines.
    Same here, although if the line gets a bit long, I'll do a line-break and indent the next line... I find it pretty clear.
    if(whatever)
        blah = something;
    else
        blah = yadda;
    


  • Registered Users, Registered Users 2 Posts: 21,263 ✭✭✭✭Eoin


    I would use brackets in your example - I only use a one liner for the simplest of statements.


  • Registered Users, Registered Users 2 Posts: 569 ✭✭✭none


    eoin wrote: »
    The only time I might not bracket my code is if I know I would never have to add any debugging code later, and then I would put it on one line rather than over two lines.
    if(a+b = c) System.out.println(c + "is the sum of " + a + " and " + b);
    

    Just to clarify that it's not about number of lines but rather statements. If only one statement, braces are optional, otherwise - mandatory. The number of lines is irrelevant, really.
    Another thing is that condition (if(a+b = c)) is invalid regardless of the data types used. Single "=" is an assignment operators while double "==" is the relational one (required here).


  • Registered Users, Registered Users 2 Posts: 21,263 ✭✭✭✭Eoin


    none wrote: »
    Just to clarify that it's not about number of lines but rather statements. If only one statement, braces are optional, otherwise - mandatory. The number of lines is irrelevant, really.

    I agree, I was just saying that I always use brackets, unless it happens to be a very simple line of code that would never require debugging and would be very clear to someone else looking at the code.
    none wrote: »
    Another thing is that condition (if(a+b = c)) is invalid regardless of the data types used. Single "=" is an assignment operators while double "==" is the relational one (required here).

    I just copied in the code from the OP.


  • Registered Users, Registered Users 2 Posts: 569 ✭✭✭none


    eoin wrote: »
    I just copied in the code from the OP.
    Sorry for the confusion, of course, it's all for the attention of the OP, I copied your code because it was closest to the end :)


  • Advertisement
  • Closed Accounts Posts: 5,482 ✭✭✭Kidchameleon


    I always use brackets. Makes it nicer to read and makes your code more maintainable. You never know if a one liner could grow into more over the years...


  • Closed Accounts Posts: 1,397 ✭✭✭Herbal Deity


    I used to be in the "always use them" camp. But recently, I think omitting them in certain situations is nice and very readable. Like, when I see:
    if (x)
    {
      y
    }
    
    I think "what was the point of those superfluous braces?".

    *shrug, personal preference I guess.


  • Registered Users, Registered Users 2 Posts: 981 ✭✭✭fasty


    I prefer leaving out the braces for single statements for readability reasons too...
    if(pThing) pThing->SomeFunction();
    

    is easier to read than the following...
    if(pThing)
    {
        pThing->SomeFunction();
    }
    


  • Registered Users, Registered Users 2 Posts: 21,263 ✭✭✭✭Eoin


    I don't think it's much easier to read, but it's a bit neater.

    I do hate reading someone's code when there's something like a loop with no brackets (especially a nested one). Many editors show the start or end bracket when you hover over or click on the corresponding bracket, and I don't know if it's as easy to figure out with no brackets.


Advertisement