0

Bit of background to explain the reasoning: I've been programming for a good while, but took a break between 2012 and 2014 for other stuff. Before that break, I would hardly ever hear about "++i", let alone hear decent explanations about it ("...isn't it... just better?" was common to hear). Now, however, I see it everywhere, and even some text editors seem to use this by default (Sublime Text comes to mind).

So I've been wondering; did "++i" become suddenly very popular as of late, or is my new work environment pro-prefix increment operators?

(another possible solution is that my old environment wasn't big on "++i" for some reason)

Note that I am definitely not asking whether this is a good thing or not; I'm well aware of how pre-increment operators work, and honestly there are enough debates about this on SO. I'm just asking if this is a local trend, or a more global one. Also this is strictly for C, not C# or Java.

Linkyu
  • 113
  • 6
  • Sorry; by "everywhere", I meant everywhere around me where I work. That's to say, in my teachers' lessons, in my classmates' codes, etc. I also asked strictly in C because I didn't see this much happen in the other languages, but then again we really mostly work with C. – Linkyu Apr 07 '15 at 14:53
  • 1
    `return recurse(i++)` rarely does what someone expects it to. `return recurse(++i)` is better in that its more likely to be what is expected. `return recurse(i + 1)` would be better yet. Maybe someone got burnt with the first one and switched to their own form of [yoda conditions](http://programmers.stackexchange.com/q/16908/40980)? –  Apr 07 '15 at 14:57
  • `So I've been wondering; did "++i" become suddenly very popular as of late, or is my new work environment pro-prefix increment operators?` - No? I mean, it became suddenly popular like 15 years ago from what I remember. There was a book (Effective C++? That doesn't seem right looking at a chapter list, but it was something like that) that pointed out a micro-optimization with using `++x` in `for` loops. – Telastyn Apr 07 '15 at 15:14
  • In C++ with custom iterators this might be a useful optimization. In C I don't see why it should matter. – CodesInChaos Apr 07 '15 at 15:16
  • @iheanyi I guess I overshoot that one a little. You're right that I shouldn't have used "a good while". What I meant was "I've been programming for 7 years, and am on my way to a PhD in AI". I figured it would have been more information than needed. – Linkyu Apr 07 '15 at 15:55
  • i see it all too often. particularly as an iterator in a `for(;;)` loop. i remember learning C in the context of also programming for the Motorola MC680x0. the post-increment on pointers was nearly always **free**, with the post-increment addressing mode. personally, i would **never** use a pre-increment and seldom use a pre-decrement (only when i have created my own LIFO stack, i would use post-increment along with pre-decrement). otherwise i would never pre- anything. i just think it's bad form. – robert bristow-johnson Apr 07 '15 at 16:09
  • 1
    The "good while" threw me off because it seemed to suggest broader exposure. My experience both in and out of school shows that some trends do start in academia and some take a really long time to break into academia. When I started my grad program in 2008, version control was still new to my dept (EE) and relatively new to the CS dept. To be fair, summer after my first year, I worked in a robotics lab at another school and they'd been using version control for years. – iheanyi Apr 07 '15 at 16:34
  • Historical background: The postincrement and predecrement operators have their roots in the PDP-11 indirect addressing modes. The addressing modes were designed that way SPECIFICALLY to facilitate operand stack PUSH/POP operations, stack-based subroutine call/return, and immediate operands (using postincrement on the program counter). C provided those cases to make it easy to generate tight code for computers with (by 21st century standards) microscopic memories. Later, they were generalized. I never see preincrement or postdecrement operators used. – John R. Strohm Apr 07 '15 at 16:57

1 Answers1

4

In C++, the pre-increment operator may be written to return a reference to the incremented object. The post-increment operator has to return a copy (because the return value is the value before the increment operation).

So, if you're using increment and don't care about the return value (such as in a loop increment), then you want to prefer pre-increment as you might be incrementing something that's costly to copy.

In C this is irrelevant, but I know that once I developed the habit in C++, it spread to my C code as well.

Michael Kohne
  • 10,038
  • 1
  • 36
  • 45
  • Hmmm. This could very well be the case; we do have a fair focus on micro-optimizations here. Maybe the same kind of spreading happened. – Linkyu Apr 07 '15 at 15:19
  • @Linkyu It is also safer to use pre-increment. Post increment can be confusing. Say I have two lines: int x = 1; int y = x++; What is the value of y? Now, mix that with some pointer math and to just understand a single line of code, one may need to spend a couple minutes and draw out a diagram. That's horrible code to maintain and fix (if there is a bug). The simpler approach is to just ban the use altogether and move people towards being more expressive. Pre-increment just does a much better job of conveying the programmer's intent. – iheanyi Apr 07 '15 at 15:55
  • Michael, could you educate me a little? (i'm more of a C coder, have coded in C++ but only to add methods to existing classes, the heart of my code was still C.) now, when you return a reference, the copy is just a copy of a pointer. why is copying the pointer (for the return value) before it is incremented more costly than copying the pointer after it is incremented? – robert bristow-johnson Apr 07 '15 at 16:15
  • 2
    @robertbristow-johnson - When you return from a postincrement operation you CAN NOT return a reference, you return a completely new object, which is a copy of the state of the original object BEFORE the increment operation. That copy may be expensive, and returning an object may itself be expensive. – Michael Kohne Apr 07 '15 at 16:36
  • thanks @MichaelKohne. it seems to me to be just another wart about C++ that i do not like. i think that OOP is both necessary and, if done right, elegant. but i do not think that C++ is an elegant example of OOP. i agree that copying objects can be expensive. but i thought that "passing by reference" precludes that. – robert bristow-johnson Apr 07 '15 at 17:43
  • 1
    @robertbristow-johnson - When you pass by reference you do avoid the copy. The point here is that in the postincrement case you can't pass by reference because you need to return a different state than the one the object will be in once the increment happens. – Michael Kohne Apr 07 '15 at 17:44
  • @iheanyi: the value of `y` is the value of `x` prior to the increment; i.e., 1. That's perfectly clear (if you know C). Are you suggesting that the postinc shouldn't be used at all, as in `x = 1; y = x; ++x;`? – John Bode Apr 07 '15 at 19:31
  • @JohnBode Yes, I'm suggesting postinc shouldn't be used at all. It's only perfectly clear in that trivial example for people who know C. However, this construct "*p++" regularly trips those who know C. And by trip, I mean it either takes them much longer to grasp what is going on than if the programmer simply split that logic into a few statements or worse - they get it wrong. As programmers spend more time reading code than writing, I don't see why I should shoot myself in the foot or setup a maintenance headache by using postincrement. – iheanyi Apr 07 '15 at 22:44
  • @JohnBode In trivial and some non-trivial cases, it only costs you one line of code to not use it. In non-trivial cases, I'm faced with either spending time being 95% sure I understood what I read or little time reading a couple more lines of code and being 100% sure I understood. – iheanyi Apr 07 '15 at 22:48
  • Okay I'm going to accept this answer; after some research around, I could conclude it was indeed spreading from one person, mostly. They're a researcher in 3D graphic data processing (among other things) and ++i is the kind of small optimization that becomes really useful (unless you're fine with waiting a month every time you process the data). From what I could get, they do have a fair influence around, and most people I've talked to started using ++i more often thanks to him. Normally I would make this an answer but the question has been put on hold (and I don't know how to edit it to fit). – Linkyu Apr 08 '15 at 17:32
  • That being said, of course, it doesn't explain why Sublime Text uses ++i by default in for loops, but then again I can see no reason why it shouldn't. – Linkyu Apr 08 '15 at 17:33