Javascript coding style recommendation: in return and throw statements use only simple variables

My collegue told me that placing the opening curly brace at the end of the line  is safer than at the beginning of the new line, because it prevents some hard-to-debug errors, related to Automatic Semicolon Insertion and  pointed me to the article Basic JavaScript Part 6: Automatic Semicolon Insertion. The article has the recommendation
 Trying to outline curly braces at the end of the line can save you some headaches in case a semicolon is forgotten somewhere in the code.
The statement  is misleading and even incorrect(the problem unrelated to forgotten semicolon).
The article has comments of Elijah Manor with link to JavaScript Semicolon Insertion, that properly explains the issue.

An Expression in a return or throw statement should start on the same line as the return or throw token. A postfix ++ or -- operator should appear on the same line as its operand. A Identifier in a break or continue statement should be on the same line as the break or continue token.


I really prefer C# style of block notation where new block starts  with curly bracket on a new line and apply it to JavaScript code as well.(You need to untick default setting for curly brackets in Visual Studio Tools->Options-Click Text Editor -> JavaScript  -> Formatting and it should be consistent for all development team).
The restriction above can be avoided by the following coding recommendation.
In return and throw statement use only simple variables and keep them on the same line as the statement:
GOOD:

var ret =

   {   

       shoeSize: 48
   };

return ret;

BAD:    
   return   //WRONG
   {         

 shoeSize: 48
   };

 
 
 
posted @ Saturday, January 14, 2012 11:15 AM
Print

Comments on this entry:

# re: Javascript coding style recommendation: in return and throw statements use only simple variables

Left by maquinas de coser at 3/20/2012 11:05 PM
Gravatar
many thanks for this recommendation, good well for me.

Your comment:



(not displayed)

 
 
 
 

Live Comment Preview:

 
«December»
SunMonTueWedThuFriSat
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910