++i vs i++

## Or Pre-increment vs Post-increment i++ is essentially more efficient when it comes to dealing with array indexes, as it includes the 0 as the first index.

For example, if i = 0, i++ will return 0, and the next call to i will show i has indeed been incremented. Try the above commands in your js console:

i = 0
i++ // Print 0
i   // Print 1

++i is theoretically faster than i++ since all it does is incrementing i without a temporary variable to store the next value of i. Thus, it could be used for example, when one would like to have a faster for loop.

i = 0
++i // Print 1
i   // Print 1

-~i && ~-i

## Or The tadpole operator This went back two months ago when a guy from MS (Raymond?) trolled HN newbies with this new MS’s VS feature.

It’s funny how young developers express their stigma against news thing coming from big corp. Or maybe, they were just joking. Cannot do correlation with forum behavior data I suppose…

A simple math equation show it is equivalent to i+1 and i-1 respectively (Google it man…) The memo below (from Raymond’s blog) is pretty handy when encoutering this guy:

  - -~i is a tadpole swimming toward i, thus it increases i by 1
  - ~-i is a tadpole swimming away from i, thus it decreases i by 1

It’s a lovely “operator” when you wanted to skip the parenthesis for sure, as it’s executed first before other math op. These two lines are equivalent:

  (i+1)*(i-1)
  -~i*~-i

Don’t abuse it tho… the damn thing when stacked is proven to be not digestible, so don’t try this:

  -~-~i
  i+2

with your boss…

On the other hand, I do prefer code with just zeros and letters… Arts?

Mid-point expansion?

### Or just a fun experiment…

Given an index. If one need to get the adjacent indexes of the current index, what should one do?

Combining the incremental and tadpole gave us one heck of a funny solution:

i = 0
j = ~-i++ // j==-1 && i==1
---------
j = -~i-- // j==1  && i==-1

Time to wrap myself up to polish the tutorial on how I setup this blog. Jekyll and all that css tricks… also, how to be organized…

Probably coming kinda soon…



blog comments powered by Disqus

Published

19 July 2015

Category

bs

Tags