|
-
Sep 7th, 2005, 08:25 AM
#1
Thread Starter
Frenzied Member
[JavaScript] Layout/Indenting Best Practice
Coming from a VB background Im not used to all these curly braces
Is there a standard/accepted way to layout the code with curly braces to make things easy to read?
e.g. like this
PHP Code:
function update(){
var intDUTs = document.frmUpload.cboDUTs.value;
try{
var strEmail = document.frmUpload.txtEmail.value;
}
catch(err){
// alert("Email does not exist");
}
}
or like this?
PHP Code:
function update()
{
var intDUTs = document.frmUpload.cboDUTs.value;
try
{
var strEmail = document.frmUpload.txtEmail.value;
}
catch(err)
{
// alert("Email does not exist");
}
}
Or something different?
-
Sep 7th, 2005, 10:42 AM
#2
Fanatic Member
Re: [JavaScript] Layout/Indenting Best Practice
Personally, I prefer your second method. It makes it far easier to match your braces and to determine what level you are at in a loop or if block.
But all the java/javascript editors I have used, all use your first method by default.
-
Sep 7th, 2005, 10:59 AM
#3
Re: [JavaScript] Layout/Indenting Best Practice
There's no standard method. It's a matter of personal taste, although you can start quite a war trying to argue their respective merits.
Personally I put the { on a new line for functions and switch statements, and on the same line for everything else. I also put the catch and else on the same line as the closing }.
Many people use automatic code shrinkers before putting JavaScript in production. These tools remove all comments and meaningless whitespace, thus saving a few bytes in file size.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 7th, 2005, 12:03 PM
#4
Fanatic Member
Re: [JavaScript] Layout/Indenting Best Practice
I prefer the first method as it saves a line and you can easily spot where a function ends..as for matching braces I've found that a good editor, which highlights matching braces and perorms auto-indenting, is invaluable in the time it saves.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|