| DHTMLdev.com Guarantee: Code Examples Work |
|
|
|
| Sunday, 08 January 2006 | |
|
If any code example does not work, or if you need help, post in the forums to let me know; use the General forum if you are not sure where to post. Great care is always taken to make sure code examples on this site work. How many times have you read an article or tutorial to find that some example does not work, which costs you hours trying to figure out what you did wrong when, in fact, you didn't do anything wrong? I always test code examples in articles and tutorials I read at other sites and in books, and I personally find so often that the code examples do not actually work. When I first started learning to program, these code example errors only made learning that much harder. How can you learn if the example does not work? See an example of this from another site, which I do not name, and be assured you will not find these types of errors on this site. Also, this article contains a minor discussion of the syntax requirements for a JavaScript condition in an if statement. Example Code Errors from Another SiteCurrently, I am in the process of mastering ActionScript, the scripting language for Flash SWFs, that is very similar to JavaScript. In fact, both JavaScript and ActionScript are based on the same ECMAScript Language Specification, ECMA-262. Since I have mastered JavaScript, ActionScript is very familiar to me, which is why I noticed two errors in a code example of an article I was reading. Here is an example piece of code from a very popular Web development publishing site. There are two errors in this code example. This code from another site has two serious errorsfunction stripSpace (inString) { var outString = ""; for (i = 0; i < inString.length; i++) { if inString.charAt(i) != "" { outString += inString.charAt(i); } } return outString } The fact that this example did not work is inexcusable: the code is short and simple and the errors are glaring and would be immediately obvious, if the code had just been tested as is in the page, by copying and pasting the code and then running it the way a reader would. What are the errors? Take a minute to copy and paste this code example into an HTML file and load it in your browser. If you need help with debugging, read Where Can You Find a JavaScript Debugger?. I'll wait here while you test the code. Ok, now I am sure you found the same two problems that I found. The first problems is a syntax error and the second problem is a human error in coding. Both errors are in line 4 of the code, this line: if inString.charAt(i) != "" {. And here they are:
The syntax error be found using the JavaScript Console in Firefox, as discussed in Where Can You Find a JavaScript Debugger?. The second, though, is a logic error and could be harder to find. The First Error: A Syntax ErrorDue to the first error, the syntax error, this code will not even run. The condition in a JavaScript if statement, as required by the syntax of the language, must be enclosed in parentheses. So the code if inString.charAt(i) != "" is incorrect and should include parentheses around inString.charAt(i) != "", like so, if (inString.charAt(i) != ""). First Error and How to Fix It// This line contains a syntax error // because the ( and ) are missing // around the test condition if inString.charAt(i) != "" { // Here is a new version // with the first error corrected. // But there is still a logic error // which is discussed in the next example if (inString.charAt(i) != "") { The Second Error: A Logic ErrorAnd even after fixing the syntax error, so the code will at least run, due to the second error, the function will return nothing at all, because the test condition for an empty string "" will never be true. Who would ever pass an empty string to the function? The whole point of the function is to take a string, that is not empty, for example "hello world," and remove the spaces. The function is not supposed to check for a non-empty string; it is supposed to check for space strings. But the test case is not checking for a space, which would be " ". It is checking for an empty string, "". So the test will always be false and, consequently, the function will never return anything. Second Error and How to Fix It// Continuing from the corrected version above // with the first error fixed // we still see a second error // which is a logic error // because it checks for an empty string "" // when it should check for a space " " if (inString.charAt(i) != "") { // Here is the corrected version // with "" replaced by " " // which now correctly checks for a space if (inString.charAt(i) != " ") { Even Great Sites Have Errors, But Not DHTMLdev.comIf you did not understand everything I said in the previous paragraph, then you are in luck, because I am pointing out exactly the kind of problems you can avoid, when you rely on the tutorials on this site. The site I took the code example from is actually a great site by a very reputable, in fact the best, Web publishing company on the market. I use their site a lot and I buy and read their books. But like so many sites, they publish code examples that, unfortunately, are oftentimes not tested well enough and do not work. While many such sites do a great public service by publishing free information and articles, the last stumbling block you need while learning is to struggle with examples that do not work. When you learning, you have no idea the code examples are flawed, and you spend valuable time and energy and sanity trying to figure what is wrong, when you could be more productive and you could be having a more enjoyable experience. The DHTMdev.com GuaranteeAll code examples on this site have been tested. Occasional errors do creep in but you can have confidence that the code examples work. |
|
| Last Updated ( Thursday, 23 March 2006 ) |
| < Prev |
|---|











