Geeks With Blogs

Michael Freidgeim's Blog MS .Net Development
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 on Saturday, January 14, 2012 11:15 AM CSS/DHTML/JavaScript | Back to top


Comments on this post: Javascript coding style recommendation: in return and throw statements use only simple variables

# re: Javascript coding style recommendation: in return and throw statements use only simple variables
Requesting Gravatar...
many thanks for this recommendation, good well for me.
Left by maquinas de coser on Mar 20, 2012 11:05 PM

Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net | Join free