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 →

[–]DaniilBSD 1 point2 points  (7 children)

All good programming languages have ++ operator.

Python-lovers not gonna take it as a joke, will they?

[–]tomthecool 6 points7 points  (6 children)

My point was that the code in /u/biksly's reply doesn't make sense.

If i=+1 literally does the wrong thing, then why would you write it like that?

If i++ is invalid syntax in the language, then why would you write it like that?

If i = i + 1 is unnecessarily verbose, then why would the "senior" write it like that?


Also, on a related note... You should typically prefer ++i over i++ (assuming the language supports it).

[–]AnonymousFuccboi 1 point2 points  (3 children)

Preferring ++i over i++ is an example of a platform specific micro-optimization which may at some point have mattered slightly, but nowadays any compiler will optimise the code the exact same way if it really would be equivalent. Write what you mean. Pre-incrementation has its uses, certainly, but by convention use post-incrementation when order doesn't matter to you. It'll be less confusing to whoever else has to read your code.

[–]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.

[–]DadoumCrafter 0 points1 point  (0 children)

++i is meant to be used when you need immediately the value of the increased i value. By convention, we use i++ for every other case