PDA

View Full Version : JavaScript condition question



dukevn
07-17-2008, 08:32 PM
I am just a novice in javascript and programming, but I like programming a lot. I did not take any classes about programming, and all the basics I have for javascript come from google and... here at the forum. While it is not always the case, but most of the scripts here were written in such a, what to say, dense format (in my opinion) that it is really difficult, especially newbie like me, to follow and understand them. In order to understand any script, I usually have to reformat them in an easier format to read, for example:

statement1;
statement2;
if (conditions) {
statement3;
}
ie adding extra semicolon ";", brackets "{,}", tabs and spaces (I prefer tabs) and make all the lines as clear as possible so that I can follow them.

While I not only totally respect all people here with all their works and scripts but also learn a lot from reading their codes and from the forum, I wonder:

a) when and why we need semicolon ";", comma "," or even none? what should I normally put at the end of lines when coding? When I reformat the script, I always put (or replace if needed) semicolon in the end of each statement. Am I doing right or not?

b) is this the typical style of javascript scripts when people write them? when I read some other scripts (php, java, c etc...) I usually see the nicer format than formats of the scripts here.

Thanks,

D.

molendijk
07-17-2008, 08:45 PM
My guess is that you are primarily referring to javascript-conventions. If that's the case, here (http://www.w3schools.com/JS/js_intro.asp)'s a good (basic) tutorial.
===
Arie Molendijk

Twey
07-18-2008, 09:15 AM
No, that is not a good tutorial (few of W3Schools' works are, I fear; people have been complaining at them in their forums for years, but they pointedly ignore all criticisms of their terrible and often entirely misleading tutorials). http://www.howtocreate.co.uk/tutorials/javascript/semicolons addresses one of your issues. For the other, I can offer no better explanation than that Javascript is a language commonly not taken seriously, so people writing it do not invest a proper amount of time into making it readable. This is a big mistake, especially with complex scripts -- Javascript's functional nature and the deeply-nested scopes that result from it in good code can become very hard to read, making proper indentation and source-code formatting even more important than in less-powerful, imperative languages such as Java, C, or PHP, the latter of which also suffers from this problem in a lot of newbie code.

Braces, though, it must be said, can often detract from the readability of code if over-used. I believe that they should be used as sparingly as possible, although opinions and site code regulations may differ.

The scripts in DD's archives, while intermittently inspired, should not by any means be taken as a measure of good code quality. I had a go at reforming them, but it is rather a daunting task, and I fear other obligations took priority. Please feel free to take up the torch — any improvements are welcome.

jscheuer1
07-18-2008, 04:08 PM
I agree with what Twey wrote (nice to see you again). I would add that, just as an example using the one you offered:



statement1;
statement2;
if (conditions) {
statement3;
}

That could be written:


statement1;
statement2;
if (conditions)
statement3;

And then one might be tempted to do this:


statement1;
statement2;
if (conditions);
statement3;

But it will cause an error, at least in browsers that don't error correct it for you while parsing it.

The semicolon is for the end of a line of code (not necessarily a line of text in the code), usually a statement, so this would be fine:


statement1;
statement2;
if (conditions) {
statement3;
};

The comma is for separating common things like a bunch of arguments, a line of variable declarations, or a line of property assignments, items in an array. When I say line here, again I mean a code line, not necessarily a line of text in the code.

dukevn
07-18-2008, 06:48 PM
No, that is not a good tutorial (few of W3Schools' works are, I fear; people have been complaining at them in their forums for years, but they pointedly ignore all criticisms of their terrible and often entirely misleading tutorials). http://www.howtocreate.co.uk/tutorials/javascript/semicolons addresses one of your issues. For the other, I can offer no better explanation than that Javascript is a language commonly not taken seriously, so people writing it do not invest a proper amount of time into making it readable. This is a big mistake, especially with complex scripts -- Javascript's functional nature and the deeply-nested scopes that result from it in good code can become very hard to read, making proper indentation and source-code formatting even more important than in less-powerful, imperative languages such as Java, C, or PHP, the latter of which also suffers from this problem in a lot of newbie code.
Thanks for the link, Twey. As advised in the article, it is useful to always use semicolon, even if not needed, and that is what I have been doing. Just wonder why javascript is so different than, for example, php. PHP accepts semicolon only (as far as I know of).
Braces, though, it must be said, can often detract from the readability of code if over-used. I believe that they should be used as sparingly as possible, although opinions and site code regulations may differ.Not in the case of loop inside another loop, at least for me. I always have difficulties to follow such situation, to understand what statement belongs to which loop.
The scripts in DD's archives, while intermittently inspired, should not by any means be taken as a measure of good code quality. I had a go at reforming them, but it is rather a daunting task, and I fear other obligations took priority. Please feel free to take up the torch any improvements are welcome.
I am at the level of learning and understanding codes, and then trying to modify, corporate different codes together for my own purpose. I dont think I am good and knowledgeable enough to make any improvements, especially the browsers' compatibilities that the codes should have to be cross-browser (meaning to be applicable and widely used). If, in the future, I feel that I am capable, then I will not hesitate to take the torch :D