1. #1
    Sencha Premium Member
    Join Date
    Apr 2012
    Posts
    41
    Answers
    6
    Vote Rating
    2
    sriram139 is on a distinguished road

      0  

    Default Unanswered: Indentation practice

    Unanswered: Indentation practice


    There are multiple ways one can indent their code, two of them that are commonly used styles are as below:

    1) Start the block on same line with open brace following the last character of that block header:
    Code:
    if(...){
    <block code>
    }
    2) Start the block in new line with open brace in the next line:
    Code:
    if(...)
    {
    <block code>
    }
    Both these approaches have their own pros and cons.
    However, I want to know which does Sencha team prefers the application developers to follow and reason behind using particular style of indentation. Specifically, I want to know if there are any disadvantages in following the second style of indentation as I feel it helps in improving the readability of code than the former.

    Thanks,
    Sriram

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    36,604
    Answers
    3449
    Vote Rating
    818
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    This is really a matter of developer style. I personally keep it in one line:

    Code:
    if (...) {
        ....
    }
    And most do that as well. I do see a difference in the else so I see this:

    Code:
    if (...) {
        ...
    } else if (...) {
        ...
    } else {
        ...
    }
    but also see:

    Code:
    if (...) {
        ...
    }
    else if (...) {
        ...
    }
    else {
        ...
    }
    The 2nd bit I can see useful if you need to add some comments but it just looks weird to me but that's just my code style.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  3. #3
    Sencha User
    Join Date
    Apr 2010
    Posts
    100
    Answers
    1
    Vote Rating
    0
    Dipish is an unknown quantity at this point

      0  

    Default


    Actually, it's a little more than just a matter of style.

    The "same line braces" way is preferred in JavaScript in order to avoid some quirks with Automatic Semicolon Insertion (ASI).

    The classic example is the following:
    Code:
    function foo() {
        return
        {
            bar: "foobar"
        };
    }
    in most js implementations will be equivalent to

    Code:
    function foo() {
        return;
        {
            bar: "foobar"
        };
    }
    and of course will return undefined. Of course not all situations involving curly braces are dangerous like this but for the sake of consistency, always put a brace on the same line. As an added bonus, "same-line braces" is a little more compact

    More reading:
    http://stackoverflow.com/questions/3218756/javascript-braces-on-new-line-or-not
    http://stackoverflow.com/questions/3960518/javascript-formatting-must-braces-be-on-the-same-line-as-the-if-function-etc-ke



Thread Participants: 2

Tags for this Thread