Friday, August 14, 2015

Heisenberg-effect while debugging JavaScript code in Chrome

I had a hard time finding a bug today in some AngularJS code and I think I got peopled by the Chrome JavaScript engine ( V8 right? ). I think it doesn't load variables into memory when it knows it's not going to use it. 

Check out my question here if you're interested on StackOverflow. I was trying to access that $http variable which is being injected into the controller, from the doSubmit function. However - I just had a debugger statement in that function and no direct call to $http. This gave me '$http not defined' errors all the time - so I thought I was doing things wrong with the controllerAs syntax or variable injection.

However, loosly interpreting the Heisenberg principle as 'measuring the system influences the system', turned out to be the case. The console.log() call influenced the system. Because of that line - the $http was not optimized away anymore and it does work if you keep that line.


Interesting stuff. The .NET counterpart of these kind of problems are Debug and Release builds. Check out the in-depth courses by Bart de Smet on C# if you're interested what's the difference is between these type of builds and why you could have NullRef exceptions in .NET code while running a Release build, that never occur in Debug mode.

No comments:

Post a Comment