0
0

[–] BitterBiped ago  (edited ago)

The post ++ is better than the pre ++. Because if it weren't Stroustrup would have called it ++C and not C++, kappa.

0
0

[–] WhiteMakesRight ago 

Who cares. CoffeeScript took "nice" syntax to it's logical conclusions and ended up with the ugliest and most unreadable language ever created.

0
0

[–] RicardoBronson ago  (edited ago)

They both suck because of unspecified behavior in some situations. And usually when you want it least. Like for loops using pointers. Trust me, just say "counter += 1" and you will never have to know the pain EDIT: Actually why bother trusting me, do a search: https://www.google.com/search?hl=en&q=postfix%20increment%20unspecified%20behavior

1
-1

[–] WhiteMakesRight 1 point -1 points (+0|-1) ago  (edited ago)

The operators have nothing to do with the unspecified behaviour you're talking about though -- they're just a succinct way to trigger it. What's actually unspecified is the order of evaluation of function arguments -- which is something you should be aware of even if you never use increment operators.

Not using increment operators at all is a typical example of "scarring on the first cut". If you're going to write software in C, rules-of-thumb will only get you so far. At some point, you have to read and understand the specification in depth.

0
0

[–] RicardoBronson ago 

I take more of a "when in doubt, do it in python" approach because life is just too short to spend on specifications :) But I'm sure you're right

0
0

[–] danielabrown ago 

the both are perfect. once they are well utilized properly. normally on hackrypton.com there are so many tutorials that will help in securing things like that easily

0
0

[–] badkangaroo ago 

i often see in a for loop (;;++i) or (;;i++) someone suggesting that ++i was better for some reason, any ideas why?

1
-1

[–] WhiteMakesRight 1 point -1 points (+0|-1) ago  (edited ago)

It makes zero difference which of the two you use as the third expression in a for loop. Whenever you see someone prescribing rules without explaining their reasoning, it's a good idea to disregard everything they say.

0
1

[–] skruf [S] 0 points 1 point (+1|-0) ago  (edited ago)

Even though modern compilers optimize for either of these uses, I'm sure, the idea was that when post-incrementing (i++), 'i' here will return the value and then increment it, which apparently takes more time than pre-increment (++i) where you would increment and then get the new value.

Actually, I'm in the habit of pre-increment in the for-loops, I think it's more of an uncontrolled obsession than anything else...

0
0

[–] RevanProdigalKnight ago  (edited ago)

Just in case you didn't already know, there is actually a difference between counter++ and ++counter, as the first increments after reading the value and the second before reading the value, e.g.:

int counter = 0;

printf("%d\n", counter++); // 0
printf("%d\n", ++counter); // 2

Most people use counter++ because of this behavior being closer to the logical alternative of counter += 1.

0
0

[–] scandalous-goat ago  (edited ago)

I prefer x += 1 for an incrementation statement, as the intent is, IMO clearer. But I'm sure plenty would disagree with me.
I didn't compare the compiler's output for all three syntax, though.

edit:

I just did a test. The compiler produces exactly the same code for all three syntaxes if they are used as a statement.

int postfix()
{
   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   83 ec 10                sub    $0x10,%esp
    int x = 1;
   6:   c7 45 fc 01 00 00 00    movl   $0x1,-0x4(%ebp)
    x++;
   d:   83 45 fc 01             addl   $0x1,-0x4(%ebp)
    return x;
  11:   8b 45 fc                mov    -0x4(%ebp),%eax
}
  14:   c9                      leave  
  15:   c3                      ret    

00000016 <_prefix>:

int prefix()
{
  16:   55                      push   %ebp
  17:   89 e5                   mov    %esp,%ebp
  19:   83 ec 10                sub    $0x10,%esp
    int x = 1;
  1c:   c7 45 fc 01 00 00 00    movl   $0x1,-0x4(%ebp)
    ++x;
  23:   83 45 fc 01             addl   $0x1,-0x4(%ebp)
    return x;
  27:   8b 45 fc                mov    -0x4(%ebp),%eax
}
  2a:   c9                      leave  
  2b:   c3                      ret    

0000002c <_statement>:

int statement()
{
  2c:   55                      push   %ebp
  2d:   89 e5                   mov    %esp,%ebp
  2f:   83 ec 10                sub    $0x10,%esp
    int x = 1;
  32:   c7 45 fc 01 00 00 00    movl   $0x1,-0x4(%ebp)
    x += 1;
  39:   83 45 fc 01             addl   $0x1,-0x4(%ebp)
    return x;
  3d:   8b 45 fc                mov    -0x4(%ebp),%eax
}
  40:   c9                      leave  
  41:   c3                      ret    

0
0

[–] skruf [S] ago 

This is one of the things I think makes programming interesting, the different styles there is. The most interesting example I think of is Fox-Toolkit's style, the author has an, I may say, interesting style...

0
1

[–] Rakosman 0 points 1 point (+1|-0) ago 

I prefer to always use the postfix operator. To me it is more important to have easy to read code than succinct code, and I also think the prefix one is uglier, so there's that.

0
0

[–] WhiteMakesRight ago 

But they're different operators, with different semantics......

1
-1

[–] Rakosman 1 point -1 points (+0|-1) ago 

Yes, and?

load more comments ▼ (3 remaining)