[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?
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.
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.
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.