"Convert for loops to enhanced" in JavaScript [message #219363] |
Mon, 18 August 2008 14:21 |
Eclipse User |
|
|
|
Originally posted by: bleper.google.com
Hello folks,
I've noticed something really weird in Eclipse Ganymede regarding
JavaScript. Quick look through the new options, and I've discovered
something like this: JavaScript > Code Style > Clean Up > [edit any
profile] > Code Style > Control statements > Convert for loops to enhanced.
The code snippet on right shows such an "optimization":
for (var i = 0; i < ids.length; i++) {
var value= ids[i] / 2;
alert(value);
}
becomes
for (var element in ids) {
var value= element / 2;
alert(value);
}
WTF? This is not only vary bad practice; it won't work in the first place!
For those equally horrified as me: yes, I tried to apply this automatic
"fix" to my code, and fortunately it didn't do anything. ;-)
BL
|
|
|
Re: "Convert for loops to enhanced" in JavaScript [message #219460 is a reply to message #219363] |
Wed, 20 August 2008 16:52 |
Eclipse User |
|
|
|
Originally posted by: david.mcneill.ge.com
I don't see what the problem is. It converted the for...next syntax into a
for...each syntax. It does the same thing (or should, anyway), but in a
more "object oriented" style. If it worked, it would be pretty nice. You
wouldn't have to worry about counter values and < versus <=, for instance.
The fact that it didn't do anything is the problem, in my view.
Have you been able to set a breakpoint in javascript?
|
|
|
Re: "Convert for loops to enhanced" in JavaScript [message #219774 is a reply to message #219460] |
Fri, 29 August 2008 10:46 |
Bartosz Leper Messages: 4 Registered: July 2009 |
Junior Member |
|
|
Dave M wrote:
> I don't see what the problem is.
Well, the problem is that this piece of software was apparently written by
someone who worked with JavaScript for less than a month.
> It converted the for...next syntax into a
> for...each syntax. It does the same thing (or should, anyway), but in a
> more "object oriented" style.
The syntactical coincidence between JavaScript and other languages here is
nothing more than a coincidence. The "for-in" loop was NEVER MEANT to be
used to iterate on arrays. No, it won't do the same thing. "for-in"
iterates over KEYS of an OBJECT, not its values. This means that in such
case:
var a = ['a', 'b', 'c'];
for (var i in a) {
alert(i);
}
You will be presented with pop-ups that don't contain 'a', 'b', and 'c',
but 0, 1, 2 instead. So the more correct version is:
for (var i in a) {
alert(a[i]);
}
But it's still flawed. Nobody said that it's illegal to extend the
prototype of Array, for example. This is what Prototype framework does,
for example. Consider such thing:
Array.prototype.bark = function() {
alert('woof!');
};
a = ['a', 'b', 'c'];
for (var i in a) {
alert(a[i]);
};
You will see: 'a', 'b', 'c', 'bark'. Ouch!
Moreover, it's only a coincidence that on most platforms it iterates in a
correct order. In fact, the language specifications don't give us any
guarantee about the order! And yes, there are browsers which iterate in
non-sequential order. And no, it's not that bad, because again: for-in is
not meant to iterate through array indexes. It's for iterable object
properties.
So now, I think it's clear that it definitely does NOT do what we want. :)
> If it worked, it would be pretty nice.
If it worked, it would break a hell lot of a code.
> Have you been able to set a breakpoint in javascript?
I don't know, I didn't try that. I remember using an older version of this
with a Mozilla plugin, but without success, and I didn't bother trying
now. I'll stick to raw Firebug for now, but I think that it's a completely
different topic. :)
So, to conclude: does any Eclipse developer listen to my moans? ANYONE?...
Don't force me to spam your precious mailing lists. ;)
Greetings,
Bartek
|
|
|
Re: "Convert for loops to enhanced" in JavaScript [message #219800 is a reply to message #219774] |
Fri, 29 August 2008 13:44 |
Eclipse User |
|
|
|
Originally posted by: mauro.molinari.cardinis.com
Bartosz Leper ha scritto:
> So, to conclude: does any Eclipse developer listen to my moans?
> ANYONE?... Don't force me to spam your precious mailing lists. ;)
Why don't you simply add a new bug report?
Cheers,
Mauro.
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02260 seconds