Javascript/Appcelerator function used before defined warning - javascript

I'm getting errors like this using Appcelerator/Javascript:
[WARN] JavaScript compiler reported "'getDefaultServerFromDatabase' was used before it was defined." at includes/server.js:36
I honestly didn't know that was an issue with Javascript. Will it cause problems or is it just good practice? I much prefer to have the main function first in the file then the functions it calls after that. Should I switch this around?

It has not caused any differences in behavior in our application.
The only issue it causes us is to fill up the build screen with those warnings so we may miss important warnings or have to scroll to find compile errors.

Related

How to get polymer.js software to report on errors/exceptions?

Is it possible to track errors in polymer.js in a strategic way? Somehow I don't get any error-messages or Exceptions. In case of Errors, Polymer just does nothing if I'm doing something wrong.
For example, if I add the property disableSwipe to paper-drawer-panel, it is ignored, but there is no error-message or logging that could give me a hint what is going wrong. (I still don't know why, but this is offtopic)
Is there any way to activate some Kind of strict-mode that would cause Polymer to throw Exceptions in case of an error, or at least activate some logging that would show error-messages?
Debugging without errorMessages feels too much coincidence-based to me.
Someone else beat you to this complaint and filed an issue with Polymer.js.
https://github.com/Polymer/polymer/issues/3367
I searched through the Polymer project and there is no mention of error handling or exception handling or increased verbosity levels, and until they code something in to implement those features, you'll be left with the default javascript runtime, coding blind and with poor mans debugger.
As an alternative, the polymer.js people recommend using polylint, which won't give you information about runtime errors or exceptions, but instead a static code analysis analyzer, and it might not find your exception because your error might be some off-by-one error that static code analysis can't find.
https://github.com/PolymerLabs/polylint
That's a bad code smell and strike against Polymer.js If you can't bother to put in proper exception reporting and error handling, I can't be bothered to use your product.
The best you're going to get other than polylint is whatever the google javascript developer tools embedded in google chrome browser gives you:
https://developers.google.com/web/tools/chrome-devtools/inspect-styles/shortcuts
That will give you warnings and errors, but that's only if the polymer.js devs wrote an actual bug. If it doesn't halt on an error or warning, you're left to inspect and watch the javascript code line by line, and try to guess what went wrong by reading the Polymer.js source.

Are console errors acceptable in Javascript if they do not cause problems?

I'm a beginner at Javascript, and I wrote some code that works perfectly, but generates a console error from time to time.
It's a coin flip simulator, where the class of the coin needs to change depending on the outcome- so it searches for the opposite outcome class name and changes it to the current outcome class name (using SetAttribute). However, sometimes you get two of the same outcomes in a row, so searching for the opposite class name returns null, and then I get an error for trying to setAttribute of null.
I could write an If statement to avoid this, but I'm wondering if that's necessary. I want to learn best practices, so please, if there's a reason to avoid console errors, let me know!
Console errors such as an unhandled exception indicate a problem in your code. Sometimes the problem might turn out to be benign, but there is no way to know if its benign without studying the code. And, by the time you have studied the code to understand the problem, it is usually one or two lines of code to avoid or handle the problem in your code.
You should add this extra code to handle the problem because if you let these benign warnings build up, then pretty soon you won't be able to notice a new warning pop up that is actually a sign of serious trouble. Your code should be exception free precisely for this reason so when an unexpected issue shows up, you can clearly see it and both you and everyone else working on the project or testing the project knows that a warning like this is unexpected.
So, it is a good practice to clean up all warnings like this and keep your code clean and warning free.
Imagine you were driving your car and a warning light came on. You take it into the mechanic and the mechanic says: "oh, that's how they designed it - the light comes on from time to time". OK, now the warning is basically useless because you don't know when a warning is a sign of a real issue and when a warning is just some incomplete design that didn't clean things up well. It's the same with your code.

JSCOMPILER_PRESERVE after compiling javascript with KJSCompiler

After compiling my JavaScript source with KJSCompiler (https://github.com/knyga/kjscompiler) I get this weird function wrapped around my code. If I try to include it in my live source code I get this error in the console "Uncaught ReferenceError: JSCOMPILER_PRESERVE is not defined ".
I thought that this problem occurred because I didn't add the "wrapper" in the JSON file of kjscompiler. After trying that it still occurs.
Does anyone have an idea how to fix this?
I came across this when using Closure Compiler directly via the Java API.
It seems to be caused by running the compiler in checks-only mode but with protect hidden side effects enabled. The late pass to remove protection of hidden side effects is skipped in checks-only mode.
I'm not sure about KJSCompiler specifically, but it might help to look at changing the compilation level to SIMPLE or ADVANCED, and/or how you might be able to control what checks and optimisations run to KJSCompiler, e.g. turning off CompilerOptions.protectHiddenSideEffects.
See https://github.com/google/closure-compiler/issues/1875
In addition to Steve S's answer:
set protectHiddenSideEffects after setting optimization level, as options.setProtectHiddenSideEffects(false); didn't work for me for GCC version v20180204 if I set it before setting CompilationLevel.
CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(compilerOptions);
//To remove JSCOMPILER_PRESERVE error: https://github.com/google/closure-compiler/issues/1875
compilerOptions.setProtectHiddenSideEffects(false);

Detect missing functions with jsHint or jsLint

I am wondering, why does code like this do not issue a warning with jsLint or jsHint
var obj={};
obj.missingFunction();
I understand that it may assume that the function will be eventually defined. But it complains for so many other things, that i thought it would be natural.
I am doing some heavy refactoring, and moving functions out of the global scope to some properties of objects, and it would be great if there was a way to detect errors early in this case
Thanks to the feedback from elclanrs, i have now installed WebStorm IDE and it does the job perfectly, which is fine for me

debugging javascript -- and gigantic error lists in standard libraries

One of the persistent problems I have when debugging javascript is that should the compiler find an error and put it on the console, the error tends to get lost. The reason being that most of the standard libraries have hundreds of console errors in standard firebug consoles. I use jQuery, bootstrap and Angular and the all produce lots and lots of noise in the console window, that makes it hard to find the real errors.
When I compile code I always clear out ALL warnings so that errors are more evident. But there doesn't seem an easy way to do this in JavaScript.
Any thoughts?
If you think instead of a flat view, an indented view of console logs might help you, you can try using console.group("Group Name") to start a new indentation block and console.groupEnd() to close it.
You can create such indented groups for different phases/interactions in your code, which might make it a little easier to read the logs as they will be nested under the "Group Name" used to create the group.

Categories

Resources