chuanxshi / javascript-patterns

JavaScript Design Patterns
http://shichuan.github.io/javascript-patterns
9.39k stars 1.7k forks source link

Define "trickyness" in for-loop pattern #148

Open bradym80 opened 9 years ago

bradym80 commented 9 years ago

On line 35 of the for-loop pattern the following comment appears.

// optimization 3 - substitutei++withi = i + 1ori += 1to avoid excessive trickiness

Could you elaborate on "excessive trickiness"?

klomontes commented 9 years ago

@bradym80 I was wondering the same thing. My assumption was that i = i + i is easier to understand since i++ requires some previous knowledge about this iterator. Not sure if there are some performance differences.

Anyway, I find i++ beautifully succinct and not tricky at all. :)

sumn2u commented 9 years ago

When you write these expressions as i = i + 1 it's clear what the intent of the programmer was and easier to find bugs in the code where as i++ prevents excess craftiness.

gianlucascoccia commented 9 years ago

Well, if i++ is too tricky I find it funny that in preferred 1 and 2 you use i-- :)