Skip to content

Loading…

Cache array length in ol.control.Attribution #636

Merged
merged 1 commit into from

3 participants

@twpayne

No description provided.

@fredj
OpenLayers member

LGTM

@elemoine elemoine commented on the diff
src/ol/control/attributioncontrol.js
@@ -84,7 +84,7 @@ ol.control.Attribution.prototype.getTileSourceAttributions =
continue;
}
tileRanges = usedTiles[tileSourceKey];
- for (i = 0; i < tileSourceAttributions.length; ++i) {
+ for (i = 0, ii = tileSourceAttributions.length; i < ii; ++i) {
@elemoine OpenLayers member

I've been wondering about this construct, i.e. separating variable assignments with comas without a heading var keyword. It turns out that this is using the JavaScript's Comma Operator, which is fine. But it is to be noted that:

var i = 1, ii = 10;

is not the same as:

var i, ii;
i = 1, i = 10;

In the second code block, i = 1, i = 10 is an actual expression that returns the value of the second operand.

@twpayne
twpayne added a note

Interesting! Thanks.

For me, jslint complains about for (var i = 1, ii = 10; ...) so I've moved the var declarations out of the for statement.

@elemoine OpenLayers member

Same for me. So I also do like that var declarations are out of for statements.

Also, on a related note. I like

var someVarName0 = 0;
var someVarName1 = 1;
var someVarName2 = 2;
var someVarName3 = 3;
var someVarName4 = 4;

I find this ok:

var a, b, c, d;

But I dislike:

var someVarName0 = 0,
      someVarName1 = 1,
      someVarName2 = 2,
      someVarName3 = 3,
      someVarName4 = 4;

Just a matter of taste, but I find the last construct less readable, especially when combining declarations of initialized and uninitialized variables.

@twpayne
twpayne added a note

Regarding var declarations, I have exactly the same tastes as you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@elemoine
OpenLayers member

+1 from me. Thanks Tom.

@twpayne twpayne merged commit bc3dc0f into openlayers:master

1 check passed

Details default The Travis build passed
@twpayne twpayne deleted the twpayne:cache-length branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 22, 2013
  1. @twpayne
Showing with 6 additions and 6 deletions.
  1. +6 −6 src/ol/control/attributioncontrol.js
View
12 src/ol/control/attributioncontrol.js
@@ -74,7 +74,7 @@ ol.control.Attribution.prototype.getTileSourceAttributions =
function(usedTiles, sources) {
/** @type {Object.<string, ol.Attribution>} */
var attributions = {};
- var i, tileRanges, tileSource, tileSourceAttribution,
+ var i, ii, tileRanges, tileSource, tileSourceAttribution,
tileSourceAttributionKey, tileSourceAttributions, tileSourceKey, z;
for (tileSourceKey in usedTiles) {
goog.asserts.assert(tileSourceKey in sources);
@@ -84,7 +84,7 @@ ol.control.Attribution.prototype.getTileSourceAttributions =
continue;
}
tileRanges = usedTiles[tileSourceKey];
- for (i = 0; i < tileSourceAttributions.length; ++i) {
+ for (i = 0, ii = tileSourceAttributions.length; i < ii; ++i) {
@elemoine OpenLayers member

I've been wondering about this construct, i.e. separating variable assignments with comas without a heading var keyword. It turns out that this is using the JavaScript's Comma Operator, which is fine. But it is to be noted that:

var i = 1, ii = 10;

is not the same as:

var i, ii;
i = 1, i = 10;

In the second code block, i = 1, i = 10 is an actual expression that returns the value of the second operand.

@twpayne
twpayne added a note

Interesting! Thanks.

For me, jslint complains about for (var i = 1, ii = 10; ...) so I've moved the var declarations out of the for statement.

@elemoine OpenLayers member

Same for me. So I also do like that var declarations are out of for statements.

Also, on a related note. I like

var someVarName0 = 0;
var someVarName1 = 1;
var someVarName2 = 2;
var someVarName3 = 3;
var someVarName4 = 4;

I find this ok:

var a, b, c, d;

But I dislike:

var someVarName0 = 0,
      someVarName1 = 1,
      someVarName2 = 2,
      someVarName3 = 3,
      someVarName4 = 4;

Just a matter of taste, but I find the last construct less readable, especially when combining declarations of initialized and uninitialized variables.

@twpayne
twpayne added a note

Regarding var declarations, I have exactly the same tastes as you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
tileSourceAttribution = tileSourceAttributions[i];
tileSourceAttributionKey = goog.getUid(tileSourceAttribution).toString();
if (tileSourceAttributionKey in attributions) {
@@ -134,8 +134,8 @@ ol.control.Attribution.prototype.updateElement_ = function(frameState) {
sources[goog.getUid(source).toString()] = source;
var attributions = source.getAttributions();
if (!goog.isNull(attributions)) {
- var attribution, i;
- for (i = 0; i < attributions.length; ++i) {
+ var attribution, i, ii;
+ for (i = 0, ii = attributions.length; i < ii; ++i) {
attribution = attributions[i];
attributionKey = goog.getUid(attribution).toString();
attributionsToRemove[attributionKey] = true;
@@ -154,8 +154,8 @@ ol.control.Attribution.prototype.updateElement_ = function(frameState) {
var attributionKeys =
goog.array.map(goog.object.getKeys(attributions), Number);
goog.array.sort(attributionKeys);
- var i, attributionElement, attributionKey;
- for (i = 0; i < attributionKeys.length; ++i) {
+ var i, ii, attributionElement, attributionKey;
+ for (i = 0, ii = attributionKeys.length; i < ii; ++i) {
attributionKey = attributionKeys[i].toString();
if (attributionKey in this.attributionElements_) {
if (!this.attributionElementRenderedVisible_[attributionKey]) {
Something went wrong with that request. Please try again.