Jump to content
TAB

use correct & compact syntax

Recommended Posts

The tutorials should use strict syntax (as a model for those who learn by example). For example, infunction myFunction(p1, p2) { return p1 * p2; // the function returns the product of p1 and p2}the semicolon is useless. The novice who does not know that the semicolon is a separator (not a terminator) will be lead to believe that it is required after the last statement.Furthermore, the equivalent formfunction myFunction (p1, p2) {return p1*p2}//returns the product of p1 and p2is more compact AND more legible. Putting a single brace on the final line may be preferred by some (even though it gobbles editing-window space up), but it should not be used so systematically that the novice will think it is a syntactical requirement.

Share this post


Link to post
Share on other sites

Having code on separate lines is more legible. It's a good practice to terminate all your lines with a semi-colon so that when you write a new line after it you don't forget it.

 

If you want to get technical, Javascript will run just fine with no semi-colon on any of the lines.

Share this post


Link to post
Share on other sites

Vastly experienced people recommend using semi-colons, and compact code is rather pointless -- since you'll run the final code through a minifier anyway.

Share this post


Link to post
Share on other sites

but it should not be used so systematically that the novice will think it is a syntactical requirement.

I'd much rather have the novice assume that well-indented code is a syntactical requirement versus assuming that single-line code is a syntactical requirement. This obviously assumes that the novice will assume that anything they see is a requirement, which is the assumption that you seem to make.Surely you aren't suggesting that something like this:
function changeVideoQual(){  if(videoQuality == 'low'){videoQuality = 'high';$('#nav_quality').html('<i class="icon-cog"></i> low band')}  else{videoQuality = 'low';$('#nav_quality').html('<i class="icon-cog"></i> high band')}  if(runningIntro === true || atMenu){alert('Video Quality has been changed.')}  else{replayPage()}}
is more legible and maintainable than this:
function changeVideoQual(){    if(videoQuality == 'low'){        videoQuality = 'high';        $('#nav_quality').html('<i class="icon-cog"></i> low band');    }else{        videoQuality = 'low';        $('#nav_quality').html('<i class="icon-cog"></i> high band');    }    if(runningIntro === true || atMenu){        alert('Video Quality has been changed.');    }else{        replayPage();    }}
If that's what you're suggesting, then you're probably the only one. Legibility is subjective, after all, and many programmers prefer the second style. It even has a name. Many programmers prefer K&R style.http://en.wikipedia.org/wiki/Indent_styleThe first form of "putting everything in a block on one line" doesn't have a name.
  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...