-
Notifications
You must be signed in to change notification settings - Fork 548
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JavaScript: Fixes to comparisons, iteration #3022
Conversation
// Find matching clip | ||
for (var clip_index = 0; clip_index < $scope.project.clips.length; clip_index++) { | ||
if ($scope.project.clips[clip_index].id == clip_id) { | ||
for (const clip of $scope.project.clips) { | ||
if (clip.id == clip_id) { | ||
// Set audio data | ||
$scope.$apply(function(){ | ||
$scope.project.clips[clip_index].audio_data = audio_data; | ||
$scope.project.clips[clip_index].show_audio = true; | ||
clip.audio_data = audio_data; | ||
clip.show_audio = true; | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mentioned that for...of
often makes the code cleaner: This change makes for a good representative example. The updated version is so much more readable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
LG |
…l HTML rendering on my local dev version.
@ferdnyc Well, on actual testing of this PR, it breaks all HTML rendering / JS syntax issues. Maybe a Qt version issue? Not sure, but I've kept some of the PR (the === comparisons), but reverted the loop code. |
Hrmf, that's too bad. I suspect it's a WebKit version issue. Everything worked fine in my local testing, but that's running with a QtWebKit build that's less than two months old, based on presumably a very recent WebKit. (Though I can't seem to find anyplace that actually indicates specifically what version, which is annoying.)
|
As Codacy was kind enough to point out, we should:
Use === and !== with bool, numbers
We should really be using
===
and!==
, instead of==
and!=
(respectively) when comparing values to Number or Boolean, to ensure that we're not accidentally type-converting values unexpectedly.Iterate over arrays with
for...of
loopsvariable[key]
syntax is evil, which makes loops of the formfor (var index = 0; index < array.length; index++) { }
inherently problematic, because they're going to accessarray[index]
inside the loop.Fortunately, there's a better way:
for...of
. Not only is is better/safer, but it often makes the code so much cleaner. So, I've replaced all of the indexedfor()
loops that looped over arrays from[0 ... length]
withfor (const element of array) { }
loops. (Which is another advantage tofor...of
loops: The iterator variable can beconst
, if the array contents don't need to be modified.)So
for...of
both cuts down on temporary variables and helps keep everything block-scoped. (var
has scoping issues. In fact, by changing toconst
iterator variables I already caught one function where a variable was being redeclared inside a large-ish loop. In that particular case it didn't matter, but the fact thatvar
allows that to happen silently is a problem.)As I said, I replaced all of the
for (index = 0; index < array.length; index++)
loops. I left a few indexedfor ()
loops alone, though, like the ones that iterated backwards over the list elements fromlength - 1
to0
. Basically, if it couldn't be trivially converted tofor...of
, I let it be.