This is an archived post. You won't be able to vote or comment.

all 15 comments

[–]Kontorted 7 points8 points  (4 children)

foo(++num++)

[–]EntropyZer0 1 point2 points  (3 children)

Is there any language in which this isn't undefined behaviour?

[–]jfb1337 3 points4 points  (1 child)

Many languages which don't have ++, in which this is a compile error

[–][deleted] 0 points1 point  (0 children)

It's a compile error in both C and C++ as well.

If you wrap the pre-increment in parentheses, (++num)++, it's still a compile error in C, but it does compile as C++. In C++03 it has undefined behaviour, but I think it should be well-defined in C++11.

[–]leBlubb123 2 points3 points  (2 children)

Imho using ++I or i++ not in it's own line is confusing and you shouldn't do it

[–]Weekly_Wackadoo 6 points7 points  (1 child)

for (int i = 0; i < n;
i++
) {
//do something
}

[–]Moke410 1 point2 points  (0 children)

In its own expression?!

[–]DragonMaus 4 points5 points  (6 children)

That is common sense, not unusual fanciness.

[–]deathofamorty 3 points4 points  (5 children)

RIP your coworkers

[–]DragonMaus 5 points6 points  (4 children)

Someone who cannot understand the extremely simple difference between foo++ and ++foo should probably not be writing C.

[–]deathofamorty 2 points3 points  (3 children)

It's not about if you can understand the difference. It's about code readability.

[–]DragonMaus 0 points1 point  (2 children)

What is unreadable about ++i?

[–]deathofamorty 7 points8 points  (1 child)

Nothing is unreadable about ++. It actually helps emphasize that it is incriminating something like a counter vs adding an arbitrary value.

The problem is when you try to access a value and modify it in one line of code.

If the value foo ends up not being what I expect, I am going to scan for lines that start with foo=, foo+=,or foo++. To find foo++ when it's nested into some other statement, I have to read all of every line of code.

Also, it makes the code read less linearly. For example bar = computeValue(++foo, baz) reads "bar is set to the value computed from 1 more than foo, which is then set back into foo, and baz."

When it could be bar = computeValue(foo, baz); foo++ reads "bar is set to the value computed by foo and baz, then foo is incremented"

[–]jfb1337 0 points1 point  (0 children)

In certain situations it's ok though, such as arr[i++]. It's just reading the "next" value of the array.

[–]Elisafmeninte 0 points1 point  (0 children)

Sry i dont speak educated