Results 1 to 4 of 4

Thread: [JavaScript] Layout/Indenting Best Practice

  1. #1

    Thread Starter
    Frenzied Member agmorgan's Avatar
    Join Date
    Dec 2000
    Location
    Lurking
    Posts
    1,383

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

  2. #2
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Three Anchor Bay, Cape Town, South Africa
    Posts
    769

    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.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.

  4. #4
    Fanatic Member
    Join Date
    Jan 2005
    Location
    In front of this pc.
    Posts
    580

    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
  •  



Click Here to Expand Forum to Full Width