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.

closed as primarily opinion-based by Robert Harvey, gbjbaanb, gnat, durron597, Blrfl Apr 7 '15 at 16:57

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 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 7 '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? – user40980 Apr 7 '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 7 '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 7 '15 at 15:16
  • 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 7 '15 at 16:34
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.

  • 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 7 '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 7 '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 7 '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 7 '15 at 16:36
  • 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 7 '15 at 17:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.