www.webdeveloper.com
Results 1 to 7 of 7

Thread: [RESOLVED] Are brackets necessary with 1-line "if" statements?

Hybrid View

  1. #1
    Join Date
    Dec 2008
    Posts
    488

    resolved [RESOLVED] Are brackets necessary with 1-line "if" statements?

    I was validating some JS using JSLint, and it returned the error:
    Problem at line 15 character 45: Expected '{' and instead saw '$$n'.
    on the code:
    Code:
    else if($$gauge==$$gauge.match($$numOverZero)) $$n=1-parseInt($$gauge.charAt(0),10);
    I was under the impression that curly brackets were not needed for one-line "if" statements, and I've long done things this way. The ECMA specs (pp 71-72) shows it in the format that I used if ( Expression ) Statement else Statement, and I've never had any problems until I used this validator (which comes recommended).

    What's the deal-y-o?

  2. #2
    Join Date
    Aug 2007
    Posts
    3,767
    Do you have to put them in, no. Should you, probably yes. In nearly all cases, it is better to put them in all the time, even though it is not necessary. Validators like that will always recommend that you do, and so do I. It is only two bytes, and saves confusion down the line.

  3. #3
    Join Date
    Dec 2008
    Posts
    488
    It's not that I don't like using the bytes. I just like to keep my code as clean as possible, and I normally like to give the brackets their own lines to I can more easily find what's inside them. In cases like this, it becomes overkill.

    So, I'll start using them!

  4. #4
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by jamesbcox1980 View Post
    and I normally like to give the brackets their own lines to I can more easily find what's inside them.
    JavaScript's automatic semi-colon insertion and valid empty statements make that practice a very bad idea for many constructs.

    it may look nice, but it's not stable.
    review crockford's "elements of javascript style" for a full explanation.

  5. #5
    Join Date
    Dec 2008
    Posts
    488
    Ok, thanks you two. I definitely appreciate it. It's funny how you learn a style from people and then find out it's not correct when you get into serious applications!

  6. #6
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by jamesbcox1980 View Post
    Ok, thanks you two. I definitely appreciate it. It's funny how you learn a style from people and then find out it's not correct when you get into serious applications!
    well, for java or php, the syntax conventions are different.
    i think that since a lot of coders learned one of those languages before javascript, they get used to a function being formatted a certain way, and bring those good habits into javascript, where they are bad habits.

  7. #7
    Join Date
    Dec 2008
    Posts
    488
    Actually, it's funny you say that. I learned a little PHP before I started learning JS, so maybe that's where I got that from.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles