-
5 Feb 2013 8:34 AM #1
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:
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.Code:if(...) { <block code> }
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
-
7 Feb 2013 8:09 AM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,656
- Vote Rating
- 435
- Answers
- 3109
This is really a matter of developer style. I personally keep it in one line:
And most do that as well. I do see a difference in the else so I see this:Code:if (...) { .... }
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.Code:if (...) { ... } else if (...) { ... } else { ... }Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
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 almost in print!
When posting code, please use BBCode's CODE tags.
-
7 Feb 2013 8:21 AM #3
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:
in most js implementations will be equivalent toCode: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 compactCode:function foo() { return; { bar: "foobar" }; }
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


Reply With Quote