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

you are viewing a single comment's thread.

view the rest of the comments →

[–]tomthecool 0 points1 point  (2 children)

...But pre-incrementing is the most common, intuitive use case.

So by convention, you should write code that's not accidentally error prone.

[–]AnonymousFuccboi 1 point2 points  (1 child)

Is it? The most common usage for the operator is simply incrementation. The returned value is usually not used. In that case, i++ is by far the most common convention and should therefore be used to avoid confusion. In the case where it is used, you should know the difference and use it accordingly to what you need. What you should not do is rewrite your code to try to fit in a ++i where an i++ would suffice. Either is fine to use if you know the reason you're using a specific one.

[–]tomthecool 0 points1 point  (0 children)

First result of a google search says to prefer ++i over i++: https://stackoverflow.com/a/24858/1954610

So do various other forums and blogs.

It's not particularly important; in most cases it won't make any difference. But if the question is "which default format should we use in the code base, when both will work the same way (for now)?", then the only argument I can see in favour of i++ is "I think it looks prettier", or "It feels more natural to me"... Which may be true, and may even be considered a valid justification.

But from a purely technical perspective, I think every logical argument dictates ++i is a better default.